how to use a variable from one task to another task

136 views
Skip to first unread message

Charles Daniel

unread,
Jul 8, 2021, 2:52:06 AM7/8/21
to Ansible Project

Team, 

I would like to use the variable i got from one ansible task to another task where hosts are different.

Please help me use the variable to use it on another task!!

---

- name: Get the primary router tunnel ip address 
  hosts: primary
  gather_facts: false
  connection: local
  tags:
    - "get_sec_tunnel_ip"
  tasks:
  - name: Obtain primary tunnel interface ip address
    ios_command:
      commands:
        - "show interface tunnel1 | sec Internet address"
    register: output
  - debug:
      msg: "{{ output.stdout}}"
  - set_fact:
      intf: |
          - {{output.stdout | map('regex_search','(([0-9]+(.)[0-9]+(.)[0-9]+(.)[0-9]+))') | list }}
    register: sec_tunnel_ip
  - debug: 
       msg={{ sec_tunnel_ip }}

- name: Display router tunnel ip address from this task
  hosts: secondary
  gather_facts: false
  connection: local
  tags:
    - "use_output"
  tasks:
  - name: Display primary tunnel interface ip address
  - debug:
      msg: "{{ sec_tunnel_ip }}"

Charles Daniel

unread,
Jul 8, 2021, 2:53:35 AM7/8/21
to Ansible Project
When i try to use/inoke on another task, i get error as sec_tunnel_ip is undefined. 

Dick Visser

unread,
Jul 8, 2021, 3:21:58 AM7/8/21
to ansible...@googlegroups.com
Those variables are scoped to a host, so they're not available to other hosts. 
A common trick is to assign them to a dummy host and access them that way. 

--
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/1daafd88-3c14-456a-bb94-e6f7f3ba8748n%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Dick Visser

unread,
Jul 8, 2021, 3:24:02 AM7/8/21
to ansible...@googlegroups.com

Charles Daniel

unread,
Jul 8, 2021, 3:33:21 AM7/8/21
to ansible...@googlegroups.com
Thanks a lot, Dick Visser, I just wanted to access it, even with a dummy hostname is ok.  let me try that.

Thanks,
Charles Daniel


Charles Daniel

unread,
Jul 8, 2021, 3:56:51 AM7/8/21
to ansible...@googlegroups.com
One more help needed, When I try to do regex to get the ip output, it comes along with the unicode characters such as - [u'
how to eliminate it while grepping it through regex_search itself.

Actual output: "- [u'172.16.121.6']\n"

I want it to be(expected): 172.16.121.6


  - name: Obtain secondary tunnel interface ip address

    ios_command:
      commands:
        - "show interface tunnel1 | sec Internet address"
    register: output
  - debug:
      msg: "{{ output.stdout}}"
  - set_fact:
      intf: |
          - {{output.stdout | map('regex_search','(([0-9]+(.)[0-9]+(.)[0-9]+(.)[0-9]+))') | list }}
    register: sec_tunnel_ip
  - debug:
       msg={{ sec_tunnel_ip }}


Output:
********

TASK [debug] ******************************************************************************************************
ok: [172.16.12.1] => {
    "msg": {
        "ansible_facts": {
            "intf": "- [u'172.16.121.6']\n"
        },
        "changed": false,
        "failed": false
    }
}

Thanks,
Charles Daniel

Dick Visser

unread,
Jul 8, 2021, 6:04:59 AM7/8/21
to ansible...@googlegroups.com
You're sending only the output of the debug task with the regex_search
applied to the output.stdout variable.
We need to see the output.stdout variable before you mangled it.
> --
> 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/CAPnV%3Dh%2B7LxPqEnbrezq3Maofib9Ak8UmLUsK10twE0DeqXKBPw%40mail.gmail.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Charles Daniel

unread,
Jul 8, 2021, 6:18:39 AM7/8/21
to ansible...@googlegroups.com
Hi Dick, Below is the complete output of the playbook,

[root@tower1 temp_play]#  ansible-playbook testplay2.yml -i /etc/ansible/temp_play/hosts

PLAY [Get the secondary router tunnel ip address] *****************************************************************

TASK [Obtain secondary tunnel interface ip address] ***************************************************************
ok: [172.16.12.1]


TASK [debug] ******************************************************************************************************
ok: [172.16.12.1] => {
    "msg": [
        "Internet address is 172.16.121.6/32"
    ]
}

TASK [set_fact] ***************************************************************************************************
ok: [172.16.12.1]


TASK [debug] ******************************************************************************************************
ok: [172.16.12.1] => {
    "msg": {
        "ansible_facts": {
            "intf": "- [u'172.16.121.6']\n"
        },
        "changed": false,
        "failed": false
    }
}

PLAY RECAP ********************************************************************************************************
172.16.12.1                : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@tower1 temp_play]#

