ホスト マシン内にコンテナーを作成するためのプレイブックを作成しました。私のアイデアは、ホストごとに複数のコンテナーを作成することです。host.ini ファイルを使用して、ホスト マシンをグループとして分割し、各コンテナーをグループ内の Ansible ホストとして分割しようとしています。コンテナーの作成に使用するプレイブックで変数 ansible_host を使用してコンテナーに名前を付けるために、ホスト ファイルを構成する方法をご存知ですか。
私のホストファイル:
-----
[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
私の機能的なプレイブック:
---
- 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
ありがとう
答え1
各ホストのコンテナをリストする変数を作成する
ホスト変数/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
他のホストも同様です
そして、プレイブックではそのリストをループする:
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 }}"
または、適切なモジュール:
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 }}"