Problem with variables

33 views
Skip to first unread message

Eugene Romero

unread,
Aug 13, 2015, 3:42:09 PM8/13/15
to Ansible Project
Hi all,

The ansible docs, here, claim that you can assign a variable to a host inline in a playbook. Then, here, it says you can use "when" paired with a variable to run certain roles on certain hosts. Why then, does this not work?

---
- hosts: "{{ log_srv }}"
  vars: 
    type: syslog
  roles:
    - syslog-server

- hosts: all
  roles:
    - { role: syslog-client, when type == "syslog" }

The first play runs fine, only on the server/s defined in the "log_srv" flag. But the second play runs on all servers, instead of just on the one/s from the first play. What am I doing wrong?

Thanks in advance.

Brian Coca

unread,
Aug 13, 2015, 3:52:46 PM8/13/15
to Ansible Project
you are missing a colon:

- { role: syslog-client, when: type == "syslog" }


fyi, setting the var that way sets it always for all hosts, so that
when statement is always true.
--
Brian Coca

Eugene Romero

unread,
Aug 13, 2015, 4:21:20 PM8/13/15
to Ansible Project
Hey Brian, thanks for the prompt reply.

Ok, so the colon got that running. So, what I actually mean to do with this playbook is the opposite, run the second play on all servers EXCEPT the one from the first play. What is the ansible equivalent of the boolean "not"? I've tried both when: type =! "syslog" and when: type not "syslog" and neither works. How would I implement this?

Thanks again!

Brian Coca

unread,
Aug 13, 2015, 4:22:50 PM8/13/15
to Ansible Project
!= , also note that 'type' will NOT be visible in the 2nd play as you
defined it as a play level variable, you want to use set_fact in this
case.



--
Brian Coca

Eugene Romero

unread,
Aug 13, 2015, 4:29:22 PM8/13/15
to Ansible Project
Of course its !=. My brain is not working today. I need a vacation... 
Thanks for the tip on set_fact, I'll look into it a bit more.
Thanks again for all the help!
Reply all
Reply to author
Forward
0 new messages