-
-
Save rbq/886587980894e98b23d0eee2a1d84933 to your computer and use it in GitHub Desktop.
--- | |
- hosts: all | |
tasks: | |
- name: Install prerequisites for Docker repository | |
apt: | |
name: ['apt-transport-https', 'ca-certificates', 'curl', 'gnupg2', 'software-properties-common'] | |
update_cache: yes | |
- name: Add Docker GPG key | |
apt_key: | |
url: https://download.docker.com/linux/ubuntu/gpg | |
- name: Add Docker APT repository | |
apt_repository: | |
repo: deb [arch=amd64] https://download.docker.com/{{ ansible_system | lower }}/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable | |
- name: Install Docker CE | |
apt: | |
name: ['docker-ce', 'docker-ce-cli', 'containerd.io'] | |
update_cache: yes | |
- name: Install prerequisites for docker-compose | |
apt: | |
name: ['python3-pip', 'python3-setuptools', 'virtualenv'] | |
- name: Install docker-compose | |
pip: | |
name: docker-compose |
change stable channel to edge channel if you use ubuntu bionic
15: repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ansible_distribution_release}} edge
@webhive update cache attribute need at the last task.
Better version:
- hosts: all
tasks:
- name: Add Docker GPG key
apt_key: url=https://download.docker.com/linux/ubuntu/gpg
- name: Add Docker APT repository
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ansible_distribution_release}} stable
- name: Install list of packages
apt:
name: "{{ item }}"
state: installed
update_cache: yes
with_items:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
- docker-ce
@erolg Since [DEPRECATION WARNING]: State 'installed' is deprecated. Using state 'present' instead.. This feature will be removed in version 2.9. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
So it's better to use:
- hosts: all
tasks:
- name: Add Docker GPG key
apt_key: url=https://download.docker.com/linux/ubuntu/gpg
- name: Add Docker APT repository
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ansible_distribution_release}} stable
- name: Install list of packages
apt:
name: "{{ item }}"
state: present
update_cache: yes
with_items:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
- docker-ce
Just another heads up in case anyone uses this again, cus I did :D
[DEPRECATION WARNING]: Invoking "apt" only once while using a loop via squash_actions is deprecated. Instead of using a loop to supply multiple items and specifying `name: {{ item }}`, please use `name: [u'apt-transport-https', u'ca-certificates', u'curl', u'software-properties-common', u'docker-ce']` and remove the loop. This feature will be removed in version 2.11. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
Better:
- hosts: all
tasks:
- name: Add Docker GPG key
apt_key: url=https://download.docker.com/linux/ubuntu/gpg
- name: Add Docker APT repository
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ansible_distribution_release}} stable
- name: Install list of packages
apt:
name: ['apt-transport-https','ca-certificates','curl','software-properties-common','docker-ce']
state: present
update_cache: yes
Trying to install on my ubuntu machine and came across the below one. Officially the command has to be
+++++++++++++++++++++
sudo add-apt-repository
"deb [arch=amd64] https://download.docker.com/linux/ubuntu
$(lsb_release -cs)
stable"
+++++++++++++++++++++
According to above script it is as below.
" repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ansible_distribution_release}} stable "
But why this "{{ansible_distribution_release}}" , What is it exactly doing here? compared to the official one and yours.
@Yasinmohammed007 I just asked myself the same question:
{{ansible_distribution_release}} inserts the value of the variable ansible_distribution_release which should be equivalent to the output of $(lsb_release -cs)
To see what variables are available use
- debug: var=ansible_facts
in a playbook.
References:
https://superuser.com/questions/1010836/ansible-determine-operating-system
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variables-discovered-from-systems-facts
Some notes about this gist:
- apt-transport-https should be installed before add the Docker's repository: it's like a race condition, you cannot do the update & install if you have an https in sources files.
- The ansible_distribution_release should be enough if you run this in a Ubuntu installation, but adapt it to Debian is cost-less
- Install docker-compose maybe will be necessary in the future, so...
#!/usr/bin/env ansible-playbook - hosts: all tasks: - name: Add Docker GPG key apt_key: url=https://download.docker.com/linux/ubuntu/gpg - name: Install basic list of packages apt: name: ['apt-transport-https','ca-certificates','curl','gnupg2','software-properties-common'] state: present update_cache: yes - name: Add Docker APT repository apt_repository: repo: deb [arch=amd64] https://download.docker.com/linux/{{ansible_distribution|lower}} {{ansible_distribution_release}} stable - name: Install Docker packages apt: name: ['docker-ce','docker-ce-cli','containerd.io'] state: present - name: Install Docker-compose shell: curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- name: Install Docker-compose
become: yes
# shell: curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
get_url:
url: "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-{{ansible_system}}-{{ansible_architecture}}"
dest: /usr/local/bin/docker-compose
mode: +x
With docker-compose as service.
---
- name: Add Docker GPG key
apt_key: url=https://download.docker.com/linux/ubuntu/gpg
become: true
- name: Install basic list of packages
apt:
name: ['apt-transport-https','ca-certificates','curl','gnupg2','software-properties-common']
state: present
update_cache: yes
become: true
- name: Add Docker APT repository
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/{{ansible_distribution|lower}} {{ansible_distribution_release}} stable
become: true
- name: Install Docker packages
apt:
name: ['docker-ce','docker-ce-cli','containerd.io']
state: present
become: true
- name: Install Docker-compose
shell: curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose && ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
become: true
The following script is working for me in uBuntu 18.04
- name: Configure instance(s)
hosts: myinstance
remote_user: "{{ deploy_user_name }}"
become: true
tasks:
- name: Install aptitude using apt
apt: name=aptitude state=latest update_cache=yes force_apt_get=yes
- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Install required system packages
apt: name={{ item }} state=latest update_cache=yes
loop: [ 'apt-transport-https', 'ca-certificates', 'curl', 'software-properties-common', 'python3-pip', 'virtualenv', 'python3-setuptools']
- name: Add Docker APT repository
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/{{ansible_distribution|lower}} {{ansible_distribution_release}} stable
state: present
- name: Update apt and install docker-ce
apt: update_cache=yes name=docker-ce state=latest
- name: Update apt and install docker-ce-cli
apt: update_cache=yes name=docker-ce-cli state=latest
- name: Update apt and install containerd.io
apt: update_cache=yes name=containerd.io state=latest
- name: Install Docker Module for Python
pip:
name: docker
- name: Install Docker-compose
shell: curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose && ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
ignore_errors: yes
@rulai-jianfang seems like your gist is not rightly indented.
I guess you can just use the part under "tasks" instead of full code snippet. The whole playbook is tested working.
Also check the format with yamllint online, it passed the format/indent test
fatal: [172.31.90.128]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible_distribution' is undefined\n\nThe error appears to be in '/home/devops/ansible/installdocker.yml': line 35, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Add Docker APT repository\n ^ here\n"}
i am getting above error when i run this playbook. what i need to do?
Update ansible?
'ansible_distribution' is undefined, better post your yaml here
i am getting [
'ansible_distribution' is undefined
] when i run this playbook. what i need to do?
Check if you accidentially disabled the gathering of host facts (e.g. gather_facts: no
in your playbook).
Just stumble upon this gist via a simple ansible apt_repository docker
google search.
Thank you all for the snippets, especially for the ansible_distribution
and ansible_distribution_release
which I did not know.
I'd suggest a safety net for adding Docker’s GPG key :
- name: Add Docker official GPG key
become: yes
apt_key:
state: present
keyserver: 'https://download.docker.com/linux/{{ansible_distribution|lower}}/gpg'
id: 7EA0A9C3F273FCD8
- name: check Docker apt key has not been altered
become: yes
command: apt-key fingerprint 0EBFCD88
register: fingerprint_check
changed_when: False
failed_when: "'9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88' not in fingerprint_check.stdout"
Notes:
- the
apt_key.id: 7EA0A9C3F273FCD8
is retrieved from the subkey1 - the fingerprint
9DC8 [...] CD88
in the check task comes from the official documentation
1 Retrieved via gpg
through this one-liner:
See more…
curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
| gpg --keyid-format long 2>/dev/null \
| grep sub \
| cut -d'/' -f2 \
| cut -d' ' -f1
https://github.com/kurarrr/docker-ansible/blob/master/playbook.yml
This worked for me with ansible 2.9.9.
But I got the
{"msg": "The field 'remote_user' has an invalid value, which includes an undefined variable. The error was: 'deploy_user_name' is undefined"}
if I added remote_user: "{{ deploy_user_name }}"
.
How can I use this variable?
I got error "Unable to find any of pip2, pip to use. pip needs to be installed." on ubuntu bionic using command ansible-playbook --connection=local --inventory 127.0.0.1, --limit 127.0.0.1 docker.yaml. It only has python 3. It worked after I setup explicitly the pip version with the parameter "executable".
This works for me
$ ansible --version
ansible 2.10.2
---
- name: apt update
apt:
update_cache: yes
- name: Install prerequisites for Docker repository
apt:
pkg:
- apt-transport-https
- ca-certificates
- curl
- gnupg2
- software-properties-common
- name: add docker apt key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: add docker apt repo
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/{{ ansible_distribution|lower }} {{ ansible_distribution_release }} stable"
state: present
update_cache: yes
- name: install docker and it's dependencies
apt:
pkg:
- docker-ce
- docker-ce-cli
- containerd.io
state: present
- name: start and enable docker daemon
service:
name: docker
state: started
enabled: yes
- name: fetch docker-compose checksum
uri:
url: https://github.com/docker/compose/releases/download/{{ docker_compose_version }}/docker-compose-Linux-x86_64.sha256
return_content: yes
register: docker_compose_checksum
- name: install docker-compose
get_url:
url: https://github.com/docker/compose/releases/download/{{ docker_compose_version }}/docker-compose-Linux-x86_64
checksum: "sha256:{{ docker_compose_checksum.content.split(' ') | first }}"
dest: /usr/local/bin/docker-compose
mode: '0755'
you can use {{ ansible_machine }}
and {{ ansible_system }}
instead of above hardcoded x86_64
and Linux
@mstevanic If you have gather_facts: True
:)
Docker on Ubuntu 22.04 AWS AMI from Canonical, sticking as close to the official install page as possible, and leaning on work from @yum-dev above:
- name: install prerequisites for Docker repository
become: yes
ansible.builtin.apt:
pkg:
- ca-certificates
- curl
- gnupg2
- lsb-release
- name: add docker apt key
become: yes
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: add docker apt repo
become: yes
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable"
state: present
update_cache: yes
- name: install docker and its dependencies
become: yes
apt:
pkg:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
state: present
- name: start and enable docker daemon
become: yes
service:
name: docker
state: started
enabled: yes
- name: start and enable containerd daemon
become: yes
service:
name: containerd
state: started
enabled: yes
Does not work with ubuntu bionic yet. No docker package yet ready.