add list from host variables and group variables

17 views
Skip to first unread message

Marc Haber

unread,
May 25, 2019, 2:42:53 PM5/25/19
to ansible...@googlegroups.com
Hi,

this is what I have in a YAML formatted inventory

---
all:
vars:
city:
- Berlin
children:
g_empty:
hosts:
emptybuster:
emptysid:
emptystretch:
city:
- Steglitz

With this code, I get "city": [ "Berlin" ] for emptybuster and
emptystretch, and "city": [ "Steglitz" ] for emptystretch. I would, for
some reason, have "city": [ "Berlin", "Steglitz" ] instead.

I have searched on the web and found a collection of wild constructions
involving filters and/or set_fact, none of which yields the desired
result.

I have thought about setting hash_behavior=merge and making the city
list a hash instead, but setting hash_behavior=merge is not recommended.

Is there a more elegant way to do this, or have I reached ansible's
limits? If so, is there a bug report against ansible where I could read
up on the situation?

Greetings
Marc


--
-----------------------------------------------------------------------------
Marc Haber | "I don't trust Computers. They | Mailadresse im Header
Leimen, Germany | lose things." Winona Ryder | Fon: *49 6224 1600402
Nordisch by Nature | How to make an American Quilt | Fax: *49 6224 1600421

Sebastian Meyer

unread,
May 27, 2019, 4:21:16 AM5/27/19
to ansible...@googlegroups.com
Hi Marc,

On 25.05.19 20:42, Marc Haber wrote:
> this is what I have in a YAML formatted inventory
>
> ---
> all:
> vars:
> city:
> - Berlin
> children:
> g_empty:
> hosts:
> emptybuster:
> emptysid:
> emptystretch:
> city:
> - Steglitz
>
> Is there a more elegant way to do this, or have I reached ansible's
> limits? If so, is there a bug report against ansible where I could read
> up on the situation?

Well, Ansibles variable system isn't as featureful as e.g. hiera and
works mostly with overwrites.

I see two possibilities here:

1. Using the a lookup plugin, not inventory variables, e.g. hiera since
it supports all kinds of merging

https://docs.ansible.com/ansible/latest/plugins/lookup/hiera.html

2. Use a different name for the variables and use set_fact in your
playbook/a role to merge them.

---
all:
vars:
_city_all:
- Berlin
children:
g_empty:
hosts:
emptystretch:
city:
- Steglitz


---
- hosts: all
pre_tasks:
- set_fact:
city: "{{ _city_all + city | default([]) }}"
tasks:
- debug:
var: city


HTH,
Sebastian

--
Sebastian Meyer
Linux Consultant & Trainer
Mail: me...@b1-systems.de

B1 Systems GmbH
Osterfeldstraße 7 / 85088 Vohburg / http://www.b1-systems.de
GF: Ralph Dehner / Unternehmenssitz: Vohburg / AG: Ingolstadt,HRB 3537
Reply all
Reply to author
Forward
0 new messages