I want to put a list of names and descriptions into a CSV, and then for each host in hostvars, pull out the corresponding descriptions in the CSV.
Below is my code
---
# Goals
# 1. Read CSV into dictionary
# 2. For each host in the playbook, print the UID of the name of the host
# 3. For example, when run on host dag, 500 should be printed,
# when run on host bob, two should be printed.
# CSV Data:
# name,uid,gid
# dag,500,550
# bob,two,ten
# john,200,250
- name: Load CSV
hosts: localhost
gather_facts: no
tasks:
- name: Read CSV Data
community.general.read_csv:
path: users.csv
key: name
register: users
- name: Print UID based on hostname of host being run on
hosts: all
gather_facts: no
tasks:
- name: Print name
ansible.builtin.debug:
msg: "{{ hostvars.localhost.users.dict.['inventory_hostname'].uid }}"
# Expect to see 500 for first host, two for second, and 200 for third
I tried the above code but got the below error:
template error while templating string: ...expected name or number
If I specify the specific key using the below code, it returns the name, but its the same name every time.
"{{ hostvars.localhost.users.dict.dag.uid }}"