Trying to use previous task output to new task to modify one file, But it is throwing error variable not defined

20 views
Skip to first unread message

Sudhanshu Sharma

unread,
Apr 21, 2017, 7:36:57 AM4/21/17
to Ansible Project
Hi,

I am trying to use previous task output to new task to modify one file, But it is throwing error variable not defined - 

---
- hosts: host4
remote_user: root
tasks:
- name: Get MAC of host4
shell: cat /sys/class/net/eth0/address

register: x

- debug: var=x.stdout

- hosts: host3
remote_user: root
tasks:
- name: find MAC_hos4 word in topology.cfg and replace the same with actual MAC of host4
lineinfile: path=/root/topology.cfg
regexp= '^mac=MAC_IS'
line= 'mac={{x.stdout}}'

Can any one please suggest something to rectify the problem.

Brian Coca

unread,
Apr 21, 2017, 4:02:48 PM4/21/17
to Ansible Project
x only exists for host4, in the context of host3 it does not, you need to do:

{{hostvars['host4']['x']['stdout']}}


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

Sudhanshu Sharma

unread,
Apr 22, 2017, 10:05:07 AM4/22/17
to Ansible Project
Thanks Brian,

Its working. 

as I am using Ansible version 2.2 so I used " instead of '  - 

{{hostvars["host4"]["x"]["stdout"]}}

Thanks once again :) 

Br//Sudhanshu
Reply all
Reply to author
Forward
0 new messages