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
, andinventory_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
, andinventory_hostname
.Conditionals based on
ansible_facts
Related Topics
Can Windows Containers Be Hosted on Linux
Difference Between Clock_Realtime and Clock_Monotonic
Printing an Integer as a String With At&T Syntax, With Linux System Calls Instead of Printf
How to Automatically Redirect Http to Https on Apache Servers
Encryption/Decryption Doesn't Work Well Between Two Different Openssl Versions
How to Install Gcc 4.9.2 on Rhel 7.4
How to Recursively Find All Files in Current and Subfolders Based on Wildcard Matching
Pseudo-Terminal Will Not Be Allocated Because Stdin Is Not a Terminal
What Does "&" At the End of a Linux Command Mean
Aborting a Shell Script If Any Command Returns a Non-Zero Value
What Is the Meaning of So_Reuseaddr (Setsockopt Option) - Linux
How to Merge Two Files Using Awk
How to Invoke a System Call Via Syscall or Sysenter in Inline Assembly
What Does the Number in Parentheses Shown After Unix Command Names in Manpages Mean