Reputation: 1499
I'm a new user of Ansible and am trying to see how I can perform an automation task using Ansible. I would like to connect to several servers and cat a file to get a list of items (list_2). I then want to check if that list of items is in another list (list_1). I would like then print a table showing which items from list_2 are in list_1? Is there a way to accomplish this via Ansible without pushing a script?
The below gathers both lists and puts them into a register but I am unsure how to evaluate to see if the items in list_2 are contained in list_1.
list_1 = 'red, green, yellow, blue, orange, purple'
list_2 = 'green, blue, purple'
hosts: myhost
tasks:
- name: get list 1
shell: "cat /dir/list_1"
register: list_1
tasks:
- name: get list 2
shell: "cat /dir/list_2"
register: list_2
Desired Output:
list_1 list2inlist1
red No
green Yes
blue Yes
orange No
purple Yes
Upvotes: 1
Views: 219
Reputation: 1579
Here's something that will get close to your desired output
- name: print intersection
debug:
msg: "{{ item }}"
with_items: "{{ list_1.stdout.split(', ') }}"
when: item in list_2.stdout.split(', ')
If you really want the same syntax as your desired output I think a template would be the easiest way.
in your tasks:
- name: print intersection template
template:
src: intersection.j2
dest: /some_dir/intersection.txt
in intersection.j2:
list1 list2inlist1
{% for item in list_1.stdout.split(', ') %}
{% if item in list_2.stdout.split(', ') %}
{{ item }} YES
{% else %}
{{ item }} NO
{% endif %}
{% endfor %}
Upvotes: 1