Accessing variables in yaml file

39 views
Skip to first unread message

Andy

unread,
Nov 29, 2018, 5:11:30 PM11/29/18
to Ansible Project
I am new to ansible. 
I create a yaml file with multiple hosts defined

---
-hosts: localhost
  vars: ....
  tasks: ....

-hosts: webserver1
 vars: same as localhost
 tasks: ....

-hosts: webserver2
 vars: same as localhost
 tasks: ...

- hosts: localhost
  vars: same as localhost
  tasks: ....

Is there a way for me to get all vars in one place instead of redefining it multiple times?
Thanks!

Uwe Sauter

unread,
Nov 29, 2018, 5:40:58 PM11/29/18
to ansible...@googlegroups.com
Look for group vars and put all servers in the same group in the inventory file.

Am 29.11.18 um 23:11 schrieb Andy:
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/7f325296-b492-4615-a85a-075d384cd416%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/7f325296-b492-4615-a85a-075d384cd416%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Andy

unread,
Nov 29, 2018, 6:00:01 PM11/29/18
to Ansible Project
Is there a way to do it in the same playbook where I define my tasks? I do not have an inventory file.


On Thursday, November 29, 2018 at 2:40:58 PM UTC-8, Uwe Sauter wrote:
Look for group vars and put all servers in the same group in the inventory file.

Am 29.11.18 um 23:11 schrieb Andy:
> I am new to ansible.
> I create a yaml file with multiple hosts defined
>
> ---
> -hosts: localhost
>    vars: ....
>    tasks: ....
>
> -hosts: webserver1
>   vars: same as localhost
>   tasks: ....
>
> -hosts: webserver2
>   vars: same as localhost
>   tasks: ...
>
> - hosts: localhost
>    vars: same as localhost
>    tasks: ....
>
> Is there a way for me to get all vars in one place instead of redefining it multiple times?
> Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to

Tony Chia

unread,
Nov 30, 2018, 2:12:51 AM11/30/18
to Ansible Project
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

Uwe Sauter

unread,
Nov 30, 2018, 2:50:04 AM11/30/18
to ansible...@googlegroups.com
If you really cannot afford an inventory file and a group_vars file then have a look at https://stackoverflow.com/a/47811099


Am 30.11.18 um 00:00 schrieb Andy:
> Is there a way to do it in the same playbook where I define my tasks? I do not have an inventory file.
>
> On Thursday, November 29, 2018 at 2:40:58 PM UTC-8, Uwe Sauter wrote:
>
> Look for group vars and put all servers in the same group in the inventory file.
>
> Am 29.11.18 um 23:11 schrieb Andy:
> > I am new to ansible.
> > I create a yaml file with multiple hosts defined
> >
> > ---
> > -hosts: localhost
> >    vars: ....
> >    tasks: ....
> >
> > -hosts: webserver1
> >   vars: same as localhost
> >   tasks: ....
> >
> > -hosts: webserver2
> >   vars: same as localhost
> >   tasks: ...
> >
> > - hosts: localhost
> >    vars: same as localhost
> >    tasks: ....
> >
> > Is there a way for me to get all vars in one place instead of redefining it multiple times?
> > Thanks!
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to
> > ansible-proje...@googlegroups.com <javascript:> <mailto:ansible-proje...@googlegroups.com <javascript:>>.
> > To post to this group, send email to ansible...@googlegroups.com <javascript:> <mailto:ansible...@googlegroups.com
> <javascript:>>.
> <https://groups.google.com/d/msgid/ansible-project/7f325296-b492-4615-a85a-075d384cd416%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/7f325296-b492-4615-a85a-075d384cd416%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
> > For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com
> <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/735b0d5e-aa47-4840-9741-27d7cac9d0e1%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/735b0d5e-aa47-4840-9741-27d7cac9d0e1%40googlegroups.com?utm_medium=email&utm_source=footer>.

Brian Coca

unread,
Nov 30, 2018, 9:51:00 AM11/30/18
to ansible...@googlegroups.com
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.



--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages