0

I am using this:

Specify docker containers in /etc/ansible/hosts file

to run my ansible playbooks against a docker container.

But is there any way to avoid having a physical /etc/ansible/hosts file with the information about the container? E.g. run it from code where this information can be configured?

I looked at:

Running ansible-playbook using Python API

but when looking at the answers I see variables pointing to physical files, e.g.:

inventory = Inventory(loader=loader, sources='/home/slotlocker/hosts2')
playbook_path = '/home/slotlocker/ls.yml'

So not really sure why that is better than simply just calling from command line without using the Python ansible API.

u123
  • 15,603
  • 58
  • 186
  • 303
  • Check out this link: https://serversforhackers.com/c/running-ansible-2-programmatically. It uses a 'NamedTemporaryFile' to make a new inventory file. – felixc Dec 20 '17 at 19:22
  • Why do you want to run ansible playbooks against running docker containers? What are you trying to accomplish? – grizzthedj Dec 28 '17 at 14:33
  • 1
    To be able to test my play books faster, e.g revert the target machine/container faster – u123 Dec 29 '17 at 00:06

1 Answers1

0

May be install Ansible in Docker container and then run it locally inside the container. For example in the Dockerfile, include:

# Install Ansible
RUN pip install ansible
COPY ansible /tmp/ansible
# Run Ansible to configure the machine
RUN cd /tmp/ansible && ansible-playbook -i inventory/docker example_playbook.yml