Ansible - delegate_to container

Moki Lv6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
- name: Remove container
ignore_errors: yes
run_once: yes
delegate_to: localhost
community.docker.docker_container:
name: "my-container"
force_kill: yes
state: absent

- name: Create build container
run_once: yes
delegate_to: localhost
community.docker.docker_container:
image: debian:12
name: "my-container"
command: sleep 1d
auto_remove: true
register: container

- name: Add container to inventory
run_once: yes
delegate_to: localhost
add_host:
name: my-container
ansible_connection: docker
changed_when: false


- name: Prepare
run_once: yes
delegate_to: my-container
vars:
ansible_ssh_user: root
block:
- name: Bootstrap docker
ansible.builtin.raw: apt update && apt install -yqq python3

- name: Get facts
setup:
delegate_facts: true


- name: Do job
run_once: yes
delegate_to: my-container
vars:
ansible_ssh_user: root
block:
- name: install htop
ansible.builtin.apt:
name: htop
update_cache: yes


- name: Remove container
run_once: yes
delegate_to: localhost
community.docker.docker_container:
name: my-container
force_kill: yes
state: absent

On this page
Ansible - delegate_to container