Ansible conditional with variable

0

I have to build a cluster where certain packages should be installed on all hosts while other packages are installed on specific hosts only.

I wish to add a conditional to my yum install loop to select the target host based on my inventory groups (master, slave, all).

- name: Install Package
  when: inventory_hostname in groups[ {{item.host }} ]
  yum:
    name: "{{ item.name }}"
    state: present
  loop:
    - {name: 'package_1', host: 'master'}
    - {name: 'package_2', host: 'slave'}
    - {name: 'package_3', host: 'all'}

I understand that jinja2 templating is not an option, but I couldn't find what is the right way to do it. Thanks!

ansible conditional-statements loops
2021-11-23 07:42:47
1

3

You have misunderstood the warning. Conditionals like when are already a Jinja expression, so you should not use additional Jinja delimiters within them. This does not mean you cannot use variables, it means you don't need to do anything special to access them.

  when: inventory_hostname in groups[item.host]
2021-11-23 09:16:49

Much appreciate it! Worked like a charm!
János

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................