install docker on remote host using ansible

STEPS ON REMOTE HOST (WHERE DOCKER WILL BE INTALLED)

1 – do the login by ssh on the remote host

ssh -o StrictHostKeyChecking=no user@192.168.0.7

2 – create an user on remote host to be used by ansible having sudo as additional group

useradd -g users -G sudo user01 -s /bin/bash

echo -e "123456\n123456" | passwd user01

3 – set sudo users to execute commands without password

sed -i '/^%sudo.*ALL:ALL/s/^/#/; //a %sudo   ALL=(ALL) NOPASSWD: ALL' /etc/sudoers

STEPS ON ANSIBLE HOST

1 – Install ansible

apt install -y software-properties-common
apt-add-repository -y --update ppa:ansible/ansible
apt install -y ansible

2 – try to connect to localhost and verify a usable python on it*

ansible localhost, --connection=local -m ping

3 – configure remote host in the ansible hosts file

cat >> /etc/ansible/hosts <<'EOF'
srv01 ansible_host=192.168.0.7 ansible_user=user01 ansible_password=123456
EOF

4 – set config params in /etc/ansible/ansible.cfg file

sed -i '/gathering .*=/a gathering = explicit' /etc/ansible/ansible.cfg
sed -i '/host_key_checking .*=/a host_key_checking = false' /etc/ansible/ansible.cfg
sed -i '/ssh_args.*=/a ssh_args = -C -o ControlMaster=auto -o ControlPersist=300s' /etc/ansible/ansible.cfg
sed -i '/control_path .*=$/a control_path = /tmp/ansible-%%h-%%r' /etc/ansible/ansible.cfg
sed -i '/pipelining .*=/a pipelining = True' /etc/ansible/ansible.cfg
sed -i '/executable .*=/a executable = /bin/bash' /etc/ansible/ansible.cfg

5 – try to connect to remote host and verify a usable python on it

ansible srv01 -m ping

– use the command bellow to install python on remote machine if it not installed

ansible srv01 -b -m raw -a 'apt install -y python3'
ansible options used: -b will execute command as root
-m module name to execute -a arguments passed to module

6 – install docker and others packages on remote host***

cat > srv01-playbook.yml <<'EOF'
---
- hosts: srv01
  become: true
  become_user: root

  tasks:
    - name: install docker and other packages
      apt:
        name:
        - docker.io
        - curl
        state: latest
        update_cache: true
EOF

7 – run ansible-playbook command****

ansible-playbook srv01-playbook.yml

8 – Test if docker was installed*****

ansible srv01 -b -m shell -a 'docker -v'

Optionally you can add a new task to playbook file to get docker version

cat >> srv01-playbook.yml <<'EOF' && ansible-playbook srv01-playbook.yml

    - name: show docker version
      shell: docker -v | cut -d, -f1
      register: result
    - debug:
        msg: "{{ result.stdout }}"
EOF

Documentation of commands or modules used in this post

* ping module: https://docs.ansible.com/ansible/latest/modules/ping_module.html

** ansible.cfg: https://docs.ansible.com/ansible/2.4/intro_configuration.html

*** apt module: https://docs.ansible.com/ansible/latest/modules/apt_module.html

**** ansible-playbook: https://docs.ansible.com/ansible/latest/user_guide/playbooks.html

***** shell module: https://docs.ansible.com/ansible/latest/modules/shell_module.html

Leave a comment