Ansible set variable task?

15,724 views
Skip to first unread message

wi...@thames.id.au

unread,
Mar 14, 2013, 9:19:43 PM3/14/13
to ansible...@googlegroups.com
Let's say that I want to use a variable if it's passed to ansible-playbook (via -e) and derive it otherwise

I can use register to get the results of the derivation, but the actual information I need is then typically in the stdout property.

Is there a better way to do the following ?

---
...
tasks:
- name: derive latest 2.5 release
  local_action: shell ls /var/releases/v2.5-* | tail -1
  register: release
  when_unset: $version

- name: register version into release
  local_action: command echo $version
  register: release
  when_set: $version


I would prefer the second task to be something like this (rather than having to store the result of echoing the property!)

- name: store release.stdout into version
  local_action: variable name=version value=${release.stdout}
  when_unset: $version

And then just use $version after that.

Hope that makes sense - basically I just want to be able to set arbitrary variables during a playbook run rather than just before.
An alternative would be if register could be given an attribute that says 'only store stdout' (or stderr or any other property)

e.g.
register: property=stdout release
or
register: property=rc returncode

Will

Michael DeHaan

unread,
Mar 14, 2013, 9:56:16 PM3/14/13
to ansible...@googlegroups.com
>
> Is there a better way to do the following ?
>
> ---
> ...
> tasks:
> - name: derive latest 2.5 release
> local_action: shell ls /var/releases/v2.5-* | tail -1
> register: release
> when_unset: $version
>
> - name: register version into release
> local_action: command echo $version
> register: release
> when_set: $version
>
>
> I would prefer the second task to be something like this (rather than having
> to store the result of echoing the property!)
>
> - name: store release.stdout into version
> local_action: variable name=version value=${release.stdout}

I'm not really against some kind of action plugin called 'setvar' that
allows you to pass in python statements, but this seems like a
question about
whether hostvars has higher priority than extravars, and we've
explicitly added extra vars at maximum priority so they can override
defaults.

http://ansible.cc/docs/playbooks2.html#understanding-variable-precedence

What it seems to me is that instead of doing it the way you are doing
it, you could set the default in group_vars/all, and then let the
computed variable
override it there.

While, no, it is not "-e", it is somewhat equivalent.




--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Will Thames

unread,
Mar 14, 2013, 10:23:58 PM3/14/13
to ansible...@googlegroups.com


On 15 Mar 2013 11:56, "Michael DeHaan" <mic...@ansibleworks.com> wrote:
>
> >
> > Is there a better way to do the following ?
> >
> > ---
> > ...
> > tasks:
> > - name: derive latest 2.5 release
> >   local_action: shell ls /var/releases/v2.5-* | tail -1
> >   register: release
> >   when_unset: $version
> >
> > - name: register version into release
> >   local_action: command echo $version
> >   register: release
> >   when_set: $version
> >
> >
> > I would prefer the second task to be something like this (rather than having
> > to store the result of echoing the property!)
> >
> > - name: store release.stdout into version
> >   local_action: variable name=version value=${release.stdout}
>
> I'm not really against some kind of action plugin called 'setvar' that
> allows you to pass in python statements, but this seems like a
> question about
> whether hostvars has higher priority than extravars, and we've
> explicitly added extra vars at maximum priority so they can override
> defaults.

I think my question is more about how I can make the results of a registered variable be equivalent to other variables.

I'm happy with the precedence order, I just want to be able to derive a variable, with the same name, if it is not set.

> http://ansible.cc/docs/playbooks2.html#understanding-variable-precedence
>
> What it seems to me is that instead of doing it the way you are doing
> it, you could set the default in group_vars/all, and then let the
> computed variable
> override it there.

My problem is that the variables are being passed in by external deployment tool (deploy release x.y.z or just deploy latest) and so are passed in as extra vars.

Michael DeHaan

unread,
Mar 14, 2013, 10:44:14 PM3/14/13
to ansible...@googlegroups.com
Yep, extra vars have highest precedence, because most folks wanted
them as overrides over the defaults.

You could consider using the environment lookup $ENV(FOO) from group_vars/all

foo: $ENV(FOO)

https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/lookup_plugins/env.py

In which case versus -e it would just be

FOO=1 ansible-playbook ...

and that may fix things.
> --
> 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.
> For more options, visit https://groups.google.com/groups/opt_out.

Grant BlahaErath

