简体   繁体   中英

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

I didn't realise you could just do this.

- name: Create container
  docker_container:
    name: docker-test
    image: ubuntu:18.04
    command: sleep 1d
- name: Install python on docker
  delegate_to: docker-test
  raw: apt -y update && apt install -y python-minimal

I think what you probably makes the most sense is install Python and your other tools in your Dockerfile when you create the image. Or you can choose a docker image with python already installed like using this as your FROM line in your Dockerfile:

FROM python

That way you won't have to run your Ansible task to install Python everytime you bring up a container, it will be built in from the time you build your image.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM