Skip to content

Instantly share code, notes, and snippets.

@nbigot
Last active August 6, 2024 06:20
Show Gist options
  • Save nbigot/3f066b1bb1aef2334788bbbe1b431bfd to your computer and use it in GitHub Desktop.
Save nbigot/3f066b1bb1aef2334788bbbe1b431bfd to your computer and use it in GitHub Desktop.
Ansible playbook AWS - install docker (2021)

Execute those commands from the machine that run ansible.

Configure Ansible

$ sudo vi /etc/ansible/ansible.cfg
# uncomment this to disable SSH key host checking
host_key_checking = False

Edit hosts file

$ sudo vi /etc/ansible/hosts
[awsec2instances]
10.2.0.39
10.2.0.217
10.2.0.208

Run ansible

$ chmod 600 my-key.pem
$ ansible-playbook --private-key my-key.pem -u ec2-user ansible_playbook-aws-install-docker-2021.yml
# example of /etc/ansible/hosts file
# sudo vi /etc/ansible/hosts
[awsec2instances]
10.2.0.39
10.2.0.217
10.2.0.208
# Ansible playbook AWS - install docker (2021)
---
- name: "AWS - Install docker"
hosts: awsec2instances
become: yes
tasks:
- name: Update all packages
yum:
name: '*'
state: latest
update_only: yes
- name: Ensure a list of yum packages are installed
yum:
name: "{{ packages }}"
state: latest
update_cache: yes
vars:
packages:
- python-pip
- yum-utils
- device-mapper-persistent-data
- lvm2
- amazon-linux-extras
- name: Add extras repository
shell: yum-config-manager --enable extras
- name: Enable Some packages from amazon-linux-extras packages
shell: "amazon-linux-extras enable python3.8 ansible2 docker"
- name: clean yum metadata cache
command: yum clean metadata
args:
warn: false
- name: Ensure a list of yum packages are installed
yum:
name: "{{ packages }}"
state: latest
update_cache: yes
vars:
packages:
- python3.8
- ansible
- docker
- name: Enable Docker CE service at startup
service:
name: docker
state: started
enabled: yes
- name: Upgrade pip3
shell: "python3.8 -m pip install pip --upgrade"
- name: Ensure Python pip packages are installed
pip:
name: "{{ packages }}"
executable: /usr/local/bin/pip3.8
vars:
packages:
- boto
- boto3
- docker-compose
sudo yum install -y amazon-linux-extras
sudo amazon-linux-extras enable python3.8 ansible2 docker
sudo yum clean metadata
sudo yum install -y python3.8 ansible docker
sudo python3.8 -m pip install pip --upgrade
sudo /usr/local/bin/pip3.8 install boto boto3 docker-compose
sudo systemctl enable docker.service
sudo systemctl start docker.service
@abhishekmandloi95
Copy link

Hi

Thanks for the yaml. Could you please tell how will it check if the docker isn't already installed on remote host?

@nbigot
Copy link
Author

nbigot commented Aug 19, 2021

Hi

Thanks for the yaml. Could you please tell how will it check if the docker isn't already installed on remote host?

Hi,

    - name: Ensure a list of yum packages are installed
      yum:
        name: "{{ packages }}"
        state: latest
        update_cache: yes
      vars:
        packages:
        - python3.8
        - ansible
        - docker

see state: latest

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/yum_module.html#parameter-state

latest will update the specified package if it's not of the latest available version.
present and installed will simply ensure that a desired package is installed.

It will also install the package if it's not already installed.

If the package is already installed and it's version is the latest one then ansible will do nothing.

@abhishekmandloi95
Copy link

Thank you for the explanation.

@Alex8Efremov
Copy link

Thank you for an excellent playbook!

@KostaWT
Copy link

KostaWT commented Apr 7, 2022

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment