Re-using "register" variable in multiple tasks

1,339 views
Skip to first unread message

Dimitri Yioulos

unread,
Feb 17, 2016, 12:07:40 PM2/17/16
to Ansible Project
I have the following play in a playbook:

- hosts: talk
  gather_facts: false

  tasks:
    - name: win update
      win_updates:
        category: ['SecurityUpdates','CriticalUpdates','Updates','Tools','DefinitionUpdates','UpdateRollups']
      register: reboot_hint

I'd like to re-use reboot_hint in additional plays in the playbook via "when", but the playbook ultimately fails after the first "when", as in:

- hosts: talk
  gather_facts: false

  tasks:
    - name: start Lyris services
      win_service: name={{ item }} state=started start_mode=auto
      with_items: ["ListManager", "LyrisAlert"]
      when: (lyris_hint.rc == 0) and (reboot_hint.reboot_required == true) # ie, the ListManager service is installed and reboot is required

- hosts: admin2
  gather_facts: false
  sudo: yes

  tasks:
    - nagios: action=downtime minutes=10 author="Dimitri Yioulos" service=host host={{item}}
      with_items:
         - talk
      when: reboot_hint.reboot_required == true
      delegate_to: admin2
    - nagios: action=downtime minutes=20 author="Dimitri Yioulos" service=all host={{item}}
      with_items:
         - talk
      when: reboot_hint.reboot_required == true
      delegate_to: admin2
      tags:
         - nagios_downtime

- hosts: talk
  gather_facts: false

  tasks:
    - name: reboot server
      raw: 'cmd /c shutdown /r /t 0'
      when: reboot_hint.reboot_required == true

This is another of my Ansible 101 questions; help making it work is appreciated!

Dimitri Yioulos

unread,
Feb 17, 2016, 4:55:40 PM2/17/16
to Ansible Project
I've found that my register variable only fails when applied to the play involving another host in my playbook.  After having poured over lots of posts from various other sources, I saw one that says, "use hostvars to access the register variables from a different host".  If, indeed, that's the solution, how do I use hostvars to achieve it?

Brian Coca

unread,
Feb 20, 2016, 12:52:19 PM2/20/16
to ansible...@googlegroups.com
hostvars[<otherhost][<varname]

--
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/38372e21-751d-4e01-a161-a0249ebb6ed9%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
----------
Brian Coca

Dimitri Yioulos

unread,
Feb 20, 2016, 2:16:52 PM2/20/16
to Ansible Project
Thanks, Brian.  OK, I'd like to use that in a "when" with the register variable "reboot_hint.reboot_required == true".  Will the following work?:

when: hostvars[myhost][reboot_hint.reboot_required == true]


On Wednesday, February 17, 2016 at 12:07:40 PM UTC-5, Dimitri Yioulos wrote:

Brian Coca

unread,
Feb 20, 2016, 2:48:25 PM2/20/16
to ansible...@googlegroups.com
no, you are conflating several things there


when: hostvars[myhost]['reboot_hint']['reboot_required']

^ that will execute when reboot_required is true.



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

For more options, visit https://groups.google.com/d/optout.



--
----------
Brian Coca

Dimitri Yioulos

unread,
Feb 22, 2016, 12:13:47 PM2/22/16
to Ansible Project
OK.  Here's what I'm trying to accomplish:

- hosts: all
  gather_facts: false

  tasks:
    - name: win update
      win_updates:
        category: ['SecurityUpdates','CriticalUpdates','Updates','Tools','DefinitionUpdates','UpdateRollups']
      register: reboot_hint

- hosts: admin2
  gather_facts: false
  sudo: yes

  tasks:
    - nagios: action=downtime minutes=20 author="Dimitri Yioulos" service=host host={{item}}
      with_items: "{{ groups['lyris'] }}"
      delegate_to: admin2
#      when: hostvars[myhost]['reboot_hint']['reboot_required']
      tags:
         - nagios_downtime
    - nagios: action=downtime minutes=20 author="Dimitri Yioulos" service=all host={{item}}
      with_items: "{{ groups['lyris'] }}"
      delegate_to: admin2
#      when: hostvars[myhost]['reboot_hint']['reboot_required']

Since "reboot_hint" is coming from the previous play, and since the host or group to run the playbook against is defined at the command line, as in: "ansible-playbook -l lyris myplaybook.yml -u myuser", what do I specify in the [myhost] section?  This must be a variable to accept the host name OR group name I specified at the command line, but I don't know what that would be.

Further, and maybe another post altogether, how do I make the substitution in "with_items: "{{ groups['lyris'] }}"" so that I don't have to change e.g. lyris is I'm specifying another host or group in running the playbook?

With thanks,

Dimitri


On Wednesday, February 17, 2016 at 12:07:40 PM UTC-5, Dimitri Yioulos wrote:
Reply all
Reply to author
Forward
0 new messages