# Accessing variables in yaml file

**URL:** <https://forum.ansible.com/t/accessing-variables-in-yaml-file/29154>\
**Category:** Ansible Project\
**Created:** [November 29, 2018, 10:11pm UTC](https://forum.ansible.com/t/accessing-variables-in-yaml-file/29154 "2018-11-29T22:11:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andy4](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Andy4](https://forum.ansible.com/u/Andy4)\
**Post date:** [November 29, 2018, 10:11pm UTC](https://forum.ansible.com/t/accessing-variables-in-yaml-file/29154/1 "2018-11-29T22:11:30Z")

</div>

I am new to ansible.  
I create a yaml file with multiple hosts defined

---

<div class="post-metadata">

**Author:** ![Uwe\_Sauter](https://avatars.discourse-cdn.com/v4/letter/u/41988e/32.png) [@Uwe\_Sauter](https://forum.ansible.com/u/Uwe_Sauter)\
**Post date:** [November 29, 2018, 10:40pm UTC](https://forum.ansible.com/t/accessing-variables-in-yaml-file/29154/2 "2018-11-29T22:40:45Z")

</div>

Look for group vars and put all servers in the same group in the inventory file.

---

<div class="post-metadata">

**Author:** ![Andy4](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Andy4](https://forum.ansible.com/u/Andy4)\
**Post date:** [November 29, 2018, 11:00pm UTC](https://forum.ansible.com/t/accessing-variables-in-yaml-file/29154/3 "2018-11-29T23:00:01Z")

</div>

Is there a way to do it in the same playbook where I define my tasks? I do not have an inventory file.

---

<div class="post-metadata">

**Author:** ![Tchia04](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@Tchia04](https://forum.ansible.com/u/Tchia04)\
**Post date:** [November 30, 2018, 7:12am UTC](https://forum.ansible.com/t/accessing-variables-in-yaml-file/29154/4 "2018-11-30T07:12:50Z")

</div>

If the variables you want to share between plays is static it’s more straight forward to use and group\_vars file.

The group\_vars file doesn’t have to be inside the inventory folder. It can be in a group\_vars folder on the same level as your playbook

---

<div class="post-metadata">

**Author:** ![Uwe\_Sauter](https://avatars.discourse-cdn.com/v4/letter/u/41988e/32.png) [@Uwe\_Sauter](https://forum.ansible.com/u/Uwe_Sauter)\
**Post date:** [November 30, 2018, 7:49am UTC](https://forum.ansible.com/t/accessing-variables-in-yaml-file/29154/5 "2018-11-30T07:49:52Z")

</div>

If you really cannot afford an inventory file and a group\_vars file then have a look at [https://stackoverflow.com/a/47811099](https://stackoverflow.com/a/47811099)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 30, 2018, 2:50pm UTC](https://forum.ansible.com/t/accessing-variables-in-yaml-file/29154/6 "2018-11-30T14:50:28Z")

</div>

group\_vars can be playbook adjacent, they do not require an inventory  
(though they normally work better with one).

You can also use `-e @varsfile.yml ` on the command line to add extra vars

Another option is to define them in a vars/myvars.yml and use  
vars\_files: in each play to include them, instead of repeating them.
