Score:0

Multiple containers in host over Ansible

au flag

Am wrote a playbook to create a container within a host machine. my idea is to create multiple containers per hosts. am trying to use the host.ini file to divide the host machines as a group and each container as an Ansible host within the group. Do you know how to structure the host file to use the Variable ansible_host to name the containers in the playbook used to create them.

my host file:

-----

[host.machine.1]
machine.1.container-1
machine.1.container-2
machine.1.container-3

[host.machine.2]
machine.2.container-1
machine.2.container-2
machine.2.container-3

[host.machine.3]
machine.3.container-1
machine.3.container-2
machine.3.container-3

my functional playbook:

---
- name: Create container
  hosts: host.machine.1:host.machine.2:host.machine.3
  vars:
    agent_name: "{{ container_name }}"

  tasks:
   - name: Docker pull 
     command: docker pull container.image:latest

   - name: Docker volume 
     command: docker volume create agent_{{ container_name }}

   - name: Docker run 
     command: docker run -d -it --privileged --name agent-{{ container_name }} -e AGENT_NAME="{{ container_name }}"   --network network1 --cpus=8 --memory=32g --ipc=host -e TZ=CET docker-registry/container.image:latest

Thank you

Score:0
in flag

Create a variable that lists the containers for every host

host_vars/host1.yml

containers:
  - name: agent1
    image: docker-registry/container.image:latest
  - name: agent2
    image: docker-registry/container.image:latest
  - name: agent3
    image: docker-registry/container.image:latest

The same for the other hosts

Then, in the playbook you can loop over that list:

hosts: host1,host2,host3
tasks:
  - name: Docker volume 
    command: "docker volume create agent_{{ item.name }}"
    loop: {{ containers }}
  - name: Docker run 
    command: "docker run -d -it --privileged --name agent-{{ item.name }} -e AGENT_NAME=\"{{ item.name }}\"   --network network1 --cpus=8 --memory=32g --ipc=host -e TZ=CET {{ item.image }}"
    loop: "{{ containers }}"

Or, using the proper modules:

hosts: host1,host2,host3
tasks:
  - name: Docker volume 
    docker_volume:
      name: "agent_{{ item.name }}"
    loop: {{ containers }}
  - name: Docker run 
    docker_container:
      name: "agent-{{ item.name }}"
      image: "{{ item.image }}"
      privileged: yes
      volumes:
        - "agent_{{ item.name }}"
    loop: "{{ containers }}"
Saptronic avatar
au flag
Thank you so much!!! Just re-wrote everything and it works like a charm
in flag
Great. Don't forget to accept the answer, otherwise your question will stay in the system as "unsolved" forever.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.