I need to save fact into variable and use it afterwards

47 views
Skip to first unread message

boncalo mihai

unread,
Jul 28, 2022, 11:43:45 AM7/28/22
to ansible...@googlegroups.com
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

Dick Visser

unread,
Jul 28, 2022, 12:39:11 PM7/28/22
to ansible...@googlegroups.com
You can't stack moustaches 

Try:

 key: "{{ lookup('file', '/git/cephprep/files/' ~ admin_host ~ '/root/.ssh/id_rsa.pub') }}"



--
Sent from Gmail Mobile

boncalo mihai

unread,
Jul 28, 2022, 12:53:18 PM7/28/22
to ansible...@googlegroups.com
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') }}"}

--
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/CAF8BbLaxe7Le-2Be1waxmzt%2BkSG38aMaduC1_5VAEU%2BPhRXVsA%40mail.gmail.com.

boncalo mihai

unread,
Jul 28, 2022, 12:56:49 PM7/28/22
to ansible...@googlegroups.com
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)

On Thu, Jul 28, 2022 at 7:39 PM Dick Visser <dnmv...@gmail.com> wrote:
--

Dick Visser

unread,
Jul 28, 2022, 12:58:11 PM7/28/22
to ansible...@googlegroups.com
Just copy paste with the computer clipboard and not with eyeballs and fingers 



Walter Rowe

unread,
Aug 1, 2022, 8:18:26 AM8/1/22
to Ansible Project
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.

--
Walter Rowe, Chief
Infrastructure Service
Office of Information Systems Management
National Institute of Standards and Technology
US Dept of Commerce

boncalo mihai

unread,
Aug 1, 2022, 12:04:33 PM8/1/22
to ansible...@googlegroups.com
I actually worked, Thanks! :)

Reply all
Reply to author
Forward
0 new messages