0

I want to extract data from a csv, turn it into a list that would be a variable (data_list in this case) and input the information according to the parameters listed in the task file. But I am getting this error, "'data_list' is undefined".

Here is the main playbook:

---
- name: Read Users 
  hosts: localhost
  vars:
    data_list: []
  tasks:
  - read_csv:
      path: user.csv
      key: name  
      fieldnames: name,firstname,surname,displayName,groups
      delimiter: ','
    register: userdata

  - name: Extract the list
    set_fact:
      data_list: "{{ data_list + [{ 'name': item.value.name, 'firstname': item.value.firstname, 'surname': item.value.surname, 'displayName': item.value.displayName, 'groups': item.value.groups }] }}"
    loop: "{{ userdata.dict|dict2items }}"
    when:
      - item.key != 'Name'

  - debug:
      msg: "{{ item.name }}"
    with_items: "{{ data_list }}"  

- name: Create multiple users
  hosts: "{{ hostname }}"
  gather_facts: false
  any_errors_fatal: false
  become: yes
  become_method: runas
  become_user: admin
  roles:
    - { role: Create Multiple Users }

Here is my tasks file:

---
- name: Create users
  community.windows.win_domain_user:
    name: "{{ item.name }}"
    firstname: "{{ item.firstname }}"
    surname: "{{ item.surname }}"
    attributes:
      displayName: "{{ item.firstname + ' ' + item.surname }}"
    groups:
      - "{{ item.groups }}"
  loop:
  with_items:  
    - "{{ data_list }}"

After running the playbooks, I received this error:

TASK [Create Multiple Users : Create user] *************************************
fatal: [10.12.201.20]: FAILED! => {"msg": "'data_list' is undefined"}

While keeping everything else default, I've already tried adding vars to:

- name: Create multiple users
  hosts: "{{ hostname }}"
  gather_facts: false
  any_errors_fatal: false
  become: yes
  become_method: runas
  become_user: admin
  roles:
    - { role: Create Multiple Users }
  vars:
    - "{{ data_list }}" 
---
- name: Create user
  vars:
    - "{{ data_list }}" 
  community.windows.win_domain_user:
    name: "{{ item.name }}"
    firstname: "{{ item.firstname }}"
    surname: "{{ item.surname }}"
    attributes:
      displayName: "{{ item.firstname + ' ' + item.surname }}"
    groups:
      - "{{ item.groups }}"
  loop:
  with_items: 
    - "{{ data_list }}"

And also tried the following syntax:

vars:
  - "{{ data_list }}" 

vars: "{{ data_list }}"


with_items:  
  - "{{ data_list }}"

with_items: "{{ data_list }}"

But I am still getting errors.

So in this case, how do I define data_list?

Update

Some of you have suggested me to combine the content in the task file into the first playbook.

But after running the playbook, the portion on "- name: Create multiple Windows AD user accounts" seems to have been entirely skipped by ansible.

Whatever troubleshooting that I have done either results in some error or it gets skipped by ansible. I am not sure which is the right way of writing the playbook so that I don't face anymore errors and I am able to create users.

My playbook looks like this now:


---
- name: Read Users 
  hosts: localhost
  vars:
    data_list: []
  tasks:
  - read_csv:
      path: user.csv
      key: name  
      fieldnames: name,firstname,surname,displayName,groups
      delimiter: ','
    register: userdata

  - name: Extract the list
    set_fact:
      data_list: "{{ data_list + [{ 'name': item.value.name, 'firstname': item.value.firstname, 'surname': item.value.surname, 'displayName': item.value.displayName, 'groups': item.value.groups }] }}"
    loop: "{{ userdata.dict|dict2items }}"
    when:
      - item.key != 'Name'

  - debug:
      msg: "{{ item.name }}"
    with_items: "{{ data_list }}"  

- name: Create multiple users
  hosts: "{{ hostname }}"
  gather_facts: false
  any_errors_fatal: false
  become: yes
  become_method: runas
  become_user: admin
  vars:
    data_list: []
  tasks: 
    - name: Create users
      community.windows.win_domain_user:
        name: "{{ item.name }}"
        firstname: "{{ item.firstname }}"
        surname: "{{ item.surname }}"
        attributes:
          displayName: "{{ item.firstname + ' ' + item.surname }}"
        groups:
          - "{{ item.groups }}" 
      loop:
      with_items: "{{ data_list }}" 

Whaily
  • 39
  • 6
  • 1
    Does this answer your question? [How do I set register a variable to persist between plays in ansible?](https://stackoverflow.com/questions/33896847/how-do-i-set-register-a-variable-to-persist-between-plays-in-ansible) – β.εηοιτ.βε Sep 05 '22 at 08:28
  • In you case, you can also have your three tasks run on localhost in the same play, but delegated with `delegate_to: localhost`. https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html – β.εηοιτ.βε Sep 05 '22 at 08:30
  • Is it really necessary to structure it in multiple plays? Why not execute the user extraction tasks in a block with "run_once" and "delegate_to:localhost", then run the tasks of the second play? (I know this doesn't answer your question exactly, but it explores another approach) – Houssem AZZOUZ Sep 05 '22 at 09:02
  • @HoussemAZZOUZ I initially thought ansible has to read the main playbook first, then go to roles directory, then the tasks file. I tried combining all the tasks into 1 play, but ansible skipped the last task. – Whaily Sep 06 '22 at 05:29

0 Answers0