Using ansible facts with when or only_if

1,665 views
Skip to first unread message

Dylan Silva

unread,
Dec 26, 2012, 8:03:33 PM12/26/12
to ansible...@googlegroups.com
Hello all!

I am trying to use ansible fact names (mainly group_names) with both when_string and only_if, but am hitting a road block.  It seems to me that if I run a playbook using a child group like foo:children but in my play book I call out when_string or only_if for bar1 and bar2 groups (children of foo:children) the plays do not get run against them.  Is this not an intended method for usage of when and only_if?  

Daniel Hokka Zakrisson

unread,
Dec 28, 2012, 10:14:12 AM12/28/12
to ansible...@googlegroups.com
group_names should work fine, we'd have to see your playbook and inventory to
really be able to say much more. Note though that if you are targeting
different hosts, you really should have multiple plays doing so, that is what
hosts: is for.

Daniel

Dylan Silva

unread,
Dec 28, 2012, 7:47:32 PM12/28/12
to ansible...@googlegroups.com
Thanks for the clarification.  Unfortunately, something is going wrong with my playbook then, and I think I know the problem.  

So the command I am running is 

ansible-playbook /opt/ansible-playbooks/application/management/setup.yml --extra-vars="hosts=application1" --verbose

My playbook has the following in it:

---
- name: Manage the nginx nginx.conf for appserver1
  template: src=../installApplicationApp/templates/nginx-conf.j2 dest=/etc/nginx/nginx.conf
  only_if: ${group_names} == 'appApp'

- name: Manage the nginx nginx.conf for dbserver1
  template: src=../installApplicationCollector/templates/nginx-conf.j2 dest=/etc/nginx/nginx.conf
  only_if: ${group_names} == 'appDb'

- name: Manage the server.xml for dbserver1
  template: src=../installApplicationCollector/template/server-xml.j2 dest=/usr/share/tomcat6/conf/server.xml
  only_if: ${group_names} == 'appDb'

- name: Manage the web-xml for dbserver1
  template: src=../installApplicationCollector/template/web-xml.j2 dest=/usr/share/tomcat6/conf/web.xml
  only_if: ${group_names} == 'appDb'

- name: Manage the my.cnf for dbserver1
  template: src=../installApplicationDB/my-cnf.j2 dest=/etc/my.cnf
  only_if: ${group_names} == 'appDb'

The hosts file has

[appApp]
appserver1

[appDb]
dbserver1

[application1:children]
appApp
appDb

I am thinking it will not run against the group_names specfied because using the application1 group just uses the hosts not the group child names. Correct?

--
Thanks,
Dylan Silva | Site Reliability Engineer | Office 415.442.3009 | Mobile 619.920.2654

AppDynamics
Monitor your Apps in Production. Now.

Watch our video | Download AppDynamics for free



Daniel

--



Brian Coca

unread,
Dec 28, 2012, 7:53:16 PM12/28/12
to ansible...@googlegroups.com

No, using a group includes child groups, I don't think you can pass hosts that way.

Try this in playbook
- hosts: $target

Then use -e 'target=application1'

Brian Coca

Michael DeHaan

unread,
Dec 28, 2012, 8:11:25 PM12/28/12
to ansible...@googlegroups.com
On Fri, Dec 28, 2012 at 7:47 PM, Dylan Silva <dsi...@appdynamics.com> wrote:
> Thanks for the clarification. Unfortunately, something is going wrong with
> my playbook then, and I think I know the problem.
>
> So the command I am running is
>
> ansible-playbook /opt/ansible-playbooks/application/management/setup.yml
> --extra-vars="hosts=application1" --verbose
>
> My playbook has the following in it:
>
> ---
> - name: Manage the nginx nginx.conf for appserver1
> template: src=../installApplicationApp/templates/nginx-conf.j2
> dest=/etc/nginx/nginx.conf
> only_if: ${group_names} == 'appApp'


For one, it's "only_if: 'appApp' in $group_names" if you *did* want to
do that, because group_names is an array, and not a string.

But, really, this is not the way ansible likes to do things.

It really loves this (as Daniel said):

hosts: appApp
tasks:
- ...

hosts: appDb
tasks:
- ...

Groups are already a hosts slicing mechanism (it's what they were made
for), so it makes it much simpler that way. only_if only exists for
the weird corner cases, if you find it in your playbooks, you should
really ask if it needs to be there.

If you do have to slice hosts up by arbitrary discovered criteria
(like OS type), the group_by module is also excellent for this.

--Michael

Dylan Silva

unread,
Dec 28, 2012, 8:17:57 PM12/28/12
to ansible...@googlegroups.com
Great, this makes sense to me now!  Thanks for the awesome suggestions!

--
Thanks,
Dylan Silva | Site Reliability Engineer | Office 415.442.3009 | Mobile 619.920.2654

AppDynamics
Monitor your Apps in Production. Now.

Watch our video | Download AppDynamics for free



--Michael

--



Reply all
Reply to author
Forward
0 new messages