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

--
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/66503670ffe785690444a0ebc34f0420%40olstad.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to