Conditional wild card usage

21 views
Skip to first unread message

sandeep athoti

unread,
Jul 7, 2020, 3:46:07 PM7/7/20
to Ansible Project

Is there any way I can use the wild cards or regex in when statements,


when: ansible_distribution_major_version|int == 6 or ansible_fqdn == "test[0-1][0-9].example.com"


I actually do not want to pass hostname of my server in vars section. Will this regex work with in conjunction with Ansible facts ? 


During my playbook execution time, it need to interpret my hostname of the server from fact ansible_fqdn.


Samole playbook,


hosts: localhost
tasks:
name: debug
debug:
msg: "passed"
when: "ansible_distribution_major_version|int == 6 or ansible_fqdn | regex_search('test[0-9][0-1]-[0-9][0-9].example.com')"

Vladimir Botka

unread,
Jul 7, 2020, 5:35:55 PM7/7/20
to sandeep athoti, ansible...@googlegroups.com
On Tue, 7 Jul 2020 12:46:07 -0700 (PDT)
sandeep athoti <sandeepa...@gmail.com> wrote:

> when: ansible_distribution_major_version|int == 6 or ansible_fqdn ==
> "test[0-1][0-9].example.com"

See "Testing strings"
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings

For example

- hosts: localhost
vars:
hostnames:
- test56.example.com
- testXY.example.com
my_regex: '^test(\d\d)\.example\.com'
tasks:
- debug:
msg: OK
loop: "{{ hostnames }}"
when:
item is match(my_regex)

gives

ok: [localhost] => (item=test56.example.com) =>
msg: OK
skipping: [localhost] => (item=testXY.example.com)


--
Vladimir Botka
Reply all
Reply to author
Forward
0 new messages