Need a register value to persist

40 views
Skip to first unread message

Dimitri Yioulos

unread,
Aug 24, 2018, 8:44:40 AM8/24/18
to Ansible Project
Hello, all.

I've created the following playbook:

---
- hosts: Host1
  gather_facts: false
  tasks:
    - name: SQL Query Pending Import Jobs
      script: /etc/ansible/files/mssql_opm_getImportJobs.ps1
      register: import_job_count
    - debug:
        msg: "Number of running imports: {{ import_job_count.rc }}"
      tags:
        - test
- hosts: Host2
  gather_facts: false
  tasks:
    - name: Stop 1Point Import Services
      win_service: name={{ item }} state=stopped
      with_items: ["ImportRecipientsService","ImportRecipientsService_V2","OnepointImportService_High","OnepointImportService_Higher","OnepointImportService_Highest","OnepointImportServi
ce_Low"]
      when: import_job_count.stdout == "0"
      tags:
        - stop_opm_import_services

As I hope you can see, the idea is to use the register value from the first play to work in the second, which is aimed at another host.  I get "'import_job_count' is undefined" because the register value doesn't persist.  Is there any way to do this?

With thanks.

Kai Stian Olstad

unread,
Aug 24, 2018, 9:40:38 AM8/24/18
to ansible...@googlegroups.com
Register variables is per host not global, so you need to specify the host you want to retrieve it from.
To do this you need to use hostvars
hostvars['Host1'].import_job_count.stdout

--
Kai Stian Olstad


Dimitri Yioulos

unread,
Aug 24, 2018, 9:54:00 AM8/24/18
to Ansible Project
Beautiful, Kai, thanks!  I was just testing something like that, and it seems to work:

- hosts: Host2
  gather_facts: false
  tasks:
    - name: Stop 1Point Import Services
      win_service: name={{ item }} state=stopped
      with_items: ["ImportRecipientsService","ImportRecipientsService_V2","OnepointImportService_High","OnepointImportService_Higher","OnepointImportService_Highest","OnepointImportServi
ce_Low"]
      when: hostvars['Host1']['import_job_count'] is defined
      tags:
        - stop_opm_import_services

Does that seem right to you?

Dimitri Yioulos

unread,
Aug 24, 2018, 10:08:11 AM8/24/18
to Ansible Project
Actually, the value of " import_job_count" must be 0 (zero) in order for the play to proceed.  How do I do that?


On Friday, August 24, 2018 at 8:44:40 AM UTC-4, Dimitri Yioulos wrote:

Kai Stian Olstad

unread,
Aug 24, 2018, 10:08:33 AM8/24/18
to ansible...@googlegroups.com
On Friday, 24 August 2018 15.54.00 CEST Dimitri Yioulos wrote:
> Beautiful, Kai, thanks! I was just testing something like that, and it
> seems to work:
>
> - hosts: Host2
> gather_facts: false
> tasks:
> - name: Stop 1Point Import Services
> win_service: name={{ item }} state=stopped
> with_items:
> ["ImportRecipientsService","ImportRecipientsService_V2","OnepointImportService_High","OnepointImportService_Higher","OnepointImportService_Highest","OnepointImportServi
> ce_Low"]
> *when: hostvars['Host1']['import_job_count'] is defined*
> tags:
> - stop_opm_import_services
>
> Does that seem right to you?

Yes, but a registered variable will always be defined if the task is run or skipped.
So if you have the play Host1 before this, the variable import_job_count will be defined and the when will always be true.

And a tips to improve readability, you can write your with_items like this.

with_items:
- ImportRecipientsService
- ImportRecipientsService_V2
- OnepointImportService_High
- OnepointImportService_Higher
- OnepointImportService_Highest
- OnepointImportService_Low


--
Kai Stian Olstad


Dimitri Yioulos

unread,
Aug 24, 2018, 10:15:25 AM8/24/18
to Ansible Project
Thanks for the "with_items tip.  Please allow me to ask this again:  the value of " import_job_count" must be 0 (zero) in order for the play to proceed.  How do I do that?


On Friday, August 24, 2018 at 8:44:40 AM UTC-4, Dimitri Yioulos wrote:

Kai Stian Olstad

unread,
Aug 24, 2018, 10:20:43 AM8/24/18
to ansible...@googlegroups.com
On Friday, 24 August 2018 16.08.11 CEST Dimitri Yioulos wrote:
> Actually, the value of " import_job_count" must be 0 (zero) in order for
> the play to proceed. How do I do that?

You can choose one of these, they do the same but just written differently to just show you some options.

hostvars.Host1.import_job_count.stdout | int == 0
hostvars['Host1']['import_job_count']['stdout'] | int == 0
hostvars['Host1'].import_job_count.stdout == "0"


--
Kai Stian Olstad


Dimitri Yioulos

unread,
Aug 24, 2018, 10:42:24 AM8/24/18
to Ansible Project
Yup!  Did this: hostvars['Host1'].import_job_count.stdout == "0" , and it worked a treat.  I really appreciate your help!!!


On Friday, August 24, 2018 at 8:44:40 AM UTC-4, Dimitri Yioulos wrote:

Karl Auer

unread,
Aug 27, 2018, 7:40:07 PM8/27/18
to ansible...@googlegroups.com
I have a similar problem but the solution doesn't seem to apply.

I have multiple plays in a single playbook, but all the plays are on localhost. A fact set in one play does not seem to be available in other plays, because the hostname is "localhost" for all of them! Is there anyway to distinguish between such plays?

Here's a very simple proof of concept :-)

---
# Test variable access across multiple plays on local host
- hosts: localhost
  gather_facts: false

  tasks:
     - set_fact:
         local_var: "this"

- hosts: localhost
  gather_facts: false

  tasks:
     - set_fact:
         local_var: "this"

- hosts: localhost
  gather_facts: false

  tasks:

    - debug:
         var: hostvars['localhost']

The only facts output by the final debug statement are those in the current play. Is there any way to reference the variables in the earlier plays? For example, is there some way to provide an "alias" for a host in the "hosts" declaration?

Regards, K.




--
Kai Stian Olstad


--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8443452.OIqpQm9JUp%40x1.
For more options, visit https://groups.google.com/d/optout.



--
Karl Auer

Email  : ka...@2pisoftware.com
Website: 
http://2pisoftware.com

GPG/PGP : 958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816
Previous: F0AB 6C70 A49D 1927 6E05 81E7 AD95 268F 2AB6 40EA

Kai Stian Olstad

unread,
Aug 28, 2018, 3:22:07 AM8/28/18
to ansible...@googlegroups.com
On 28.08.2018 01:39, Karl Auer wrote:
> I have a similar problem but the solution doesn't seem to apply.
>
> I have multiple plays in a single playbook, but all the plays are on
> localhost. A fact set in one play does not seem to be available in
> other
> plays, because the hostname is "localhost" for all of them! Is there
> anyway
> to distinguish between such plays?

It works, you probably just overlooked the variable.


$ ansible --version | head -n 1
ansible 2.6.3


test.yml
---
- hosts: localhost
gather_facts: no
tasks:
- set_fact:
local_var: "play 1"

- hosts: localhost
gather_facts: no
tasks:
- debug:
var: hostvars['localhost'].local_var
- set_fact:
local_var: "play 2"

- hosts: localhost
gather_facts: no
tasks:
- debug:
var: hostvars['localhost'].local_var


Output:

$ ansible-playbook test.yml

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

TASK [set_fact]
************************************************************
ok: [localhost]

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

TASK [debug]
***************************************************************
ok: [localhost] => {
"hostvars['localhost'].local_var": "play 1"
}

TASK [set_fact]
************************************************************
ok: [localhost]

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

TASK [debug]
***************************************************************
ok: [localhost] => {
"hostvars['localhost'].local_var": "play 2"
}

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


> The only facts output by the final debug statement are those in the
> current
> play.

As you can see the variable is carried through to the next play.
But only the last one "stick" since the fist one is overwritten by the
second one.

> Is there any way to reference the variables in the earlier plays? For
> example, is there some way to provide an "alias" for a host in the
> "hosts"
> declaration?

You already create aliases in the inventory, the fist part is called
alias.
Alias here is myhost

myhost ansible_host=<any real ip or hostname>

To avoid the overwriting part you need unique variable names.


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages