Using a variable in a loop

28 views
Skip to first unread message

lift...@gmail.com

unread,
Jun 25, 2021, 2:44:40 PM6/25/21
to Ansible Project
I'm trying to look through a loop of users to get their email addresses from an IPA server.  When I run it, I get an error stating that "name" is an undefined variable.  Can anyone see what I'm doing wrong?

---
- hosts: localhost
  become: yes
  become_method: sudo
  gather_facts: no

  vars_files:
    - /etc/ansible/vault.yml

  vars:
    - users:
      - { name: "user1 }
      - { name: "user2" }

  tasks:

  - name: Get User's Email Address
    shell: "set -o pipefail && /bin/ipa user-show {{ item.name }} --all --raw | grep mail: | awk '{print $2}'"
    register: email
    loop:
    - "{{ users }}"

  - name: Print email
    debug:
      msg: "{{ email.stdout }}"

The error states  "The task includes an option with an undefined variable.  The error was: 'list object' has no attribute 'name'".

Thanks,
Harry

Matt Martz

unread,
Jun 25, 2021, 2:49:48 PM6/25/21
to ansible...@googlegroups.com
Instead of:

  loop:
    - "{{ users }}"

You want:

  loop: "{{ users }}"

Otherwise, you are creating a list in YAML with the value of users as the single element.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ac83150f-a884-4a72-8024-330f5ce8b0fen%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

lift...@gmail.com

unread,
Jun 25, 2021, 2:58:59 PM6/25/21
to Ansible Project
Thanks!  I feel stupid.  That worked.  However, now I'm getting an error in printing out the user's email address.  I'm registering a variable called email, and when I do the debug message, I get an error that says that stdout isn't defined.  if I just print out the variable 'email' I see stdout present.  I'm just doing this so i can hopefully use the email later on.

Thanks,
Harry

Matt Martz

unread,
Jun 25, 2021, 3:18:22 PM6/25/21
to ansible...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages