I'm trying to run an ansible playbook that installs docker and docker-compose and runs a compose file on a ec2 t2.micro.
To use the docker modules in ansible, I need to have docker and docker-compose python modules installed in the ec2.
Here is my playbook:
- name: Update yum repo, install docker, pip, docker-compose & pip and python modules
hosts: all
become: true
tasks:
- name: update yum repo and cache
yum:
name: '*'
state: latest
update_cache: true
- name: install docker and pip
yum:
name:
- docker
- pip
- name: download and install docker-compose
get_url:
url: https://github.com/docker/compose/releases/latest/download/docker-compose-Linux-{{lookup('pipe','uname -m')}}
dest: /usr/local/bin/docker-compose
mode: +x
- name: start docker daemon
systemd:
name: docker
state: started
########################################################### this is the trouble part
- name: Install docker and docker-compose python module
pip:
name:
- docker
- docker-compose
############################################################
the rest of the playbook adds ec2-user to the docker group, resets the ec2 connection, copies docker-compose file to the server and executes it, which all works perfectly fine.
Take a look if required
#play2
- name: Reset connection
hosts: all
become: true
tasks:
- name: add ec2-user to docker group
user:
name: ec2-user
groups: docker
append: yes
- name: reconnect to server session
meta: reset_connection
#play3
- name: start docker containers
hosts: all
vars_files:
- docker-vars.yml
tasks:
- name: copy docker-compose file
copy: # copy files from controller to remote node
src: /home/ansible-project/docker-compose.yaml
dest: /home/ec2-user/docker-compose.yaml
- name: login to docker private repo
docker_login:
registry_url: https://index.docker.io/v1
username: "{{user_name}}"
password: "{{docker_passwd}}"
- name: execute docker-compose and start container
docker_compose:
project_src: /home/ec2-user
state: present # equivalent of docker-compose up
My host file is simple with a single group and 1 server-ip configured with ansible_user and ssh private key for the login. The docker-vars.yaml has only my docker username and passwd.
I have tried to run the playbook by installing python3 specifically and select the "interpreter_python= /usr/bin/python3" in the ansible.cfg file and also using the default python installed i,e usr/bin/python3.9, but the playbook runs into an error saying:
"Found existing installation: requests 2.25.1\n\n:stderr:ERROR: Cannot uninstall requests 2.25.1 Hint: The package was installed by rpm."
But if I manually log in to the ec2 and do
$ pip install docker
$ pip install docker-compose
the modules are successfully installed. And if I comment out the highlighted code and run the playbook it executes successfully.
I need to run this completely in single click, as I have to integrate it into the terraform file later, and I cannot perform that single step this manually.