How do I loop a task with variables defined in another file?

22 views
Skip to first unread message

Olof Lundgren

unread,
Apr 11, 2018, 2:57:16 AM4/11/18
to Ansible Project
I have built a .yml file with a set of users that I want to push to my routers with vyos_user module.
This is my users.yml

users:
 
- full_name:               "Olof Lundgren"
    name
:                    "olof"
    configured_password
:     "abcd1234"
    level
:                   "admin"
    ssh
-key:
        name
:                "olof@olof-ubuntu-work"
        type
:                "ssh-rsa"
        key
:                  "xxxxxxxxxxxxxxx"


 
- full_name:           "T7D VyOS default user"
    name
:                "vyos"
    configured_password
: "vyos"
    level
:               "admin"


 
- full_name:           "Servicedesk operator user"
    name
:                "servicedesk"
    configured_password
: "password"
    level
:               "operator"


I built my playbook, but I dont know how to use this list of users in the play.

This is my attempt: add-users-vyos.yml

  - hosts:                        vyos
    connection
:                   network_cli
    gather_facts
:                 no


    tasks
:
       
- name:                   Include user database
          include_vars
:
            file
:                 users.yml
#            name:                 vyosusers


       
- name:                    add and set users
          vyos_user
:
            full_name
:           "{{ users.full_name }}"
            configured_password
: "{{ users.configured_password }}"
            name
:                "{{ users.name }}"
            level
:               admin
            purge
:               no
            state
:               present
            update_password
:     always
          loop
: "{{ users }}"


First it successfully plays the task "Include user database" and I can see the variables are defined 
The on the next task. my error message is this: 

TASK [add and set users] ************************************************************************************************************************************************************************************
fatal
: [vyos-router.lan]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'configured_password'\n\nThe error appears to have been in '/home/olof/Documents/ansible/add-users-vyos.yml': line 11, column 11, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n        - name:                    add and set users\n          ^ here\n"}


Olof Lundgren

unread,
Apr 11, 2018, 3:01:23 AM4/11/18
to Ansible Project
This is what the task "Include user database" debug message looks like when its jsonprettyfied

TASK [Include user database] ********************************************************************************************************************************************************************************
ok: [vyos-router.lan] => {
   
"ansible_facts": {
       
"users": [{
           
"configured_password": "abcd1234",
           
"full_name": "Olof Lundgren",
           
"level": "admin",
           
"name": "olof",
           
"ssh-key": {
               
"key": "xxxxxxxxxxxxxxxxx",
               
"name": "olof@olof-ubuntu-work",
               
"type": "ssh-rsa"
           
}
       
},  {
           
"configured_password": "vyos",
           
"full_name": "T7D VyOS default user",
           
"level": "admin",
           
"name": "vyos"
       
}, {
           
"configured_password": "password",
           
"full_name": "Servicedesk operator user",
           
"level": "operator",
           
"name": "servicedesk"
       
}]
   
},
   
"ansible_included_var_files": ["/home/olof/Documents/ansible/users.yml"],
   
"changed": false
}

Kai Stian Olstad

unread,
Apr 14, 2018, 5:01:34 AM4/14/18
to ansible...@googlegroups.com
On Wednesday, 11 April 2018 08.57.16 CEST Olof Lundgren wrote:
> I built my playbook, but I dont know how to use this list of users in the
> play.
>
> This is my attempt: *add-users-vyos.yml*
>
> - hosts: vyos
> connection: network_cli
> gather_facts: no
>
>
> tasks:
> - name: Include user database
> include_vars:
> file: users.yml
> # name: vyosusers
>
>
> - name: add and set users
> vyos_user:
> full_name: "{{ users.full_name }}"
> configured_password: "{{ users.configured_password }}"
> name: "{{ users.name }}"
> level: admin
> purge: no
> state: present
> update_password: always
> loop: "{{ users }}"

When using loop/with_ the current result is in a variable called item, so change all your {{ user.xxxx }} to {{ item.xxxx }}

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