Home > Back-end >  Ansible playbook not woking with multivalued lists
Ansible playbook not woking with multivalued lists

Time:10-24

I am trying to write a playbook that creates multiple users but it does not work when i put my variables in a list form.

Not working playbook

[root@ansible-master playbooks]# cat users_list_new.yml
---
users:
  - username:
      - amba
      - ruchita

[root@ansible-master playbooks]# cat multi_users_new.yml
---
- name: Creating the multi users with a new approach
  hosts: california
  become: true
  vars_files:
    - users_list_new.yml
  tasks:
    - name: Create the user
      user:
        name: "{{ item.username[0] }}"
      loop: "{{ users }}"

It only creates the first user because I have put the subscript 0 in the user module. My question is how we can create a loop over username . For instance I modified my playbook but it didn't work

---
- name: Creating the multi users with a new approach
  hosts: california
  become: true
  vars_files:
    - users_list_new.yml
  tasks:
    - name: Create the user
      user:
        name: "{{ item }}"
      loop: "{{ users.username }}"

It threw below error when I ran the playbook

PLAY [Creating the multi users with a new approach] ********************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [10.128.0.5]

TASK [Create the user] *************************************************************************************************************************************************
fatal: [10.128.0.5]: FAILED! => {"msg": "'list object' has no attribute 'username'"}

PLAY RECAP *************************************************************************************************************************************************************
10.128.0.5                 : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

[root@ansible-master playbooks]#

Please help me to figure out the solution

****EDIT **** Suppose I want to create two users . What is the best practice of variable declaration ?

First Approach

users:
  - username:
      - amba
      - ruchita
  - shell:
      - /bin/bash
      - /bin/ksh

Second Approach

users:
  - username: amba
    shell: /bin/bash
  - username: ruchita
    shell: /bin/bash

CodePudding user response:

Your issue is that your variable structure users contains a list not a dict so you cant access username as a key of it since its a list not a dict. You would need to access as users[0] to get access to the list of usernames.

---

- name: Creating the multi users with a new approach
  hosts: localhost
  vars_files:
    - users_list_new.yml
  tasks:
    - name: Create the user
      user:
        name: "{{ item }}"
      loop: "{{ users[0].username }}"
TASK [Create the user] ********************************************************************************************************************************************************************
Sunday 23 October 2022  08:47:41  0100 (0:00:07.567)       0:00:08.179 ******** 
changed: [localhost] => (item=amba)
changed: [localhost] => (item=ruchita)

PLAY RECAP ********************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

BEFORE

root@ff9c44b4c94f:/local# id amba; id ruchita
id: ‘amba’: no such user   
id: ‘ruchita’: no such user

AFTER

root@ff9c44b4c94f:/local# id amba; id ruchita
uid=8257(amba) gid=8257(amba) groups=8257(amba)
uid=8258(ruchita) gid=8258(ruchita) groups=8258(ruchita)
  • Related