How to use a gathered fact to get item from a dictionary?

22 views
Skip to first unread message

ZillaYT

unread,
Oct 11, 2016, 7:31:37 PM10/11/16
to Ansible Project
I have a dictionary that defines AWS mounts based on IP address

aws_efs_mount:
- 10.22.100.1: aws_mount_10_22_100_1
- 10.22.100.2: aws_mount_10_22_100_2

Now I want a task that creates an entry in /etc/fstab

# The idea is the src value resolves to src=aws_mount_10_22_100_1 for a IP of 10.22.100.1
- name: Add entry in /etc/fstab
  mount
: name=/mt/mount_path src="{{ aws_efs_mount.{{ ansible_default_ipv4.address }} }}"


I get

TASK [elasticsearch-elk : Add entry in /etc/fstab *****
fatal
: [10.22.x.x]: FAILED! => {"failed": true, "msg": "plate error while templating string: expected name or number. String: {{ aws_efs_mount.{{ ansible_default_ipv4.address }} }}"}

How can I achieve this? I can't use a template since there are values in the /etc/fstab file.

Kai Stian Olstad

unread,
Oct 12, 2016, 10:27:30 AM10/12/16
to ansible...@googlegroups.com
On 12. okt. 2016 01:31, ZillaYT wrote:
> I have a dictionary that defines AWS mounts based on IP address
>
> aws_efs_mount:
> - 10.22.100.1: aws_mount_10_22_100_1
> - 10.22.100.2: aws_mount_10_22_100_2

You don't have a dictionary, you have a list with dictionaries.


> Now I want a task that creates an entry in /etc/fstab
>
> # The idea is the src value resolves to src=aws_mount_10_22_100_1 for a IP
> of 10.22.100.1
> - name: Add entry in /etc/fstab
> mount: name=/mt/mount_path src="{{ aws_efs_mount.{{
> ansible_default_ipv4.address }} }}"
>
>
> I get
>
> TASK [elasticsearch-elk : Add entry in /etc/fstab *****
> fatal: [10.22.x.x]: FAILED! => {"failed": true, "msg": "plate error while
> templating string: expected name or number. String: {{ aws_efs_mount.{{
> ansible_default_ipv4.address }} }}"}
>
> How can I achieve this? I can't use a template since there are values in
> the /etc/fstab file.

You can't use curly brackets inside curly bracket.

To make it work you need to change aws_efs_mount to
aws_efs_mount:
10.22.100.1: aws_mount_10_22_100_1
10.22.100.2: aws_mount_10_22_100_2

And the src in mount to
src={{
aws_efs_mount[hostvars[inventory_hostname].ansible_default_ipv4.address] }}

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages