using a variable from one task in the next

46 views
Skip to first unread message

Veera

unread,
Sep 21, 2023, 8:02:28 AM9/21/23
to Ansible Project
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?


Rowe, Walter P. (Fed)

unread,
Sep 21, 2023, 8:32:31 AM9/21/23
to ansible...@googlegroups.com
Works for me ..


 % ansible --version

 ansible [core 2.15.4]





 % cat foo.yml 

 ---

 - name: play 1

   hosts: all

   become: no

   gather_facts: no

   tasks:

     - set_fact:

         my_fact: "this is a fact"


 - name: play 2

   hosts: all

   become: no

   gather_facts: no

   tasks:

     - debug: var=my_fact





 % ansible-playbook -i localhost, foo.yml


 PLAY [play 1] **********************************************************************************************************


 TASK [set_fact] ********************************************************************************************************

 ok: [localhost]


 PLAY [play 2] **********************************************************************************************************


 TASK [debug] ***********************************************************************************************************

 ok: [localhost] => {

     "my_fact": "this is a fact"

 }


 PLAY RECAP *************************************************************************************************************

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




Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
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/e414a185-f743-4618-ab14-b4449dfecf6dn%40googlegroups.com.

Rowe, Walter P. (Fed)

unread,
Sep 21, 2023, 8:37:20 AM9/21/23
to ansible...@googlegroups.com
If you gather facts you can reference inventory_hostname_short vs running a shell command.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123
On Sep 21, 2023, at 8:02 AM, Veera <svee...@gmail.com> wrote:

Todd Lewis

unread,
Sep 21, 2023, 10:23:01 AM9/21/23
to ansible...@googlegroups.com, uto...@gmail.com
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.
--
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/e414a185-f743-4618-ab14-b4449dfecf6dn%40googlegroups.com.

-- 
Todd

Brian Coca

unread,
Sep 21, 2023, 10:23:22 AM9/21/23
to ansible...@googlegroups.com
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']
--
----------
Brian Coca

Veera

unread,
Sep 21, 2023, 10:34:38 AM9/21/23
to Ansible Project
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'] }}"

Rowe, Walter P. (Fed)

unread,
Sep 21, 2023, 10:55:02 AM9/21/23
to ansible...@googlegroups.com

Veera

unread,
Oct 31, 2023, 8:49:27 AM10/31/23
to Ansible Project

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 .

---
- name: play 1
  hosts: localhost
  gather_facts: no
  tasks:
    - name: task1 in play1
      shell: 'cat list_of_servers'
      register: task1_output
       
    - set_fact:
        server_list: "{{task1_output.stdout}}"

    - name: print the servers collected
      debug:
        msg: "{{server_list}}"

- name: play 2  
  hosts: localhost
  gather_facts: no
  tasks:
    - name: printing the smb.conf from the target servers
      debug:
        msg: "{{lookup('file', '/etc/samba/smb.conf')}}"
   
    - name: printing the /etc/issue from the target servers
      debug:
        msg: "{{lookup('file', '/etc/issue')}}"


Let's assume that after  multiple tasks execution  in play1, I got a list of servers. (list exactly ... in set_fact)
server1
server11
server111


I want to use those variables as an input to the second play(play2).
Logically .. the second play must be executed with  (- e server1)   and after all the tasks in play2  executed .. it should re-run play2 with (-e server 11) and the loop should continue until the  server111.

Is it possible to pass the variables which are the output from play1 as an input variable to play with multiple tasks ?
On looping ., is there a way to repeat the play2(with all its  tasks) for the each listed item of the variables .. server1.. server11.. server111)


$ ap Untitled-2.yml

PLAY [play 1] *************************************************************************************************************************

TASK [task1 in play1] *****************************************************************************************************************
changed: [localhost]

TASK [set_fact] ***********************************************************************************************************************
ok: [localhost]

TASK [print the servers collected] ****************************************************************************************************
ok: [localhost] =>
  msg: |-
    server1.example.com
    server11.example.com
    server111.example.com

PLAY [play 2] *************************************************************************************************************************

TASK [printing the smb.conf from the target servers] **********************************************************************************
fatal: [localhost]: FAILED! =>
  msg: The 'file' lookup had an issue accessing the file '/etc/samba/smb.conf'. file not found, use -vvvvv to see paths searched

PLAY RECAP ****************************************************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Brian Coca

unread,
Oct 31, 2023, 11:02:58 AM10/31/23
to ansible...@googlegroups.com
in play2 just use `hostvars['localhost']['server_list']`


--
----------
Brian Coca

Reply all
Reply to author
Forward
0 new messages