# I need to save fact into variable and use it afterwards

**URL:** <https://forum.ansible.com/t/i-need-to-save-fact-into-variable-and-use-it-afterwards/36917>\
**Category:** Ansible Project\
**Created:** [July 28, 2022, 3:43pm UTC](https://forum.ansible.com/t/i-need-to-save-fact-into-variable-and-use-it-afterwards/36917 "2022-07-28T15:43:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mboncalo](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@mboncalo](https://forum.ansible.com/u/mboncalo)\
**Post date:** [July 28, 2022, 3:43pm UTC](https://forum.ansible.com/t/i-need-to-save-fact-into-variable-and-use-it-afterwards/36917/1 "2022-07-28T15:43:04Z")

</div>

I have a task with run\_once:true , which must run only on the first host and I need to use that hostname into a lookup file , something like this:

- _name: Print facts_  
_debug:_  
_var: ansible\_nodename_  
_register: admin\_host_

_- name: Set authorized key took from file_  
_become: yes_  
_authorized\_key:_  
_user: root_  
_state: present_  
_key: “{{ lookup(‘file’, ‘/git/cephprep/files/{{ admin\_host }}/root/.ssh/id\_rsa.pub’) }}”_

Just this way it’s not working

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [July 28, 2022, 4:38pm UTC](https://forum.ansible.com/t/i-need-to-save-fact-into-variable-and-use-it-afterwards/36917/2 "2022-07-28T16:38:46Z")

</div>

You can’t stack moustaches

---

<div class="post-metadata">

**Author:** ![mboncalo](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@mboncalo](https://forum.ansible.com/u/mboncalo)\
**Post date:** [July 28, 2022, 4:52pm UTC](https://forum.ansible.com/t/i-need-to-save-fact-into-variable-and-use-it-afterwards/36917/3 "2022-07-28T16:52:54Z")

</div>

Not working unfortunately…:

TASK [cephprep : Print facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /git/cephprep/tasks/keyprep.yml:19  
ok: [ha01] =\> {  
“ansible\_facts["nodename"]”: “ha01”  
}

TASK [cephprep : Set authorized key took from file] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /git/cephprep/tasks/keyprep.yml:24  
fatal: [ha01]: FAILED! =\> {“msg”: “template error while templating string: unexpected ‘~’. String: {{ lookup(‘file’, ‘/git/cephprep/files/’ ~ ~ ‘/root/.ssh/id\_rsa.pub’) }}”}  
fatal: [ha02]: FAILED! =\> {“msg”: “template error while templating string: unexpected ‘~’. String: {{ lookup(‘file’, ‘/git/cephprep/files/’ ~ ~ ‘/root/.ssh/id\_rsa.pub’) }}”}  
fatal: [ha03]: FAILED! =\> {“msg”: “template error while templating string: unexpected ‘~’. String: {{ lookup(‘file’, ‘/git/cephprep/files/’ ~ ~ ‘/root/.ssh/id\_rsa.pub’) }}”}

---

<div class="post-metadata">

**Author:** ![mboncalo](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@mboncalo](https://forum.ansible.com/u/mboncalo)\
**Post date:** [July 28, 2022, 4:56pm UTC](https://forum.ansible.com/t/i-need-to-save-fact-into-variable-and-use-it-afterwards/36917/4 "2022-07-28T16:56:27Z")

</div>

Sorry, actually this is the result:

[WARNING]: Unable to find ‘/git/cephprep/files/{‘ansible\_facts[“nodename”]’: ‘ha01’, ‘failed’: False, ‘changed’: False}/root/.ssh/id\_rsa.pub’ in expected paths (use -vvvvv to see paths)

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [July 28, 2022, 4:57pm UTC](https://forum.ansible.com/t/i-need-to-save-fact-into-variable-and-use-it-afterwards/36917/5 "2022-07-28T16:57:43Z")

</div>

Just copy paste with the computer clipboard and not with eyeballs and fingers

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 1, 2022, 12:18pm UTC](https://forum.ansible.com/t/i-need-to-save-fact-into-variable-and-use-it-afterwards/36917/6 "2022-08-01T12:18:25Z")

</div>

Are all the tasks inside one playbook, or are different tasks in different jobs in an ansible tower workflow?

Use set\_fact for within-playbook, and set\_stats for creating artifacts sent back to ansible tower to be presented as extra\_vars to downstream jobs in a workflow.

For within-playbook:

- set\_fact:  
admin\_host: “{{ ansible\_nodename }}”

For downstream jobs in a workflow:

- set\_stats:  
data:  
admin\_host: “{{ ansible\_nodename }}”

If you need both you can do both. An artifact created with set\_stats in a playbook cannot be used later in that same playbook as a variable. You have to use set\_fact for that.

For loading your file within the playbook or downstream

- name: Set authorized key took from file

become: yes  
authorized\_key:  
user: root  
state: present  
key: “{{ lookup(‘file’, ‘/git/cephprep/files/ **’ + admin\_host + '** /root/.ssh/id\_rsa.pub’) }}”

NOTE that I broke up the file path and inserted the admin\_host with “+” which in Python (ansible runs on Python) will concatenate strings.

---

<div class="post-metadata">

**Author:** ![mboncalo](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@mboncalo](https://forum.ansible.com/u/mboncalo)\
**Post date:** [August 1, 2022, 4:04pm UTC](https://forum.ansible.com/t/i-need-to-save-fact-into-variable-and-use-it-afterwards/36917/7 "2022-08-01T16:04:08Z")

</div>

I actually worked, Thanks! 🙂
