0

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.

2 Answers 2

0

I found your post while fighting with ERROR: Cannot uninstall requests 2.25.1, RECORD file not found. Hint: The package was installed by rpm. in Amazon Linux 2023 AMI 2023.0.20230517.1 x86_64 HVM kernel-6.1

You gave me an idea to try locally (ec2-user account, not root) and it seems that commenting out become: true line fixes the problem for me. But I'm using docker_container not docker compose.

0

I have also got the same issue while practicing Ansible lab from "Techworld with Nana" boot camp, Below are steps that resolved my issue:

My machine Platform: Amazon Linux (Inferred)

Did docker python module installation as ec2-user, instead of mixing it with other rpm installations as the root user

- name: Install docker python module
  hosts: aws
  become: yes
  become_user: ec2-user
  tasks:
    - name: Install docker python module #This is dependency with ansible docker_image module
      pip:
        name: docker

Full Ansible playbook in case if you are interested

---
- name: Install python3,docker and docker-compose
  hosts: aws
  become: yes
  tasks:
    - name: Make sure python3 and docker are installed
      vars: 
        ansible_python_interpreter: /usr/bin/python
      dnf:  #yum module as ofnow supports only python2 interpreter
        name: 
          - python3
          - docker
          - python3-pip
        update_cache: yes
        state: present
    - name: Install docker-compose
      get_url: 
        url: https://github.com/docker/compose/releases/download/1.27.4/docker-compose-Linux-{{lookup('pipe', 'uname -m')}} #using "lookup" from jinja template to interpret commands inside url
        dest: /usr/local/bin/docker-compose
        mode: +x
    - name: Ensure docker is running
      systemd:
        name: docker
        state: started


- name: Add user to docker group
  hosts: aws
  become: yes
  tasks:
    - name: Add ec2-user to docker group
      user:
        name: ec2-user
        groups: docker
        append: yes
    - name: Reconnect to server session
      meta: reset_connection  #To reset connection immediate after above task

- name: Install docker python module
  hosts: aws
  become: yes
  become_user: ec2-user
  tasks:
    - name: Install docker python module #This is dependency with ansible docker_image module
      pip:
        name: docker

- name: Test docker pull
  hosts: aws
  tasks:
    - name: pull redis
      # command: docker pull redis
      docker_image:
        name: redis
        source: pull

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.