--- - name: Disable pinned Docker version file: dest: "/etc/apt/preferences.d/docker-ce.pref" state: "absent" when: not docker__version | d() - name: Enable pinned Docker version template: src: "etc/apt/preferences.d/docker-ce.pref.j2" dest: "/etc/apt/preferences.d/docker-ce.pref" owner: "root" group: "root" mode: "0644" when: docker__version | d() - name: Install Docker's dependencies apt: name: "{{ docker__package_dependencies + docker__pip_dependencies }}" - name: Add Docker's public PGP key to the APT keyring apt_key: id: "{{ docker__apt_key_id }}" url: "{{ docker__apt_key_url }}" - name: Configure Docker's upstream APT repository apt_repository: repo: "{{ docker__apt_repository }}" update_cache: true - name: Install Docker apt: name: "docker-{{ docker__edition }}" state: "{{ docker__state }}" - name: Check for existing Docker Compose file stat: path: "/usr/local/bin/docker-compose" register: docker__register_docker_compose - name: Remove Docker Compose if it's been installed without PIP file: path: "/usr/local/bin/docker-compose" state: "absent" when: - docker__register_docker_compose.stat.exists - docker__register_docker_compose.stat.isreg | d(false) - name: Install Virtualenv pip: name: "virtualenv" - name: Install Python packages pip: name: > {{ item.name }}{% if item.version | d() %}=={{ item.version }}{% endif %} virtualenv: "{{ docker__pip_virtualenv }}" state: "{{ item.state | d('present') }}" loop: "{{ docker__default_pip_packages + docker__pip_packages }}" when: item.name | d() - name: Symlink Python binary to /usr/local/bin/python-docker file: path: "/usr/local/bin/python-docker" src: "{{ docker__pip_virtualenv }}/bin/python" state: "link" - name: Symlink selected Python package binaries to /usr/local/bin file: path: "{{ item.path }}" src: "{{ item.src }}" state: "link" loop: "{{ docker__default_pip_packages + docker__pip_packages }}" when: - item.state | d("present") != "absent" - item.path | d() and item.src | d() - name: Add user(s) to "docker" group user: name: "{{ item }}" groups: "docker" append: true loop: "{{ docker__users }}" - name: Create Docker configuration directories file: path: "{{ item }}" state: "directory" owner: "root" group: "root" mode: "0755" loop: - "/etc/docker" - "/etc/systemd/system/docker.service.d" - name: Configure Docker daemon options (json) template: src: "etc/docker/daemon.json.j2" dest: "/etc/docker/daemon.json" owner: "root" group: "root" mode: "0644" when: docker__default_daemon_json or docker__daemon_json notify: ["Restart Docker"] - name: Configure Docker daemon options (flags) template: src: "etc/systemd/system/docker.service.d/options.conf.j2" dest: "/etc/systemd/system/docker.service.d/options.conf" owner: "root" group: "root" mode: "0644" register: docker__register_daemon_flags when: docker__daemon_flags notify: ["Restart Docker"] - name: Configure Docker daemon environment variables template: src: "etc/systemd/system/docker.service.d/environment.conf.j2" dest: "/etc/systemd/system/docker.service.d/environment.conf" owner: "root" group: "root" mode: "0644" register: docker__register_daemon_environment when: docker__daemon_environment notify: ["Restart Docker"] - name: Configure custom systemd unit file override template: src: "etc/systemd/system/docker.service.d/custom.conf.j2" dest: "/etc/systemd/system/docker.service.d/custom.conf" owner: "root" group: "root" mode: "0644" register: docker__register_custom_override when: docker__systemd_override | d() notify: ["Restart Docker"] - name: Reload systemd daemon systemd: daemon_reload: true when: (docker__register_daemon_flags | d() and docker__register_daemon_flags is changed) or (docker__register_daemon_environment | d() and docker__register_daemon_environment is changed) or (docker__register_custom_override | d() and docker__register_custom_override is changed) notify: ["Restart Docker"] - name: Restart Docker now to make sure `docker login` works meta: "flush_handlers" - name: Manage Docker registry login credentials docker_login: registry_url: "{{ item.registry_url | d(omit) }}" username: "{{ item.username }}" password: "{{ item.password }}" email: "{{ item.email | d(omit) }}" reauthorize: "{{ item.reauthorize | d(omit) }}" config_path: "{{ item.config_path | d((ansible_env.PWD | d('/root')) + '/.docker/config.json') }}" state: "{{ item.state | d('present') }}" loop: "{{ docker__registries }}" when: item.username | d() and item.password | d() vars: ansible_python_interpreter: "{{ '/usr/bin/env python-docker' if docker__pip_virtualenv | d() else ansible_python.executable }}" - name: Remove Docker related cron jobs file: path: "/etc/cron.d/{{ item.cron_file }}" state: "absent" loop: "{{ docker__cron_jobs }}" when: - item.state | d("present") == "absent" - item.name | d() and item.job | d() - item.schedule | d() and item.cron_file | d() - name: Create Docker related cron jobs cron: name: "{{ item.name }}" job: "{{ item.job }}" minute: "{{ item.schedule[0] }}" hour: "{{ item.schedule[1] }}" day: "{{ item.schedule[2] }}" month: "{{ item.schedule[3] }}" weekday: "{{ item.schedule[4] }}" cron_file: "{{ item.cron_file }}" user: "{{ item.user | d('root') }}" loop: "{{ docker__cron_jobs }}" when: - item.state | d("present") != "absent" - item.name | d() and item.job | d() - item.schedule | d() and item.cron_file | d()