Thanks,
Charles Daniel


Stefan Hornburg (Racke)

unread,
Jul 8, 2021, 6:36:12 AM7/8/21
to ansible...@googlegroups.com
On 08/07/2021 12:18, Charles Daniel wrote:
> Hi Dick, Below is the complete output of the playbook,
>
> [root@tower1 temp_play]#  ansible-playbook testplay2.yml -i /etc/ansible/temp_play/hosts
>
> PLAY [Get the secondary router tunnel ip address] *****************************************************************
>
> TASK [Obtain secondary tunnel interface ip address] ***************************************************************
> ok: [172.16.12.1]
>
> TASK [debug] ******************************************************************************************************
> ok: [172.16.12.1] => {
>     "msg": [
>         "Internet address is 172.16.121.6/32 <http://172.16.121.6/32>"
>     ]
> }
>

Hello Charles,

the output looks like a string, so the map filter doesn't makes sense here.

Please try:

{{output.stdout | regex_search('(([0-9]+(.)[0-9]+(.)[0-9]+(.)[0-9]+))') }}

Regards
Racke


> TASK [set_fact] ***************************************************************************************************
> ok: [172.16.12.1]
>
> TASK [debug] ******************************************************************************************************
> ok: [172.16.12.1] => {
>     "msg": {
>         "ansible_facts": {
>             "intf": "- [u'172.16.121.6']\n"
>         },
>         "changed": false,
>         "failed": false
>     }
> }
>
> PLAY RECAP ********************************************************************************************************
> 172.16.12.1                : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
>
> [root@tower1 temp_play]#
>
> Thanks,
> Charles Daniel
>
>
> On Thu, 8 Jul 2021 at 15:34, Dick Visser <dick....@geant.org <mailto:dick....@geant.org>> wrote:
>
> You're sending only the output of the debug task with the regex_search
> applied to the output.stdout variable.
> We need to see the output.stdout variable before you mangled it.
>
> > To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAPnV%3Dh%2B7LxPqEnbrezq3Maofib9Ak8UmLUsK10twE0DeqXKBPw%40mail.gmail.com <https://groups.google.com/d/msgid/ansible-project/CAPnV%3Dh%2B7LxPqEnbrezq3Maofib9Ak8UmLUsK10twE0DeqXKBPw%40mail.gmail.com>.
>
>
>
> --
> Dick Visser
> Trust & Identity Service Operations Manager
> GÉANT
>
> --
> 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 <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAL8fbwPitsJ0-RyoEdfUWJkV%3DBusc8y7gx8r_YnB1YuvDwuCeQ%40mail.gmail.com <https://groups.google.com/d/msgid/ansible-project/CAL8fbwPitsJ0-RyoEdfUWJkV%3DBusc8y7gx8r_YnB1YuvDwuCeQ%40mail.gmail.com>.
>
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAPnV%3DhLBRtLbu0RWnNodwDs7v7xfrtprATkeKhfKC6%2By98OVsQ%40mail.gmail.com <https://groups.google.com/d/msgid/ansible-project/CAPnV%3DhLBRtLbu0RWnNodwDs7v7xfrtprATkeKhfKC6%2By98OVsQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration.

Rajthecomputerguy

unread,
Jul 8, 2021, 7:32:46 AM7/8/21
to ansible...@googlegroups.com
Try   "{{ output.stdout[0] }}" on the debug task

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/db06aa70-d5d3-de3e-f7ab-2c608b915ed3%40linuxia.de.


--

Thanks,

Pushparaj G


Charles Daniel

unread,
Jul 8, 2021, 9:20:54 AM7/8/21
to ansible...@googlegroups.com
returns error as a pattern not matching!!.

TASK [debug] ******************************************************************************************************
ok: [172.16.12.1] => {
    "hostvars['SEC_HOST']['sec_tunnel_ip1']": "- [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]\n"
}
 

Thanks,
Charles Daniel


Brian Coca

unread,
Jul 8, 2021, 10:33:27 AM7/8/21
to Ansible Project
no need for dummy, just use hostvars[<host that has var>][<varname>]

if you set the vars on a group you can
hostvars[group[<groupname>][0]][<varname>] ( 0 is 'first host in
group, but you can use random index <= # of hosts in group)

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

Charles Daniel

unread,
Jul 8, 2021, 1:38:33 PM7/8/21
to ansible...@googlegroups.com, bc...@redhat.com
Thanks Brian

--
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.
--
Thanks,
Charles Daniel
Reply all
Reply to author
Forward
0 new messages