0

I'm working with ansible version 2.9.13 and I'm trying to modify the following task -which btw is working fine - so I could compare a list of given hostnames stored in a yml file against the fact ansible_hostname before/while performing the create student operation.

The following is the create_students task fully working:

create_a27_students.yml

---
- hosts: a27-alumnes
  become: yes
  vars_files:
    - users/students.yml

  tasks:
  - import_tasks: tasks/create_students.yml
  - import_tasks: tasks/restart_lightdm.yml

tasks/create_students.yml

  - name: Create student's regular users
    user:
      name: "{{ item.name }}"
      # mkpasswd --method=sha-512
      password: "{{ item.pass | password_hash('sha512') }}"
      state: present
      shell: /bin/bash       # Defaults to /bin/bash
      system: no             # Defaults to no
      createhome: yes        # Defaults to yes
      home: /home/{{ item.name }}       # Defaults to /home/<username>
    with_items: "{{  students  }}"

  - import_tasks: restart_lightdm.yml

tasks/restart_lightdm.yml

  - name: Check if lightdm service exists (stat module)
    stat: path=/etc/init.d/lightdm
    register: service_stat

  - name: Check if lightdm service exists (service module)
    service: name=lightdm
    register: service

  - name: Restart lightdm service if exists and is running
    service: name=lightdm state=restarted
    when: service_stat.stat.exists and service.status.SubState == 'running'
    register: service_started

The following is the var file yml, users/students.yml:

---
    students:
        - name: smx
          pass: smx
          state: present

        - name: smx2
          pass: smx2
          state: present

        - name: arduino
          pass: arduino
          state: present

The following would be the list of hostnames, data/hostnames.yml:

---
    hostnames:
        - name: PC-a27-01
          hostname: PC-a27-01

        - name: PC-a27-01
          hostname: PC-a27-01

        - name: arduino
          hostname: arduino

The following would be the create_students task modified to run the comparison but it doesn't seem to work:

create_a27_students.yml

---
- hosts: a27-alumnes
  become: yes
  vars_files:
    - users/students.yml
    - data/hostnames.yml

  tasks:
  - import_tasks: tasks/create_students.yml
  - import_tasks: tasks/restart_lightdm.yml

tasks/create_students.yml

  - name: Create student's regular users
    user:
      name: "{{ item.name }}"
      # mkpasswd --method=sha-512
      password: "{{ item.pass | password_hash('sha512') }}"
      state: present
      shell: /bin/bash       # Defaults to /bin/bash
      system: no             # Defaults to no
      createhome: yes        # Defaults to yes
      home: /home/{{ item.name }}       # Defaults to /home/<username>
    ## I would also be interested on using the "in" condition as long as the "=="
    when: item.hostname == ansible_hostname
    with_items: "{{  students  }}", "{{ hostnames }}"

  - import_tasks: restart_lightdm.yml

Hope someone can help me to fix the issue.

Thanks,

PD: Here's [1] a copy of my github repo in case it helps

[1] https://github.com/pauperis/ansible/tree/master/playbooks

1 Answer 1

0

hope this will be helpful.

First a comment, from Ansible 2.5, loop is the preferred syntax for iterations.
Ansible Doc - Migrating from with_X to loop

So to my understanding you like to iterate over nested lists.
Ansible Docs - Iterating over nested lists

Notice that the list items will be item.0 and item.1.
You you need to add the item index to your code as well item.0.name.

Following above example I think following would be something for you to elaborate around.
I just comment your previous line and add new line underneath.

  - name: Create student's regular users
    user:
      #name: "{{ item.name }}"
      name: "{{ item.0.name }}"
      # mkpasswd --method=sha-512
      #password: "{{ item.pass | password_hash('sha512') }}"
      password: "{{ item.0.pass | password_hash('sha512') }}"
      state: present
      shell: /bin/bash       # Defaults to /bin/bash
      system: no             # Defaults to no
      createhome: yes        # Defaults to yes
      #home: /home/{{ item.name }}       # Defaults to /home/<username>
      home: /home/{{ item.0.name }}       # Defaults to /home/<username>
    ## I would also be interested on using the "in" condition as long as the "=="
    #when: item.hostname == ansible_hostname
    when: item.1.hostname == ansible_hostname
    #with_items: "{{  students  }}", "{{ hostnames }}"
    loop: "{{ students|product(hostnames)|list }}"

  - import_tasks: restart_lightdm.yml

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .