Conditional register not working

165 views
Skip to first unread message

Manuel Quiñones

unread,
Aug 6, 2014, 10:02:10 AM8/6/14
to ansible...@googlegroups.com
Hi,

I'm trying to do a conditional register in a playbook:

  tasks:
    - shell: echo /opt/cache/www/
      register: www_data
      when: ansible_hostname | match("^xy-")

    - shell: echo /mnt/code/www/
      register: www_data
      when: ansible_hostname | match("^az-")

    - debug: var=www_data.stdout
      when: www_data.rc is defined and www_data.rc == 0

Only the last register "/mnt/code/www/" is shown by debug, and for the other I'm getting failures.
I would like to know what is the proper way to do this.

Thanks,

Michael DeHaan

unread,
Aug 6, 2014, 11:02:59 AM8/6/14
to ansible...@googlegroups.com
Can you share what a failure means in the above, any output you have, and what ansible version you are running?

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-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/258d8420-6865-474d-a847-f0da5ae4512d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Manuel Quiñones

unread,
Aug 7, 2014, 7:45:34 AM8/7/14
to ansible...@googlegroups.com
I found the answer in a previous post: https://groups.google.com/d/topic/ansible-project/NGXcbyQvz-Y/discussion

So register stores the variable even if the task is skipped, and this is the intended behaviour.

I was able to do what I want with one longer shell command, although I was expecting something more elegant:

  tasks:
   - name: test
     shell: if [ {{ ansible_hostname | match("^xy-") }} == True ]; then echo /opt/cache/www; else if [ {{ ansible_hostname | match("^az-") }} == True ]; then echo /mnt/code/www; fi; fi
     register: www_data

   - debug: var=www_data.stdout
     when: www_data.rc == 0

Manuel Quiñones

unread,
Aug 7, 2014, 8:34:14 AM8/7/14
to ansible...@googlegroups.com


On Wednesday, August 6, 2014 12:02:59 PM UTC-3, Michael DeHaan wrote:
Can you share what a failure means in the above, any output you have, and what ansible version you are running?

Thanks!

 Hi Michael,

ansible version is 1.6.3, and below is a sample output.  You will see that debug task is being executed only for the vars registered in the second shell task.  For failures I was wrong, sorry, there are all "failed=0" in this run.

Might be a good addition to the docs that register always store, even when tasks are skipped.  I can write a line and send a pull request if you want.
Also, maybe print a warning when the same register is being used more than once?

Thanks!

 ______________________
< PLAY [test register under conditionals] >
 ----------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


 _________________
< GATHERING FACTS >
 -----------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


ok: [az-123]
ok: [xy-01]
ok: [xy-02]
ok: [az-122]
ok: [xy-03]
 __________________________________
< TASK: shell echo /opt/cache/www/ >
 ----------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


skipping: [az-122]
changed: [xy-01]

cmd:
echo /opt/cache/www/

start:
2014-08-07 08:08:00.679567

end:
2014-08-07 08:08:00.682666

delta:
0:00:00.003099

stdout:
/opt/cache/www/

stderr:

changed: [xy-02]

cmd:
echo /opt/cache/www/

start:
2014-08-07 08:08:01.152222

end:
2014-08-07 08:08:01.155408

delta:
0:00:00.003186

stdout:
/opt/cache/www/

stderr:

skipping: [az-123]
changed: [xy-03]

cmd:
echo /opt/cache/www/

start:
2014-08-07 13:08:02.013062

end:
2014-08-07 13:08:02.015739

delta:
0:00:00.002677

stdout:
/opt/cache/www/

stderr:

 _________________________________
< TASK: shell echo /mnt/code/www/ >
 ---------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


skipping: [xy-03]
skipping: [xy-01]
skipping: [xy-02]
changed: [az-123]

cmd:
echo /mnt/code/www/

start:
2014-08-07 12:08:02.730487

end:
2014-08-07 12:08:02.733503

delta:
0:00:00.003016

stdout:
/mnt/code/www/

stderr:

changed: [az-122]

cmd:
echo /mnt/code/www/

start:
2014-08-07 12:08:03.669314

end:
2014-08-07 12:08:03.673185

delta:
0:00:00.003871

stdout:
/mnt/code/www/

stderr:

 _________________________________
< TASK: debug var=www_data.stdout >
 ---------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


ok: [az-123] => {
    "www_data.stdout": "/mnt/code/www/"
}
skipping: [xy-02]
skipping: [xy-01]
skipping: [xy-03]
ok: [az-122] => {
    "www_data.stdout": "/mnt/code/www/"
}
 ____________
< PLAY RECAP >
 ------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


az-123                     : ok=3    changed=1    unreachable=0    failed=0
xy-01                      : ok=2    changed=1    unreachable=0    failed=0
xy-02                      : ok=2    changed=1    unreachable=0    failed=0
xy-03                      : ok=2    changed=1    unreachable=0    failed=0
az-122                     : ok=3    changed=1    unreachable=0    failed=0



Michael DeHaan

unread,
Aug 7, 2014, 5:30:56 PM8/7/14
to ansible...@googlegroups.com
"Also, maybe print a warning when the same register is being used more than once?"

It's somewhat common for someone to use a repeated variable "result", so this isn't really a problem.

The above conditionals could be made easier using "set_fact" BTW:

- set_fact: path=default_path_goes_here
- set_fact: path=other_path
  when: ansible_hostname | match("^xy-")

+1 for cowsay installs.  Don't see it enough :)




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

Manuel Quiñones

unread,
Aug 7, 2014, 10:49:02 PM8/7/14
to ansible...@googlegroups.com
I'm glad to know about set_fact now.  Thanks Michael!
Reply all
Reply to author
Forward
0 new messages