# Erorr while using magic variables

**URL:** <https://forum.ansible.com/t/erorr-while-using-magic-variables/32784>\
**Category:** Ansible Project\
**Created:** [April 26, 2020, 2:01am UTC](https://forum.ansible.com/t/erorr-while-using-magic-variables/32784 "2020-04-26T02:01:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![P\_r1](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@P\_r1](https://forum.ansible.com/u/P_r1)\
**Post date:** [April 26, 2020, 2:01am UTC](https://forum.ansible.com/t/erorr-while-using-magic-variables/32784/1 "2020-04-26T02:01:03Z")

</div>

Please refer to the attached file for issues I am running into…

**Here is my playbook:**

— # Ansible facts example

- hosts: centos

tasks:

- name: create a file to write the fact values

lineinfile:

path: /home/ansible/hostname

create: yes

line: “{{ ansible\_hostname }}”

- name: access magic variables

lineinfile:

path: /home/ansible/hostname

line: “{{ hostvars[‘centos’][‘ansible\_default\_ipv4’][‘address’] }}”

**And here is the command I am using to run it:**

ansible-playbook -i inv facts.yml

**And here is the inventory (host):**

[centos]

cen ansible\_host=172.31.4.215

**I keep running into this error:**

PLAY [centos] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [cen]

TASK [create a file to write the fact values] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [cen]

TASK [access magic variables] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

fatal: [cen]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: "hostvars[‘centos’]" is undefined\n\nThe error appears to have been in ‘/home/ansible/facts.yml’: line 9, column 13, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n line: "{{ ansible\_hostname }}"\n - name: access magic variables\n ^ here\n”}

to retry, use: --limit @/home/ansible/facts.retry

PLAY RECAP

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [April 26, 2020, 4:53am UTC](https://forum.ansible.com/t/erorr-while-using-magic-variables/32784/2 "2020-04-26T04:53:44Z")

</div>

Hi

I don’t see attached files (which is good I’d say) but then the indentation of the include yaml files is a bit messed up.

Regardless of that, it is not clear what you are trying to do with this playbook?  
Write the default ipv4 address of all hosts in a group to a file on all those hosts?  
Please explain what you want to do, and why (as there may be alternative way to achieve what you want).

---

<div class="post-metadata">

**Author:** ![P\_r1](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@P\_r1](https://forum.ansible.com/u/P_r1)\
**Post date:** [April 26, 2020, 4:07pm UTC](https://forum.ansible.com/t/erorr-while-using-magic-variables/32784/3 "2020-04-26T16:07:33Z")

</div>

Hello -  
I am trying to create a new file called 'hostname", and within it trying to copy the ansible\_hostname, and ipv4 address using the hostvars magic variable.  
Attaching the screenshots.

 ![Screen Shot 2020-04-26 at 9.06.21 AM.png](https://us1.discourse-cdn.com/ansible/original/2X/c/ccb7de84995b002ccb30294383d292f1bd537158.png)

 ![Screen Shot 2020-04-26 at 9.06.43 AM.png](https://us1.discourse-cdn.com/ansible/original/2X/1/18f2b19b65381b21cc4c4b445aafbca0f9fb22d6.png)

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [April 26, 2020, 4:23pm UTC](https://forum.ansible.com/t/erorr-while-using-magic-variables/32784/4 "2020-04-26T16:23:34Z")

</div>

If you want to create files then I would suggest using the template module, or even copy.

You are targeting a group, but are using hostvars- what is your actual intention? A group, a host, or…? This is necessary to answer your question.

BTW attaching screendumps add unnecessary clutter for people and make it difficult for content to be indexed. So don’t use screendumps but just text.

> **(attachments)**
>
> ![Screen Shot 2020-04-26 at 9.06.21 AM.png](https://us1.discourse-cdn.com/ansible/original/2X/c/ccb7de84995b002ccb30294383d292f1bd537158.png)  
> ![Screen Shot 2020-04-26 at 9.06.43 AM.png](https://us1.discourse-cdn.com/ansible/original/2X/1/18f2b19b65381b21cc4c4b445aafbca0f9fb22d6.png)

---

<div class="post-metadata">

**Author:** ![P\_r1](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@P\_r1](https://forum.ansible.com/u/P_r1)\
**Post date:** [April 26, 2020, 4:33pm UTC](https://forum.ansible.com/t/erorr-while-using-magic-variables/32784/5 "2020-04-26T16:33:34Z")

</div>

This is part of the training module I am currently learning, to try how the magic variables work.  
the centos is group, with one host, in the inventory file.  
the output of this playbook is supposed to be a file named “hostname” gets created on target host. And it should have two values: “ansible\_hostname”, and the “ipv4” address.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [April 27, 2020, 5:09am UTC](https://forum.ansible.com/t/erorr-while-using-magic-variables/32784/6 "2020-04-27T05:09:42Z")

</div>

Which training is this?

Usually with a training you need to come up with answers to questions yourself, and the trainer shows you the answer if you don’t know it.

---

<div class="post-metadata">

**Author:** ![P\_r1](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@P\_r1](https://forum.ansible.com/u/P_r1)\
**Post date:** [April 27, 2020, 5:11am UTC](https://forum.ansible.com/t/erorr-while-using-magic-variables/32784/7 "2020-04-27T05:11:25Z")

</div>

Its linux academy, video based…no trainer as such.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [April 27, 2020, 7:24am UTC](https://forum.ansible.com/t/erorr-while-using-magic-variables/32784/8 "2020-04-27T07:24:17Z")

</div>

Assuming you connect as user ‘ansible’, this playbook will do what you want:

---

<div class="post-metadata">

**Author:** ![P\_r1](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@P\_r1](https://forum.ansible.com/u/P_r1)\
**Post date:** [April 27, 2020, 11:39pm UTC](https://forum.ansible.com/t/erorr-while-using-magic-variables/32784/9 "2020-04-27T23:39:11Z")

</div>

Thank you ! that worked 🙂
