-1

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 }}"

  • => `hostvars.localhost.users.dict[inventory_hostname].uid` – β.εηοιτ.βε Dec 08 '22 at 18:42
  • I tried that and I get the same error. > template error while templating string: ...expected name or number – seekingassistance Dec 08 '22 at 19:05
  • You should then show your inventory and the definition of your variables. – β.εηοιτ.βε Dec 08 '22 at 19:07
  • But you are probably still doing the same mistake: `hostvars.localhost.users.dict.[inventory_hostname].uid` instead of `hostvars.localhost.users.dict[inventory_hostname].uid`; mind that there should be not dot before the brackets. – β.εηοιτ.βε Dec 08 '22 at 19:13
  • That was the problem, I had a `.` after `dict` Thank you for your help. I am new to stack and not quite sure how to mark your response as the answer. – seekingassistance Dec 08 '22 at 19:35
  • Also, would you mind elaborating on why the `.` broke the command? If I use `.dict.dag.uid` and it works why would I not use `.dict.[inventory_hostname].name` or `.dict.inventory_hostname.uid`. – seekingassistance Dec 08 '22 at 19:41
  • That's a typo, so, no, I am not going to make an answer out of that. The dot and brackets notations are two faces of the same coin: https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#referencing-key-value-dictionary-variables. This is why you don't need a dot when using the brackets notations. And in your case you need the bracket notation in order to access a property of your dictionary based on a variable. – β.εηοιτ.βε Dec 08 '22 at 20:04
  • Also worth reading: https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#ansible-allows-dot-notation-and-array-notation-for-variables-which-notation-should-i-use – β.εηοιτ.βε Dec 08 '22 at 20:06

1 Answers1

0

Someone replied to my question with the below code which worked.

The '' and . needed to be removed when calling inventory_hosts

Full code:

hostvars.localhost.users.dict[inventory_hostname].uid