Change a global var in a task

4,497 views
Skip to first unread message

Petros Moisiadis

unread,
May 15, 2014, 8:37:22 AM5/15/14
to ansible...@googlegroups.com
Is there any way to change a global variable (e.g. defined in the vars
section of a playbook) in a task for the current playbook run?

The use case is that I want to start with a boolean variable set to
True, and then run a command over a set of hosts. If a command at _any_
of the hosts returns a specific value in stdout, I want to set the
global boolean variable to False. This 'control' boolean variable will
be checked later to decide where to run (or not run) some other tasks,
so what I want actually is to create a conditional based on output from
multiple tasks or multiple runs in a task loop . I know about the
'set_fact' module, but this works on a per-host basis, not on global
vars, so it does not help. Perhaps, I need a 'set_var' or 'set_global'
module or something like that. I can think of some hacky ways to
workaround my case (e.g. use a temporary file instead of a global var),
but I think changing the value of a global var at will in the course of
a playbook run should be something that Ansible should allow us to do.
Am I missing something here?

James Cammarata

unread,
May 15, 2014, 9:17:30 AM5/15/14
to ansible...@googlegroups.com
Using set_fact should still achieve the same effect, you're using that variable over multiple plays with differing sets of hosts?



--
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/5374B502.1010302%40yahoo.gr.
For more options, visit https://groups.google.com/d/optout.

Petros Moisiadis

unread,
May 15, 2014, 10:27:11 AM5/15/14
to ansible...@googlegroups.com
On 05/15/14 16:17, James Cammarata wrote:
Using set_fact should still achieve the same effect, you're using that variable over multiple plays with differing sets of hosts?


Hmm... I see your point. It can be done by looping over the results of the previous task (which should run on all hosts) and run set_fact which will eventually set the same boolean value for all hosts. Thanks.

However, I am thinking if a 'set_global' would be useful in other, more complicated cases. Perhaps, this is why you ask about different sets of hosts. I have a feeling that in such cases the 'per-host' nature of set_fact could become inconvenient. Also, it comes natural to me that setting and changing a var globally (for all hosts) in a single pass would be something possible.


James Cammarata

unread,
May 15, 2014, 11:31:33 AM5/15/14
to ansible...@googlegroups.com
It might be useful, if you're interested in writing/contributing something like that we'd definitely consider it.

Thanks!


Petros Moisiadis

unread,
May 16, 2014, 8:39:21 AM5/16/14
to ansible...@googlegroups.com
On 05/15/14 17:27, 'Petros Moisiadis' via Ansible Project wrote:
On 05/15/14 16:17, James Cammarata wrote:
Using set_fact should still achieve the same effect, you're using that variable over multiple plays with differing sets of hosts?


Hmm... I see your point. It can be done by looping over the results of the previous task (which should run on all hosts) and run set_fact which will eventually set the same boolean value for all hosts. Thanks.

Unfortunately this does not work. set_fact does not override variables defined in "vars" section.
I used the following playbook to test it:

---
- hosts: localhost
  user: root
  vars:
    test: 1
  tasks:
    - name: set test
      set_fact: test=2

    - name: debug
      debug: msg="{{ test }}"


The output:

PLAY [localhost] **************************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [set test] **************************************************************
ok: [localhost] => {"ansible_facts": {"test": "2"}}

TASK: [debug] *****************************************************************
ok: [localhost] => {
    "msg": "1"
}

PLAY RECAP ********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0  


As you see, although set_fact sets test to "2",  the debug task prints "1". I am not sure if this is a bug or not, but it surely destroys a possible workaround.

Michael DeHaan

unread,
May 16, 2014, 10:56:02 AM5/16/14
to ansible...@googlegroups.com
This is due to a (intentional) change in 1.6, to make sure variables from hosts that are tampered with don't influence variables that describe whether or not a specific configuration is applied to them.

Thus we would need a "set_global" type action plugin (which would also need to be "bypass host loop" in order to be functional).  


--
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.

Michael DeHaan

unread,
May 16, 2014, 10:56:23 AM5/16/14
to ansible...@googlegroups.com
Minor correction, change was made in the 1.5 updates.
Reply all
Reply to author
Forward
0 new messages