
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 2,925 Views
how to replicate bash for loop with two if condition in ansible playbook?
hi
can someone show me how to replicate below bash script into ansible-playbook.
#!/bin/sh
for NODE in server01 server02 server03 server04
do
grep -q $NODE file1
RETURN1=$?
grep -q $NODE file2
RETURN2=$?
if [ $RETURN1 -eq 1 ] && [ $RETURN2 -eq 1 ]
then
echo $NODE
fi
done


- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 2,916 Views
Hi,
Look, please, at example below:
- name: Multi if demo
hosts: all
gather_facts: false
vars:
file1: "server02 server03"
file2: "server03 server04"
tasks:
- name: Simple multi if demo
debug:
msg: "{{ inventory_hostname }}"
when:
- inventory_hostname in file1
- inventory_hostname in file2

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 2,901 Views
Is it possible to pass content of file1 and file2 as variable in file1 and
file2.
cat file1
-------
server02
server03
cat file2
---------
server03
server04
Thanks
Shrijan


- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 2,895 Views
Hi
It is a little bit more complicate:
- name: Multi if demo
hosts: all
gather_facts: false
vars:
file1s: "{{ lookup('file','file1') }}"
file2s: "{{ lookup('file','file2') }}"
tasks:
- name: Simple multi if demo
debug:
msg: "{{ inventory_hostname }}"
when:
- inventory_hostname in file1s
- inventory_hostname in file2s
Good luck.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 2,898 Views
Many Thanks Alex.
its works now.