# How to obtain a value from one host in a play, and apply the value on another host.

**URL:** <https://forum.ansible.com/t/how-to-obtain-a-value-from-one-host-in-a-play-and-apply-the-value-on-another-host/17142>\
**Category:** Ansible Project\
**Created:** [December 24, 2014, 1:59pm UTC](https://forum.ansible.com/t/how-to-obtain-a-value-from-one-host-in-a-play-and-apply-the-value-on-another-host/17142 "2014-12-24T13:59:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frits\_Hoogland](https://avatars.discourse-cdn.com/v4/letter/f/9de0a6/32.png) [@Frits\_Hoogland](https://forum.ansible.com/u/Frits_Hoogland)\
**Post date:** [December 24, 2014, 1:59pm UTC](https://forum.ansible.com/t/how-to-obtain-a-value-from-one-host-in-a-play-and-apply-the-value-on-another-host/17142/1 "2014-12-24T13:59:04Z")

</div>

I am trying to orchestrate a database replication setup. This means I need to read configuration details from the master node, and apply this on the slave to facilitate the replication setup.

I try to read a configuration value, which works (master has ‘primary’ set to true):

- name: get audit file dest from database  
action: “shell export ORACLE\_SID={{ oracle\_sid }}; export ORACLE\_HOME={{ oracle\_home }}; printf "set head off\nselect value from v\$parameter where name = ‘audit\_file\_dest’;" | $ORACLE\_HOME/bin/sqlplus -S / as sysdba | grep -v ^$”  
when: primary  
register: audit\_file\_dest

Then I try to apply the value on the slave (slave has ‘standby’ set to true):

- name: create audit file dest on standby  
file: path={{ audit\_file\_dest.stdout }} state=directory  
when: standby

This is how the execution looks like:

TASK: [get audit file dest from database] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [192.168.39.142]  
changed: [192.168.39.139]

TASK: [create audit file dest on standby] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [192.168.39.139]  
fatal: [192.168.39.142] =\> One or more undefined variables: ‘dict object’ has no attribute ‘stdout’

When using the debug module, I see the problem:

TASK: [debug var=audit\_file\_dest] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [192.168.39.142] =\> {  
“audit\_file\_dest”: {  
“changed”: false,  
“skipped”: true  
}  
}  
ok: [192.168.39.139] =\> {  
“audit\_file\_dest”: {  
“changed”: true,  
“cmd”: “export ORACLE\_SID=test; export ORACLE\_HOME=/u01/app/oracle/product/11.2.0.4/dbhome\_1; printf "set head off\nselect value from v\$parameter where name = ‘audit\_file\_dest’;" | $ORACLE\_HOME/bin/sqlplus -S / as sysdba | grep -v ^$”,  
“delta”: “0:00:00.035030”,  
“end”: “2014-12-24 15:54:41.230344”,  
“invocation”: {  
“module\_args”: “export ORACLE\_SID=test; export ORACLE\_HOME=/u01/app/oracle/product/11.2.0.4/dbhome\_1; printf "set head off\nselect value from v\$parameter where name = ‘audit\_file\_dest’;" | $ORACLE\_HOME/bin/sqlplus -S / as sysdba | grep -v ^$”,  
“module\_name”: “shell”  
},  
“rc”: 0,  
“start”: “2014-12-24 15:54:41.195314”,  
“stderr”: “”,  
“stdout”: “/u01/app/oracle/admin/test/adump”,  
“stdout\_lines”: [  
“/u01/app/oracle/admin/test/adump”  
],  
“warnings”:   
}  
}

Because I try to use the variable on an host on which the variable was not filled, there is not stdout for it.  
How can I simply obtain a value on one host for use on another? In this case I would love to make the registered variable contents global.  
It can’t be that this is a unique situation? Or is there another way that I should do this?

Thanks

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 24, 2014, 2:04pm UTC](https://forum.ansible.com/t/how-to-obtain-a-value-from-one-host-in-a-play-and-apply-the-value-on-another-host/17142/2 "2014-12-24T14:04:31Z")

</div>

register stays on the host and gets reset in the loop to reflect the  
task was skipped, you might want to use a subsequent set\_fact and then  
access the variable thourgh hostvars[\<primary\_host\_name\>].

---

<div class="post-metadata">

**Author:** ![Frits\_Hoogland](https://avatars.discourse-cdn.com/v4/letter/f/9de0a6/32.png) [@Frits\_Hoogland](https://forum.ansible.com/u/Frits_Hoogland)\
**Post date:** [December 24, 2014, 2:14pm UTC](https://forum.ansible.com/t/how-to-obtain-a-value-from-one-host-in-a-play-and-apply-the-value-on-another-host/17142/3 "2014-12-24T14:14:08Z")

</div>

Thank you Brian!

Can you help me a bit with that?

Please mind I created a group of one called ‘primary’ and a group of one called ‘standby’ in the hosts file, not sure if that can be used.  
I used that to have specific per node variables declared.

It would really help in this specific case to have a value/status obtained on one node to configure the other or others. How is that normally done with Ansible?

Cheers,  
Frits

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 24, 2014, 2:21pm UTC](https://forum.ansible.com/t/how-to-obtain-a-value-from-one-host-in-a-play-and-apply-the-value-on-another-host/17142/4 "2014-12-24T14:21:48Z")

</div>

you can access the data from other nodes through hostvars:

hostvars[\<hostname\>][\<varname\>]

---

<div class="post-metadata">

**Author:** ![Frits\_Hoogland](https://avatars.discourse-cdn.com/v4/letter/f/9de0a6/32.png) [@Frits\_Hoogland](https://forum.ansible.com/u/Frits_Hoogland)\
**Post date:** [December 24, 2014, 2:33pm UTC](https://forum.ansible.com/t/how-to-obtain-a-value-from-one-host-in-a-play-and-apply-the-value-on-another-host/17142/5 "2014-12-24T14:33:44Z")

</div>

Thank you again Brian, for all your help.

As I’ve stated in the explanation of the problem, I’ve declared groups in the host file. It’s a group of one, but still a group.  
I solved this in the following way:

1. get the value

- name: get audit file dest from database  
action: “shell export ORACLE\_SID={{ oracle\_sid }}; export ORACLE\_HOME={{ oracle\_home }}; printf "set head off\nselect value from v\$parameter where name = ‘audit\_file\_dest’;" | $ORACLE\_HOME/bin/sqlplus -S / as sysdba | grep -v ^$”  
when: primary  
register: audit\_file\_dest

1. set the registered variable as fact

- name: register audit file dest  
set\_fact: audit\_file\_dest=“{{ audit\_file\_dest.stdout }}”  
when: primary

1. call the fact from another host via the group name and (mandatory, as far as I can see) host group number:

- name: create audit file dest on standby  
file: path={{ hostvars[groups[‘primary’][0]][‘audit\_file\_dest’] }} state=directory  
when: standby

Cheers,  
Frits
