|
2 | 2 | # Ansible Playbook: Install Docker |
3 | 3 | # This playbook installs Docker CE on Ubuntu/Debian systems |
4 | 4 | # |
| 5 | +# ⚠️ IMPORTANT: APT cache update logic has been moved to update-apt-cache.yml |
| 6 | +# Run the update-apt-cache.yml playbook first if you need to update the package cache. |
| 7 | +# This separation helps avoid CI issues with network-sensitive operations. |
| 8 | +# |
5 | 9 | # 🔗 RELATIONSHIP WITH INFRASTRUCTURE: |
6 | 10 | # 1. This playbook runs after VM provisioning (OpenTofu) and cloud-init completion |
7 | 11 | # 2. It prepares the VM for running containerized applications |
8 | 12 | # 3. Can be used as part of a larger deployment pipeline for Torrust applications |
| 13 | +# 4. Assumes APT cache is already updated (via update-apt-cache.yml or manually) |
9 | 14 |
|
10 | 15 | # Define which hosts this playbook will run on |
11 | 16 | - name: Install Docker |
|
22 | 27 |
|
23 | 28 | # List of tasks to execute in order |
24 | 29 | tasks: |
25 | | - # Task 0: Network diagnostics for CI troubleshooting |
26 | | - - name: Check network connectivity and DNS resolution |
27 | | - ansible.builtin.shell: | |
28 | | - echo "=== Network Diagnostics ===" |
29 | | - echo "Testing DNS resolution..." |
30 | | - nslookup archive.ubuntu.com || echo "DNS resolution failed" |
31 | | - echo "Testing connectivity to Ubuntu repositories..." |
32 | | - curl -I https://archive.ubuntu.com/ubuntu/ --connect-timeout 10 || echo "Ubuntu repo unreachable" |
33 | | - echo "Testing connectivity to Docker repositories..." |
34 | | - curl -I https://download.docker.com --connect-timeout 10 || echo "Docker repo unreachable" |
35 | | - echo "Current apt sources:" |
36 | | - cat /etc/apt/sources.list |
37 | | - register: network_diagnostics |
38 | | - changed_when: false |
39 | | - ignore_errors: true |
40 | | - |
41 | | - - name: Display network diagnostics |
42 | | - ansible.builtin.debug: |
43 | | - var: network_diagnostics.stdout_lines |
44 | | - when: network_diagnostics is defined |
| 30 | + # NOTE: APT cache update logic has been moved to update-apt-cache.yml |
| 31 | + # Run that playbook first if you need to update the package cache |
45 | 32 |
|
46 | | - # Task 1: Update package cache with retries and better error handling |
47 | | - - name: Update apt package cache |
48 | | - ansible.builtin.apt: |
49 | | - update_cache: true |
50 | | - cache_valid_time: 3600 # Cache valid for 1 hour |
51 | | - force_apt_get: true # Force using apt-get instead of aptitude for better CI compatibility |
52 | | - register: apt_update_result |
53 | | - retries: 3 |
54 | | - delay: 10 |
55 | | - until: apt_update_result is succeeded |
56 | | - when: ansible_os_family == "Debian" |
57 | | - ignore_errors: false # Fail if apt update ultimately fails |
58 | | - |
59 | | - # Task 1.1: Fallback apt update with different approach if needed |
60 | | - - name: Fallback apt update with apt-get directly |
61 | | - ansible.builtin.command: apt-get update |
62 | | - register: apt_get_update |
63 | | - retries: 2 |
64 | | - delay: 15 |
65 | | - until: apt_get_update.rc == 0 |
66 | | - when: |
67 | | - - ansible_os_family == "Debian" |
68 | | - - apt_update_result is failed |
69 | | - ignore_errors: false |
70 | | - |
71 | | - # Task 2: Install required packages for Docker repository with retries |
| 33 | + # Task 1: Install required packages for Docker repository with retries |
72 | 34 | - name: Install required packages for Docker repository |
73 | 35 | ansible.builtin.apt: |
74 | 36 | name: |
|
79 | 41 | - lsb-release |
80 | 42 | state: present |
81 | 43 | force_apt_get: true |
| 44 | + update_cache: false # Skip cache update - assume it was done separately |
82 | 45 | register: prereq_packages |
83 | 46 | retries: 3 |
84 | 47 | delay: 10 |
85 | 48 | until: prereq_packages is succeeded |
86 | 49 | when: ansible_os_family == "Debian" |
87 | 50 |
|
88 | | - # Task 3: Add Docker's official GPG key |
| 51 | + # Task 2: Add Docker's official GPG key |
89 | 52 | - name: Add Docker's official GPG key |
90 | 53 | ansible.builtin.get_url: |
91 | 54 | url: https://download.docker.com/linux/ubuntu/gpg |
92 | 55 | dest: /etc/apt/keyrings/docker.asc |
93 | 56 | mode: "0644" |
94 | 57 | when: ansible_os_family == "Debian" |
95 | 58 |
|
96 | | - # Task 4: Add Docker repository |
| 59 | + # Task 3: Add Docker repository |
97 | 60 | - name: Add Docker repository |
98 | 61 | ansible.builtin.apt_repository: |
99 | 62 | repo: "deb [arch={{ docker_arch }} signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" |
100 | 63 | state: present |
101 | 64 | filename: docker |
| 65 | + update_cache: true # Need to update cache after adding new repository |
102 | 66 | when: ansible_os_family == "Debian" |
103 | 67 |
|
104 | | - # Task 5: Update package cache after adding repository with retries |
105 | | - - name: Update apt package cache after adding Docker repository |
106 | | - ansible.builtin.apt: |
107 | | - update_cache: true |
108 | | - force_apt_get: true # Force using apt-get for better CI compatibility |
109 | | - register: apt_update_docker_repo |
110 | | - retries: 3 |
111 | | - delay: 10 |
112 | | - until: apt_update_docker_repo is succeeded |
113 | | - when: ansible_os_family == "Debian" |
114 | | - |
115 | | - # Task 6: Install Docker packages with retries |
| 68 | + # Task 4: Install Docker packages with retries |
116 | 69 | - name: Install Docker packages |
117 | 70 | ansible.builtin.apt: |
118 | 71 | name: |
|
122 | 75 | - docker-buildx-plugin |
123 | 76 | state: present |
124 | 77 | force_apt_get: true |
| 78 | + update_cache: false # Skip cache update - assume repository was updated separately |
125 | 79 | register: docker_install |
126 | 80 | retries: 3 |
127 | 81 | delay: 10 |
128 | 82 | until: docker_install is succeeded |
129 | 83 | when: ansible_os_family == "Debian" |
130 | 84 |
|
131 | | - # Task 7: Start and enable Docker service |
| 85 | + # Task 5: Start and enable Docker service |
132 | 86 | - name: Start and enable Docker service |
133 | 87 | ansible.builtin.systemd: |
134 | 88 | name: docker |
135 | 89 | state: started |
136 | 90 | enabled: true |
137 | 91 |
|
138 | | - # Task 8: Add user to docker group (for non-root Docker usage) |
| 92 | + # Task 6: Add user to docker group (for non-root Docker usage) |
139 | 93 | - name: Add user to docker group |
140 | 94 | ansible.builtin.user: |
141 | 95 | name: "{{ ansible_user }}" |
142 | 96 | groups: docker |
143 | 97 | append: true |
144 | 98 | register: user_added_to_docker_group |
145 | 99 |
|
146 | | - # Task 9: Verify Docker installation |
| 100 | + # Task 7: Verify Docker installation |
147 | 101 | - name: Verify Docker installation |
148 | 102 | ansible.builtin.command: docker --version |
149 | 103 | register: docker_version |
150 | 104 | changed_when: false |
151 | 105 |
|
152 | | - # Task 10: Display Docker version |
| 106 | + # Task 8: Display Docker version |
153 | 107 | - name: Display Docker version |
154 | 108 | ansible.builtin.debug: |
155 | 109 | msg: "{{ docker_version.stdout }}" |
156 | 110 |
|
157 | | - # Task 11: Test Docker with hello-world (optional verification) |
| 111 | + # Task 9: Test Docker with hello-world (optional verification) |
158 | 112 | - name: Test Docker with hello-world container |
159 | 113 | ansible.builtin.command: docker run --rm hello-world |
160 | 114 | register: docker_test |
161 | 115 | changed_when: false |
162 | 116 | ignore_errors: true # Don't fail the playbook if this test fails |
163 | 117 |
|
164 | | - # Task 12: Display Docker test result |
| 118 | + # Task 10: Display Docker test result |
165 | 119 | - name: Display Docker test result |
166 | 120 | ansible.builtin.debug: |
167 | 121 | msg: "{{ docker_test.stdout }}" |
168 | 122 | when: docker_test is succeeded |
169 | 123 |
|
170 | | - # Task 13: Warning about group membership |
| 124 | + # Task 11: Warning about group membership |
171 | 125 | - name: Important notice about Docker group membership |
172 | 126 | ansible.builtin.debug: |
173 | 127 | msg: | |
174 | 128 | ⚠️ IMPORTANT: User '{{ ansible_user }}' has been added to the 'docker' group. |
175 | 129 | You may need to log out and log back in (or restart the session) for this change to take effect. |
176 | 130 | Alternatively, you can use 'newgrp docker' to activate the group membership in the current session. |
| 131 | +
|
| 132 | + NOTE: If you need to update the APT cache, run the update-apt-cache.yml playbook first. |
177 | 133 | when: user_added_to_docker_group is changed |
178 | 134 |
|
179 | 135 | # Handlers section - tasks that run when triggered by other tasks |
|
0 commit comments