Q: "Apply role raspberrypi on master node only for prod environment.
A: There are more options. 1) Either use the special variable inventory_file, or 2) use the special variable inventory_dir and create variable raspberrypi_skip_master by the inventory plugin ansible.builtin.constructed.
- Use the special variable inventory_file. For example, given the inventory files
shell> cat test
[master]
local-machine ansible_host=192.168.0.201 host_alias=local-machine
[node]
n1.com ansible_host=192.168.0.202 host_alias=n1
n2.com ansible_host=192.168.0.203 host_alias=n2
#n3.com ansible_host=192.168.0.204 host_alias=n3
[k3s_cluster:children]
master
node
shell> cat prod
[master]
m1.com ansible_host=192.168.0.201 host_alias=m1
[node]
n1.com ansible_host=192.168.0.202 host_alias=n1
n2.com ansible_host=192.168.0.203 host_alias=n2
#n3.com ansible_host=192.168.0.104 host_alias=n3
[k3s_cluster:children]
master
node
Run the playbook
shell> cat pb.yml
- hosts: k3s_cluster
roles:
- role: prereq
- role: download
- role: raspberrypi
when: not (inventory_hostname in groups.master and
inventory_file|basename != 'prod')
- The next option is to create a variable raspberrypi_skip_master by the inventory plugin ansible.builtin.constructed. See
shell> ansible-doc -t inventory ansible.builtin.constructed
Create the inventory prod
shell> tree prod
prod/
├── 01-hosts
└── 02-constructed.yml
0 directories, 2 files
shell> cat prod/01-hosts
[master]
m1.com ansible_host=192.168.0.201 host_alias=m1
[node]
n1.com ansible_host=192.168.0.202 host_alias=n1
n2.com ansible_host=192.168.0.203 host_alias=n2
#n3.com ansible_host=192.168.0.104 host_alias=n3
[k3s_cluster:children]
master
node
shell> cat prod/02-constructed.yml
plugin: ansible.builtin.constructed
strict: true
compose:
raspberrypi_skip_master: inventory_dir|basename != 'prod' and
'master' in group_names
Create the inventory test (the files 02-constructed.yml are identical)
shell> tree test/
test/
├── 01-hosts
└── 02-constructed.yml
0 directories, 2 files
shell> cat test/01-hosts
[master]
local-machine ansible_host=192.168.0.201 host_alias=local-machine
[node]
n1.com ansible_host=192.168.0.202 host_alias=n1
n2.com ansible_host=192.168.0.203 host_alias=n2
#n3.com ansible_host=192.168.0.204 host_alias=n3
[k3s_cluster:children]
master
node
shell> cat test/02-constructed.yml
plugin: ansible.builtin.constructed
strict: true
compose:
raspberrypi_skip_master: inventory_dir|basename != 'prod' and
'master' in group_names
Run the playbook
- hosts: k3s_cluster
roles:
- role: prereq
- role: download
- role: raspberrypi
when: not raspberrypi_skip_master
Given the trivial roles
shell> cat roles/*/tasks/main.yml
- debug:
msg: Role download
- debug:
msg: Role prereq
- debug:
msg: Role raspberrypi
The inventory prod will run all roles
shell> ansible-playbook -i prod pb.yml
PLAY [k3s_cluster] *******************************************************************************************************************
TASK [prereq : debug] ****************************************************************************************************************
ok: [m1.com] =>
msg: Role prereq
ok: [n1.com] =>
msg: Role prereq
ok: [n2.com] =>
msg: Role prereq
TASK [download : debug] **************************************************************************************************************
ok: [m1.com] =>
msg: Role download
ok: [n1.com] =>
msg: Role download
ok: [n2.com] =>
msg: Role download
TASK [raspberrypi : debug] ***********************************************************************************************************
ok: [m1.com] =>
msg: Role raspberrypi
ok: [n1.com] =>
msg: Role raspberrypi
ok: [n2.com] =>
msg: Role raspberrypi
PLAY RECAP ***************************************************************************************************************************
m1.com: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
n1.com: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
n2.com: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
The inventory test will skip the master group
shell> ansible-playbook -i test pb.yml
PLAY [k3s_cluster] *******************************************************************************************************************
TASK [prereq : debug] ****************************************************************************************************************
ok: [local-machine] =>
msg: Role prereq
ok: [n1.com] =>
msg: Role prereq
ok: [n2.com] =>
msg: Role prereq
TASK [download : debug] **************************************************************************************************************
ok: [local-machine] =>
msg: Role download
ok: [n1.com] =>
msg: Role download
ok: [n2.com] =>
msg: Role download
TASK [raspberrypi : debug] ***********************************************************************************************************
skipping: [local-machine]
ok: [n1.com] =>
msg: Role raspberrypi
ok: [n2.com] =>
msg: Role raspberrypi
PLAY RECAP ***************************************************************************************************************************
local-machine: ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
n1.com: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
n2.com: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0