Docker Community Forums

Share and learn in the Docker community.

How to install ansible-galaxy roles in docker?

I tried to install it in Dockerfile

FROM python:3.7-slim-stretch

RUN apt-get update && apt-get install -y \
    ssh \
    sshpass \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

COPY requirements.txt /tmp
RUN pip install -r requirements.txt && rm requirements.txt

RUN groupadd --gid 1001 ansible && useradd --uid 1001 --gid 1001 -m ansible
COPY ansible.cfg /home/ansible/.ansible.cfg
RUN mkdir -p /home/ansible/.ansible/roles/ && chown -R ansible:ansible /home/ansible/

COPY requirements.yml /home/ansible/requirements.yml
RUN ansible-galaxy install -r /home/ansible/requirements.yml

WORKDIR /playbook
USER ansible




- src: geerlingguy.php


roles_path = ~/.ansible/roles

I start docker with docker-compose. The config file is

version: "3.8"
      context: ./ansible
      dockerfile: Dockerfile
    image: ansible
    container_name: ansible
    hostname: ansible
    #command: ['/bin/bash', '-c', 'ansible-galaxy install -r ./playbook/requirements.yml']
    tty: true
      - ./playbook:/playbook
      - ./roles:/home/ansible/.ansible/roles/

In the build step I can see ansible-galaxy installed roles that defined in the requirements.yml file.

But when I login into the container ansible, I can’t find the roles at all.

If I remove the two lines from Dockerfile, add command way in the docker-compose.yml file, after build, I can’t login into the container. There was an error

Error response from daemon: Container 11111111111111111111111111111111111111111111111111111111111 is not running

I’m confused why they weren’t been installed in the image if set the install action in Dockerfile?

Since you mounted your local folder into the container it overrides the content of the folder inside the container where you had the roles.

Mount your roles to an other folder like /opt/roles and add that folder to the roles_path

roles_path = ~/.ansible/roles:/opt/roles

Thank you very much. That was the reason.

But for ansible, maybe even I define 2 paths, it will use the first one only. The roles weren’t been installed to an other folder.