Veera
(Sveemani)
September 21, 2023, 12:02pm
1
I have a 2 tasks in a playbook and from task1 I have a variable collected from the register output as below.
name: Playbook to collect the actual hostname
hosts: server1
become: yes
tasks:
name: collect the actual hostname
shell: ‘hostname’
register: short_name
name: collect the hostname for the next task
set_fact:
reg_name: “{{ short_name.stdout_lines[-1].split(‘.’)[1] }}”
name: Playbook to test the new hostname collections
hosts: 127.0.0.1
become: yes
tasks:
name: print the collected hostname
debug:
msg: {{reg_name }}
collecting the host’s shortname using set_fact is working . However the data stored using set_fact in reg_name is not carry to the next task.
Is there an option/module available to have the variable from one task to be made available to the next?
Walter_Rowe
(Walter Rowe)
September 21, 2023, 12:36pm
3
utoddl
(Todd Lewis)
September 21, 2023, 2:22pm
4
Your first play is targeting server1 , while your second play is targeting a different host.
If the reg_name is available in the second task, you would need to retrieve it relative to server1 :
**msg: "{{ hostvars['server1']['reg_name'] }}"**
You should look at and contrast the values of ansible_hostname , inventory_hostname , and ansible_fqdn .
system
(system)
September 21, 2023, 2:22pm
5
You are confusing scopes, `set_fact` is PER HOST, not a global. So you
defined the variable on server1 but are trying to read it from
127.0.0.1.
So to access in the 2nd play you need to go through hostvars:
{{hostvasr['server1']['reg_name']
Veera
(Sveemani)
September 21, 2023, 2:34pm
6
yes. Exactly …
I did set_fact on a remote_host and try to re-call the value of the variable in the localhost.
So to access in the 2nd play you need to go through hostvars:
{{hostvasr[‘server1’][‘reg_name’]
Do you mean I have to use like below?
name: print the collected hostname
debug:
msg: “{{hostvars[‘server1’][‘reg_name’] }}”
Brian / Todd … good eyes!
Walter
Veera
(Sveemani)
October 31, 2023, 12:49pm
8
Hi ,
I am re-opening this thread as I get a new question on above the existing one.
Sorry for that…(not lazy to open another)
Is there a way to use the output of play1 , which is a list(of variables) to be passed variable to the second play2
The playbook for illustration only .
system
(system)
October 31, 2023, 3:02pm
9
in play2 just use `hostvars['localhost']['server_list']`