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