Run an Ansible Task Only When the Variable Contains a Specific String

Run an Ansible task only when the hostname contains a string

Below should do the trick:

- name: Install PHP Modules
command: <command>
with_dict: php_modules
when: "'batch' in inventory_hostname"

Note you'll have a couple of skipped hosts during playbook run.

inventory_hostname is one of Ansible's "magic" variables:

Additionally, inventory_hostname is the name of the hostname as
configured in Ansible’s inventory host file. This can be useful for
when you don’t want to rely on the discovered hostname
ansible_hostname or for other mysterious reasons. If you have a long
FQDN, inventory_hostname_short also contains the part up to the first
period, without the rest of the domain.

Source: Ansible Docs - Magic variables and how to access information about other hosts

In Ansible, how to check if a variable contains a string?

You just need to quote development in your task, because right now it's interpreting it as a variable name and attempting to parse it, rather than interpreting it as a string property name.

- name: Set NodeJS version for development branch
set_fact:
nodejs_version: "{{ nodejs_versions['development'] }}"
when: '"development" in branch_name'

How to use ansible when condition when string contains '@'

For the first cited issue, yaml doesn't behave like python or shell which automatically concatenate string literals together

You'll want:

     stat:
path: "{{ user_dir }}/{{ keytab_name }}"

And the second error is because yaml believes the leading " is the start of a YAML literal, but in fact it's the start of a Jinja2 literal, thus:

  when: '"{{ kerberos_username }}@{{ emr_kerberos_realm }}" not in user_princs.stdout'

Or you can use any of the scalar folding syntaxes, if you prefer that:

  when: >-
"{{ kerberos_username }}@{{ emr_kerberos_realm }}"
not in user_princs.stdout

Ansible - How to run tasks on specific host only and not on all 'inventory_hosts'?

As already mentioned in the comments the proper approach would be to group the inventory by function ...


Given an inventory of

[app1_test]
test1.example.com

[app2_test]
test2.example.com

a small test playbook

---
- hosts: app1_test,app2_test
become: false
gather_facts: false

vars:

APPNAME: "app1,app2"

tasks:

- name: Check on
debug:
msg: "{{ item }} on {{ group_names }}"
when: group_names is search(item)
with_items: "{{ APPNAME.split(',') }}"

is resulting into the requested output of

TASK [Check on] *************************
ok: [test1.example.com] => (item=app1) =>
msg: app1 on [u'app1_test']
ok: [test2.example.com] => (item=app2) =>
msg: app2 on [u'app2_test']

How do I put a when condition so that ... the respective app should run on the respective host only

This means, you need to check (only) if the "app" is contained within the respective group name(s). One host can belong to multiple groups.

Further Q&A

  • How do you search for substring in Ansible group_names?
  • Run task only if host does not belong to a group?

Documentation

  • Information about Ansible: magic variables

    The most commonly used magic variables are hostvars, groups, group_names, and inventory_hostname.

  • Conditionals based on ansible_facts

Ansible condition when string not matching

Try:

when: nginxVersion.stdout != 'nginx version: nginx/1.8.0'

or

when: '"nginx version: nginx/1.8.0" not in nginxVersion.stdout'

Ansible - How to run tasks on specific host only and not on all 'inventory_hosts'?

As already mentioned in the comments the proper approach would be to group the inventory by function ...


Given an inventory of

[app1_test]
test1.example.com

[app2_test]
test2.example.com

a small test playbook

---
- hosts: app1_test,app2_test
become: false
gather_facts: false

vars:

APPNAME: "app1,app2"

tasks:

- name: Check on
debug:
msg: "{{ item }} on {{ group_names }}"
when: group_names is search(item)
with_items: "{{ APPNAME.split(',') }}"

is resulting into the requested output of

TASK [Check on] *************************
ok: [test1.example.com] => (item=app1) =>
msg: app1 on [u'app1_test']
ok: [test2.example.com] => (item=app2) =>
msg: app2 on [u'app2_test']

How do I put a when condition so that ... the respective app should run on the respective host only

This means, you need to check (only) if the "app" is contained within the respective group name(s). One host can belong to multiple groups.

Further Q&A

  • How do you search for substring in Ansible group_names?
  • Run task only if host does not belong to a group?

Documentation

  • Information about Ansible: magic variables

    The most commonly used magic variables are hostvars, groups, group_names, and inventory_hostname.

  • Conditionals based on ansible_facts



Related Topics



Leave a reply



Submit