Need a register value to persist

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.

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

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?

Actually, the value of " import_job_count" must be 0 (zero) in order for the play to proceed. How do I do that?

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

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?

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"

Yup! Did this: hostvars[‘Host1’].import_job_count.stdout == “0” , and it worked a treat. I really appreciate your help!!!

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 :slight_smile:

It works, you probably just overlooked the variable.

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

test.yml