# using ansible facts in key value

**URL:** <https://forum.ansible.com/t/using-ansible-facts-in-key-value/32417>\
**Category:** Ansible Project\
**Created:** [March 8, 2020, 1:41pm UTC](https://forum.ansible.com/t/using-ansible-facts-in-key-value/32417 "2020-03-08T13:41:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Azir\_Guleroglu](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Azir\_Guleroglu](https://forum.ansible.com/u/Azir_Guleroglu)\
**Post date:** [March 8, 2020, 1:41pm UTC](https://forum.ansible.com/t/using-ansible-facts-in-key-value/32417/1 "2020-03-08T13:41:39Z")

</div>

Hello,

I try to use ansible facts in variable file but i get below error. Anyone how any idea how can i use my variable in to the playbook

fatal: [ANSIBLET01]: FAILED! =\> {“ansible\_facts”: {}, “ansible\_included\_var\_files”: , “changed”: false, “message”: “Syntax Error while loading YAML.\n found unacceptable key (unhashable type: ‘AnsibleMapping’)\n\nThe error appears to be in ‘/etc/ansible/yml/AzirPlaybooks/install/elk/elkvar.yml’: line 1, column 12, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\nhostname: {{ ansible\_hostname }}\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with\_items:\n - {{ foo }}\n\nShould be written as:\n\n with\_items:\n - "{{ foo }}"\n”}

**My var file.**

**cat elkvar.yml**  
hostname: {{ ansible\_hostname }}  
network: {{ ansible\_default\_ipv4.address }}  
transport: {{ ansible\_default\_ipv4.address }}

**My part of playbook.**

tasks:

- name: include dynamic variables  
include\_vars: elkvar.yml

- name: replace security  
lineinfile:  
path: /etc/elasticsearch/elasticsearch.yml  
insertafter: ‘#node.name: node-1’  
line: ‘node.name: “{{ hostname }}”’

- name: replace security  
lineinfile:  
path: /etc/elasticsearch/elasticsearch.yml  
insertafter: ‘#network.host: 192.168.0.1’  
line: ‘network.host: {{ network }}’

- name: replace security  
lineinfile:  
path: /etc/elasticsearch/elasticsearch.yml  
insertafter: ‘http.cors.allow-origin’  
line: ‘transport.host: {{ transport }}’

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 8, 2020, 1:46pm UTC](https://forum.ansible.com/t/using-ansible-facts-in-key-value/32417/2 "2020-03-08T13:46:49Z")

</div>

> Hello,
> 
> I try to use ansible facts in variable file but i get below error. Anyone how any idea how can i use my variable in to  
> the playbook

Actually anyone who reads the error message carefully ... you need double quotes around the curly braces.

hostname: "{{ ansible\_hostname }}"

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Azir\_Guleroglu](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Azir\_Guleroglu](https://forum.ansible.com/u/Azir_Guleroglu)\
**Post date:** [March 8, 2020, 2:08pm UTC](https://forum.ansible.com/t/using-ansible-facts-in-key-value/32417/3 "2020-03-08T14:08:58Z")

</div>

Hello Stefan

I tried it before in to the playbook. But now i did in var file and its work. Thanks

8 Mart 2020 Pazar 16:47:24 UTC+3 tarihinde Stefan Hornburg (Racke) yazdı:
