How to iterate over nested lists with lineinfile?

85 views
Skip to first unread message

Joanna Delaporte

unread,
Sep 23, 2015, 2:48:50 PM9/23/15
to Ansible Project
I am trying to build a hash of users from a csv file. I am stuck on this last step, where the playbook just hangs:

   - name: print user details into hash
     lineinfile: 'dest=~csit-ansible/files/auto_students.yml
                 line="{{ item }}"
                 state=present'
     with_items:
      - "  username: {{ item[0] }}"
      - "    firstname: {{ item[1] }}"
      - "    lastname: {{ item[2] }}"
      - "    studentid: {{ item[3] }}"
      - "    dob: {{ item[4] }}"
     with_nested:
      - "{{ usernames.stdout_lines }}"
      - "{{ firstnames.stdout_lines }}"
      - "{{ lastnames.stdout_lines }}"
      - "{{ studentids.stdout_lines }}"
      - "{{ dobs.stdout_lines }}"


Earlier in the playbook, I register the values like so:
   - name: print all usernames to stdout
     shell: awk -F ':' '{print $1}' ~csit-ansible/files/sortedu${date}.csv
     register: usernames

   - name: print all firstnames to stdout
     shell: awk -F ':' '{print $2}' ~csit-ansible/files/sortedu${date}.csv
     register: firstnames

   - name: print all lastnames to stdout
     shell: awk -F ':' '{print $3}' ~csit-ansible/files/sortedu${date}.csv
     register: lastnames

   - name: print all studentids to stdout
     shell: awk -F ':' '{print $4}' ~csit-ansible/files/sortedu${date}.csv
     register: studentids

   - name: print all dobs to stdout
     shell: awk -F ':' '{print $5}' ~csit-ansible/files/sortedu${date}.csv
     register: dobs

Joanna Delaporte

unread,
Sep 24, 2015, 12:57:25 PM9/24/15
to Ansible Project
I ended up learning a little python3 so I could use it to generate the dictionary for me.
Reply all
Reply to author
Forward
0 new messages