Trouble with basic conditional

140 views
Skip to first unread message

Tom Vernon

unread,
Oct 8, 2015, 9:34:57 AM10/8/15
to Ansible Project
Hi there, slowly learning ansible here and having issues with a simple conditional.  I want to run something against all hosts in group "webservers" that end in "01".  The following doesnt seem to match any hosts (they get skipped), but looks right to me. Any ideas?

- hosts: webservers
  tasks
:
   
- name: check cluster status
      command
: 'echo $hostname'
     
when: inventory_hostname == "*01"



Thanks

Matt Martz

unread,
Oct 8, 2015, 9:47:53 AM10/8/15
to ansible...@googlegroups.com
There are a number of ways to do this.

1. Set "hosts" to "web*01" or whatever the pattern is to target only those servers in the play
2. In your when statement use 'inventory_hostname.endswith("01")'
3. inventory_hostname|search('01$')

Those are just a few of the many ways.
--
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/33d06b07-bb1a-4d31-92fd-8009d5bbb3b5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Tom Vernon

unread,
Oct 8, 2015, 10:14:32 AM10/8/15
to Ansible Project
Thanks Matt, inventory_hostname.endswith did the job perfectly.  Out of interest is there a way of performing an AND in the hosts declaration i.e member of webservers group AND hostname ends in 01.  This would prevent unnecessarily gathering facts on servers that we arent interested in. The documentation for patterns seems to suggest that you can perform OR but no mention of AND. Apologies for the basic questions, I'm currently reading up on this stuff! Thanks


On Thursday, 8 October 2015 14:47:53 UTC+1, Matt Martz wrote:
There are a number of ways to do this.

1. Set "hosts" to "web*01" or whatever the pattern is to target only those servers in the play
2. In your when statement use 'inventory_hostname.endswith("01")'
3. inventory_hostname|search('01$')

Those are just a few of the many ways.

On Thursday, October 8, 2015, Tom Vernon <tomve...@gmail.com> wrote:
Hi there, slowly learning ansible here and having issues with a simple conditional.  I want to run something against all hosts in group "webservers" that end in "01".  The following doesnt seem to match any hosts (they get skipped), but looks right to me. Any ideas?

- hosts: webservers
  tasks
:
   
- name: check cluster status
      command
: 'echo $hostname'
     
when: inventory_hostname == "*01"



Thanks

--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Markus Ellers

unread,
Oct 12, 2015, 3:01:17 PM10/12/15
to Ansible Project
In your case I would recommend reading and playing around with this


Without having tested it you should be able to achieve what you asked for using this:

hosts: *01,&webserver
Reply all
Reply to author
Forward
0 new messages