Unable to get value of variable from another Play in Ansible

28 views
Skip to first unread message

Mohtashim S

unread,
Aug 22, 2019, 3:26:00 PM8/22/19
to Ansible Project

I wish to obtain the value of "f_APP" variable of Play 2 into Play 3.


Below is my playbook which case be used as a testcase:


- name: "Play 1"
  hosts
: localhost
  tasks
:

   
- add_host: name={{ item }}
                        groups
=dest_nodes
                        ansible_user
={{ USER }}
     with_items
: "{{ Dest_IP.split(',') }}"


- name: "Play 2"
  hosts
: dest_nodes
  user
: "{{ USER }}"
  tasks
:

   
- set_fact:
       f_APP
: "Yellow"

- name: "Play 3"

  hosts
: localhost
  user: "{{ USER }}"

  vars
:
    dbfiledet
: "{{ hostvars['dest_nodes']['f_APP'] }}"

  tasks
:
   
- debug: msg="{{ dbfiledet.stdout }}"







I get the below error for my attempt:


playbook RUN command:


ansible-playbook variabletest.yml -e "USER=user1 Dest_IP=10.17.44.26,10.17.54.26"

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [Play 1]


TASK [Gathering Facts] ************************************************************************************************************************************** ok: [localhost]


TASK [add_host] ********************************************************************************************************************************************* changed: [localhost] => (item=10.17.44.26) changed: [localhost] => (item=10.17.54.26)


PLAY [Play 2]


TASK [Gathering Facts] ************************************************************************************************************************************** ok: [10.17.54.26] ok: [10.17.44.26]


TASK [set_fact] ********************************************************************************************************************************************* ok: [10.17.44.26] ok: [10.17.54.26]


PLAY [Play 3]


TASK [Gathering Facts] ************************************************************************************************************************************** ok: [localhost]


TASK [debug] ************************************************************************************************************************************************ fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: \"hostvars['dest_nodes']\" is undefined\n\nThe error appears to be in 'variabletest.yml': line 36, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug: msg=\"{{ dbfiledet.stdout }}\"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}


PLAY RECAP


10.17.44.26 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

10.17.54.26 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 localhost

: ok=3 changed=1 unreachable=0 failed=1 skipped=0

rescued=0 ignored=0


I'm on the latest version of ansible and python 2.7.5


Can someone suggest what is wrong and how can i get the value for the variable in Play 3 please ?

Zolvaring

unread,
Aug 22, 2019, 3:50:39 PM8/22/19
to ansible...@googlegroups.com
Unfortunately play variables do not persist in between plays by design in Ansible, generally I would try and group the tasks in a play together but you can write your vars to files on the local Host or remote node in play 2 and then read that file in play 3. One thing I can also suggest although I don't have a laptop to test currently is:

Play2:
Hosts: test
Tasks:
  set_fact:
    myvar: foo

Play3
Hosts: test
Tasks:
  set_fact:
    myvar: "{{ hostvars['test']['myvar'] }}"


^ that might work as I believe hostvars once defined will be carried throughout execution

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/36a39c4b-1e5f-4b21-b9c4-89c1f9941452%40googlegroups.com.

Mike Eggleston

unread,
Aug 22, 2019, 4:58:05 PM8/22/19
to ansible...@googlegroups.com
I’ve read variables don’t survive across “hosts” lines. 

Mike
--

Kai Stian Olstad

unread,
Aug 22, 2019, 5:23:47 PM8/22/19
to ansible...@googlegroups.com
On 22.08.2019 22:57, Mike Eggleston wrote:
> I’ve read variables don’t survive across “hosts” lines.

They do.
Variables is register to the host and is available in subsequent plays.

But to read variables from another host you always need to use hostvars
to access them.

--
Kai Stian Olstad

Vladimir Botka

unread,
Aug 22, 2019, 5:35:32 PM8/22/19
to Mohtashim S, ansible...@googlegroups.com
On Thu, 22 Aug 2019 12:26:00 -0700 (PDT)
Mohtashim S <mohta...@gmail.com> wrote:

> - name: "Play 1"
> [...]
> - add_host: name={{ item }}
> groups=dest_nodes
> ansible_user={{ USER }}
> with_items: "{{ Dest_IP.split(',') }}"
> [...]
> - name: "Play 3"
> [...]
> vars:
> dbfiledet: "{{ hostvars['dest_nodes']['f_APP'] }}"

"dest_nodes" is the group of hosts. "hostvars" requires a host as an index.
This is the reason of the error:

The error was: "hostvars['dest_nodes']" is undefined

Cheers,

-vlado
Reply all
Reply to author
Forward
0 new messages