1
I have a hosts file that looks a bit different for prod and test and here they are:

# Contains the host mappings

[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

[all:vars]
ansible_python_interpreter=/usr/bin/python3

The above is for the test environment and the below is for prod environment:

# Contains the host mappings

[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

[all:vars]
ansible_python_interpreter=/usr/bin/python3

In my playbook, I have the following:

---

# K3s installation and set up
- name: "K3S SETUP: K3s download, install and setup"
  hosts: k3s_cluster
  gather_facts: yes
  become: yes
  roles:
    - {role: prereq, tags: ['host', 'k3s']}
    - {role: download, tags: ['host', 'k3s']}
    - {role: raspberrypi, tags: ['host', 'k3s']}

- hosts: master
  become: yes
  roles:
    - {role: k3s/master, tags: ['k3s', 'master']}

- hosts: node
  become: yes
  roles:
    - {role: k3s/node, tags: ['k3s', 'node']}

I want to now apply that role: raspberrypi on my master node only for my prod environment and I would like to skip that role for the master node when I run Ansible against the test hosts file. Any suggestions on how I could do this?

joesan
  • 13,963
  • 27
  • 95
  • 232

2 Answers2

3

You can add when conditions to your roles, so you could do something like this:

- name: "K3S SETUP: K3s download, install and setup"
  hosts: k3s_cluster
  gather_facts: true
  become: true
  roles:
    - role: prereq
      tags: ['host', 'k3s']
    - role: download
      tags: ['host', 'k3s']
    - role: raspberrypi
      tags: ['host', 'k3s']
      when: not skip_raspberrypi|default(false)

And then in your test inventory, set skip_raspberrypi=true for the master node:

hosts:
  master_node:
    skip_raspberrypi: true

If you're using an ini-style inventory:

master_node skip_raspberrypi=true
larsks
  • 277,717
  • 41
  • 399
  • 399
1

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.

  1. 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')

  1. 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
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63