I have 3 applications hosted on three separate hosts and mentioned in the my.hosts
file like below:
[app1_webapp]
host3.mybank.com
[app2_webapp]
host5.mybank.com
[app3_webapp]
host8.mybank.com
My requirement is to run two types of raw
module commands.
First
raw
task which should run on all three hosts for all three applications, i.euptime
Second
raw
task isps
command that should run only on the respective host, i.eps -ef | grep app1
should only run onhost3.mybank.com
Below is how I call
ansible-playbook -i my.hosts main.yml -e appname=app1,app2,app3
and my main.yml
- hosts: "{{ product(appname.split(',')) | product(['webapp'])|map('flatten')|map('join', '_') }}"
user: user1
gather_facts: no
tasks:
- name: Check Running Process
raw: "ps -ef | grep {{ item }}"
register: psout
with_items: "{{ appname.split(',') }}"
- name: DUMP Running Process
debug:
msg: "{{ psout.stdout }}"
The above raw
fails as it tried ps
for each app on each hosts which is what I wish to skip (correct).
How do I put a when condition so that the ps
command for the respective app should run on the respective host only and not on all three hosts?