unread,
Mar 21, 2014, 6:46:34 PM3/21/14
to ansible...@googlegroups.com
Old thread, but it popped up in my goog when I was looking for answers. Since I worked out two different solutions, I wanted to share them.

1) 

I needed to get a user account home directory and share it with a role.  Just using register meant I would have to use "variable.stdout" everywhere.  Instead I used the pre_tasks section of the playbook to retrieve the value and then pulled the key/value out on the role's variable declaration.  Here's the code I used.  I hope this helps out others.

  pre_tasks:
    - name: Get ansible_user home directory
      shell: 'getent passwd "{{ansible_ssh_user}}" | cut -d: -f6'
      register: ansible_home_result
  roles:
    - { role: boxprep_debian, ansible_home: '{{ansible_home_result.stdout}}', when: ansible_os_family == 'Debian' }
    - { role: boxprep_redhat, ansible_home: '{{ansible_home_result.stdout}}', when: ansible_os_family == 'RedHat' }

2)

Another approach that is far more elaborate but can solve anything is to create a python application that uses jinja2, and then process all your .yml files as if they were jinja2 templates.  The python application can solve any kind of variable instantiation challenge while leaving the .yml reusable for other scenarios.  Because jinja2 will only process the template variables it is told to, it will leave other {{variables}} for ansible to define. I use this approach to query an elaborate AWS cluster, and then preprocess the ansible scripts.

Michael DeHaan

unread,
Mar 21, 2014, 7:20:47 PM3/21/14
to ansible...@googlegroups.com
If you do "ansible hostname -m setup" in recent versions, you should see that environment variables are provided, in which case you can pull $HOME from there.

This of course would only work for the active user.





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

Grant BlahaErath

unread,
Mar 21, 2014, 7:52:18 PM3/21/14
to ansible...@googlegroups.com
Okay, my example kind of sucks and I'm not surprised that you noticed.   I had to implement this approach after finding that ansible_env.HOME did what it should, but not what I needed.

The playbooks run with "sudo: yes" because it was maddening to declare sudo state for each task.    That makes ansible_env.HOME evaluate to "/root".     This is how I solved that problem, but it became difficult to reuse code that had {{ansible_home_result.stdout}} everywhere.  

The pattern is generally applicable for converting any dictionary pair into a single value variable. 

I'm not sure how durable this solution will be since it depends on the pre_task executing and updating the global dictionary before the role is evaluated.   I feel like its depending on an implied behavior instead of a contract.  A "setvar" solution like you proposed would be superior because the behavior is explicit. 
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/wqiclLkqmGU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Michael DeHaan

unread,
Mar 21, 2014, 7:54:25 PM3/21/14
to ansible...@googlegroups.com
"I had to implement this approach after finding that ansible_env.HOME did what it should, but not what I needed."

Yep!

The HOME environment variable is not always predictable in sudo scenarios. 



Brian Coca

unread,
Mar 21, 2014, 7:57:21 PM3/21/14
to ansible...@googlegroups.com
shell: 'getent passwd <user> |cut -f 6 -d ":" '
register: userhome

Will Thames

unread,
Mar 21, 2014, 8:00:02 PM3/21/14
to ansible...@googlegroups.com
Surely the result is still in userhome.stdout

Since this thread was created, (and still a good while ago) set_fact was invented

set_fact: new_variable="{{result.stdout}}"



On 22 Mar 2014, at 09:57, Brian Coca <bria...@gmail.com> wrote:

shell: 'getent passwd <user> |cut -f 6 -d ":" '
register: userhome

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/wqiclLkqmGU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Brian Coca

unread,
Mar 22, 2014, 8:03:10 PM3/22/14
to ansible...@googlegroups.com

Grant BlahaErath

unread,
Mar 24, 2014, 6:39:19 PM3/24/14
to ansible...@googlegroups.com
Thanks Will.

I did not know about set_facts.  I am not surprised that I got the search term wrong. If we are lucky, this thread will bridge the two lexical choices in the goog.

I modified my snippet to look like this.


  pre_tasks:

    - name: Get ansible_user home directory
      shell: 'getent passwd "{{ansible_ssh_user}}" | cut -d: -f6'
      register: ansible_home_result

    - name: Set the fact for the other scripts to use
      set_fact: ansible_home='{{ansible_home_result.stdout}}'

I also submitted a pull for a doc change in the variable documentation since it was the first place i went looking for answers.
Reply all
Reply to author
Forward
0 new messages