Ansible dynamic looping

36 views
Skip to first unread message

Andrew Morgan

unread,
Feb 1, 2018, 7:01:51 PM2/1/18
to Ansible Project
Hello all,

I have a play I want to run wich deletes all hosts matching a certain criteria:

tasks:
 
- name: Delete unavailable nodes
    zabbix_host
:
     server_url
: http://zabbix.aa.net/zabbix
     login_user
: Admin
     login_password
: aq!
     host_groups
: Discovered hosts
     status
: disabled
     state
: absent
     host_name
: "{{ item }}"
    with_items
:
     
- ip-10-1-*-*.us-west-2.compute.internal
   


The above doesnt work, but if I list out the items
with_items:
     
- ip-10-1-65-27.us-west-2.compute.internal
     
- ip-10-1-65-28.us-west-2.compute.internal
     
- ip-10-1-65-37.us-west-2.compute.internal

then it works, but I want it to be dynamic and find all ips matching
     - ip-10-1-*-*.us-west-2.compute.internal

any help please?

Lê Mạnh Hùng

unread,
Feb 1, 2018, 7:37:10 PM2/1/18
to Ansible Project
Vào 07:01:51 UTC+7 Thứ Sáu, ngày 02 tháng 2 năm 2018, Andrew Morgan đã viết:

Lê Mạnh Hùng

unread,
Feb 1, 2018, 7:46:25 PM2/1/18
to Ansible Project
Vào 07:01:51 UTC+7 Thứ Sáu, ngày 02 tháng 2 năm 2018, Andrew Morgan đã viết:
Vào 07:01:51 UTC+7 Thứ Sáu, ngày 02 tháng 2 năm 2018, Andrew Morgan đã viết:
Vào 07:01:51 UTC+7 Thứ Sáu, ngày 02 tháng 2 năm 2018, Andrew Morgan đã viết:

Pshem Kowalczyk

unread,
Feb 1, 2018, 9:08:51 PM2/1/18
to ansible...@googlegroups.com
with_items requires a list to iterate over. I think one way of achieving your outcome is to create a group in the inventory and populate it with the hosts and run the playbook against that group.

kind regards
Pshem


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/dcd118b2-775b-44d3-baf5-713b9b0918be%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Morgan

unread,
Feb 2, 2018, 12:38:55 AM2/2/18
to Ansible Project
Thanks for your reply, but the issue with that is , the servers in aws get removed and created again each build( every 1hr) , so I would need a way to get the servers dynamically.Is there anyway to do that? i want to dynamically have a list, not create a list of servers in inventory.

Pshem Kowalczyk

unread,
Feb 2, 2018, 2:56:20 AM2/2/18
to ansible...@googlegroups.com
Hi,

If you use the dynamic inventory script (ec2.py) you can use tags to dynamically create groups. Have a look here: https://aws.amazon.com/blogs/apn/getting-started-with-ansible-and-dynamic-amazon-ec2-inventory-management/ 

kind regards
Pshem


Triển Lập Lâm

unread,
Feb 2, 2018, 3:02:12 AM2/2/18
to Ansible Project
Hi,



Vào 07:01:51 UTC+7 Thứ Sáu, ngày 02 tháng 2 năm 2018, Andrew Morgan đã viết:
Hello all,

Andrew Morgan

unread,
Feb 3, 2018, 5:48:22 PM2/3/18
to Ansible Project
thank you , with files worked, after piping list of ips

Mark Lam

unread,
Feb 3, 2018, 10:20:31 PM2/3/18
to Ansible Project
Hi Andrew

Can you show us how you did it?

Andrew Morgan

unread,
Feb 3, 2018, 11:24:21 PM2/3/18
to Ansible Project
I did it by 

host_name: "{ item }"
with_lines: cat "file"
Reply all
Reply to author
Forward
0 new messages