How can I call list as a conditional for ansible_hostname

624 views
Skip to first unread message

rajthecomputerguy

unread,
Nov 4, 2019, 1:21:39 AM11/4/19
to Ansible Project
Hi team,

I am calling a list that contains multiple strings, How to put condition in when statement to match list of strings that matches ansible_hostname

below playbook does not work. please help

ansible_net_hostname: testalpcsr029

vars:

northregion: [ 'alp', alt' ]


playbook:- 
      name: Configure 
      ios_config:
        commands:
            - "{{ item }}"
      with_items: "{{ snmp_commands_ap }}"
      when:
          - "'csr' in ansible_net_hostname or 'asw' in ansible_net_hostname"
          - northregion in ansible_net_hostname


thanks

Sam Doran

unread,
Nov 4, 2019, 10:15:51 AM11/4/19
to ansible...@googlegroups.com
Use the search string test.

when: ansible_net_hostname is search('(csr|asw)')


You could store the regexp in a variable:

vars:
  regexp: 'csr|asw'

when: ansible_net_hostname is search(regexp)


Or you can construct the regexp from a list in the task.

- ios_config:
  ...
  when: ansible_net_hostname is search(regexp)
  vars:
    regexp: "{{ '|'.join(northregion) }}"

---

Sam

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1abf8f37-f2ec-430b-a3f3-00db3e7f4bc9%40googlegroups.com.

rajthecomputerguy

unread,
Dec 5, 2019, 11:17:53 AM12/5/19
to Ansible Project
Great Sam! 

---

Sam

To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages