# How to use fact information such as IP address in task yml files

**URL:** <https://forum.ansible.com/t/how-to-use-fact-information-such-as-ip-address-in-task-yml-files/25467>\
**Category:** Ansible Project\
**Created:** [September 12, 2017, 9:12pm UTC](https://forum.ansible.com/t/how-to-use-fact-information-such-as-ip-address-in-task-yml-files/25467 "2017-09-12T21:12:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vijay\_R](https://avatars.discourse-cdn.com/v4/letter/v/67e7ee/32.png) [@Vijay\_R](https://forum.ansible.com/u/Vijay_R)\
**Post date:** [September 12, 2017, 9:12pm UTC](https://forum.ansible.com/t/how-to-use-fact-information-such-as-ip-address-in-task-yml-files/25467/1 "2017-09-12T21:12:35Z")

</div>

Hello there!

I am new to Ansible, I have been searching all through the web for this info, but I couldn’t able to get it correctly.

I am trying to get the IP address of the machine (local) with some variable under tasks/main.yml file using $tag, tag is a variable name, but I am not able to get it.

Have tried in playbook like this,

> - hosts: local

> connection: local  
> become: true  
> tasks:
> 
> - set\_fact:  
> tag: “{{ ansible\_default\_ipv4.address }}”  
> roles:
> - conf

Note: this tag variable has got the IP address, even tried to use debug and check the output.

What I am trying to do now is to introduce the IP address to the task file, but I am not able to,

conf/tasks/main.yml

-lineinfile:  
path: /root/test.conf  
regexps: ‘^IP\_ADDR’  
line: ‘IP\_ADDR:$tag’

Tried like this as well, line: “IP\_ADDR:$(tag)” none worked.

Please help me with this case.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 12, 2017, 9:29pm UTC](https://forum.ansible.com/t/how-to-use-fact-information-such-as-ip-address-in-task-yml-files/25467/2 "2017-09-12T21:29:23Z")

</div>

> - hosts: local
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connection: local  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;become: true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tag: "{{ ansible\_default\_ipv4.address }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;roles:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- conf

\<snip /\>

> -lineinfile:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /root/test.conf  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexps: '^IP\_ADDR'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: 'IP\_ADDR:$tag'

It's only one way to write variables in Ansible, double curly brackets, just as you did in your play above.

&nbsp;&nbsp;&nbsp;line: 'IP\_ADDR:{{ tag }}'

---

<div class="post-metadata">

**Author:** ![Vijay\_R](https://avatars.discourse-cdn.com/v4/letter/v/67e7ee/32.png) [@Vijay\_R](https://forum.ansible.com/u/Vijay_R)\
**Post date:** [September 12, 2017, 10:16pm UTC](https://forum.ansible.com/t/how-to-use-fact-information-such-as-ip-address-in-task-yml-files/25467/3 "2017-09-12T22:16:21Z")

</div>

Thanks for the quick response, but that didn’t help. I get the below error,

> Error:  
> TASK [conf : lineinfile] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> task path: /home/elastic/ansible/roles/conf/tasks/main.yml:61  
> fatal: [localhost]: FAILED! =\> {  
> “failed”: true,  
> “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefine  
> d. The error was: ‘tag’ is undefined\n\nThe error appears to have been in ‘/home/elastic/ansible/roles/  
> conf/tasks/main.yml’: line 61, column 3, but may\nbe elsewhere in the file depending on the exact synta  
> x problem.\n\nThe offending line appears to be:\n\n\n- lineinfile:\n ^ here\n”  
> }  
> to retry, use: --limit @/home/elastic/ansible/sample.retry  
> PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> localhost : ok=12 changed=0 unreachable=0 failed=1

This is the actual sample.yml(playbook file),

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 13, 2017, 5:32am UTC](https://forum.ansible.com/t/how-to-use-fact-information-such-as-ip-address-in-task-yml-files/25467/4 "2017-09-13T05:32:37Z")

</div>

Ansible exectue things in the following order.

pre\_tasks:  
roles:  
tasks:  
post\_tasks:

So you need to move your set\_fact to pre\_tasks so it will run before roles.

---

<div class="post-metadata">

**Author:** ![Vijay\_R](https://avatars.discourse-cdn.com/v4/letter/v/67e7ee/32.png) [@Vijay\_R](https://forum.ansible.com/u/Vijay_R)\
**Post date:** [September 13, 2017, 9:27am UTC](https://forum.ansible.com/t/how-to-use-fact-information-such-as-ip-address-in-task-yml-files/25467/5 "2017-09-13T09:27:18Z")

</div>

**Awesome! It worked, thanks for your help.**
