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.



On Fri, Aug 24, 2018 at 11:40 PM, Kai Stian Olstad <
[email protected]> wrote:

> On Friday, 24 August 2018 14.44.40 CEST Dimitri Yioulos wrote:
> > 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?
>
> 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
>
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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  : [email protected]
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

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CA%2B%2BT08RYgVFBhjJH_8XZCEjDM-UkxzN9NJeohmzCLA9Y%2BLgbSg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to