Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provisioning a docker container using ansible

I'm trying to use my existing docker playbooks to provision a docker container using ubuntu 18.04 for local development.

I'm having trouble running playbooks on the container as it does not come with python installed, so from my understanding ansible cannot run.

Is there a way i can install python on the container so that my playbooks can run?

NB I know that ansible-container exists but i would like to use my existing playbooks which use become_user and that doesn't work as stated on the build instructions

like image 266
jwtea Avatar asked Oct 29 '25 11:10

jwtea


1 Answers

You'll need to add your Docker container to the ansible inventory before you can target it in your playbooks. Something like this would work:

---
- hosts: localhost
  gather_facts: false

  tasks:
    - name: create container
      docker_container:
        name: ansible-test
        image: ubuntu:bionic
        command: bash
        detach: true
        interactive: true
        tty: true

    - name: add docker container to inventory
      add_host:
        name: ansible-test
        ansible_connection: docker

- hosts: ansible-test
  gather_facts: false
  tasks:

    - name: update apt cache
      delegate_to: ansible-test
      raw: apt -y update

    - name: install python
      delegate_to: ansible-test
      raw: apt -y install python-minimal

    - name: demonstrate that normal ansible modules work
      file:
        path: /etc/testdir
        state: directory

Note that while that works, it's not a terribly good model: you don't generally want to perform configuration tasks in your containers at run time; you want to configure your images at build time.

like image 83
larsks Avatar answered Nov 01 '25 01:11

larsks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!