Ansible dynamic looping

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?
``

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:

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?

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,

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?

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

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

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.

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

Hi,

can you try “with_fileglob” ? Refer in http://docs.ansible.com/ansible/latest/playbooks_loops.html#id4

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

thank you , with files worked, after piping list of ips

Hi Andrew

Can you show us how you did it?

I did it by

host_name: “{ item }”
with_lines: cat “file”