forked from gc3-uzh-ch/ansible-playbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnestedvars.yml
45 lines (44 loc) · 1.27 KB
/
nestedvars.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
---
#######################
# Nested VARS example #
#######################
#
# This example is used to explain how you can call nested variables
# (like, an item of a dictionary) using ansible prior v1.2. It may be
# a bit tricky because of the way ansible does the expansion of th
# evariables...
#
# Bascially, to expand `hostvars[item]` where `hostvars` is a
# dictionary-like object and `item` is a variable, you have to do:
#
# ${hostvars.{$item}}
#
# Note that the expansion of the dictionary is done with `${...}`
# while the expansion of the attribute of the dictionary is done with
# `{$...}`.
#
# The following example will define a dictionary `hosts`:
#
# hosts = { 'a': {'b': 'c'}}
#
# and try to print `hosts['a']['b']`
#
- hosts: all
vars:
hosts:
a:
b: c
x: a
y: b
tasks:
- debug: msg="${hosts.{$x}.{$y}}"
# The original use case I used for this was taken from this task, in
# which I wanted to produce an ``/etc/hosts`` file starting from the
# data sotred in the inventory, adding or updating the IP addresses of
# all the known hosts.
#
# - hosts: all
# connection: local
# tasks:
# - lineinfile: dest=/tmp/hostfile regexp=".* $item .*" line="${hostvars.{$item}.ansible_default_ipv4.address} $item "
# with_items: $hostvars