1
votes

I am new to Ansible and not very versed to Linux in general so this is probably quite basic question but it makes me scratch my head quite for some time. Debugging with several levels of verbosity, Googling for problem nor study of several tutorials didn´t provide an answer nor hint what is wrong. But to my issue:

I tryied to use roles for reusability. I manually made folders tasks and roles directly under /etc/ansible/roles on my DEV machine. But after decomposing original playbook into role it stared failing with error The task includes an option with an undefined variable. The error was: 'groups_additional' is undefined.

My decomposed files:

playbook test_user.yml

---
- hosts: test_host
  remote_user: test
  gather_facts: no

  roles:
    - user

  vars:
    user_name: test
    user_pwd: <secret>
    group_name: test_grp

roles/user/tasks/main.yml ############ Error happens here in groups

---
- name: Check '{{ user_name }}' user, set his pwd and groups
  ansible.builtin.user:
    name: '{{ user_name }}'
    password: '{{ user_pwd }}'
    group: '{{ user_name if group_name is undefined else group_name }}'
    groups: '{{ user_name if group_name is undefined else group_name }} {{ groups_additional if groups_additional is defined }}'      
  become: yes  #use sudo

roles/user/vars/main.yml

---
groups_additional: ', qwqe'

Please, note that this is really simplified version of my playbook but with important places presented.

1

1 Answers

1
votes

TL; DR: Check owner and permissions


Disclaimer: This is my own question and I am answering so another unhappy person might get hint what might get wrong.

The reason is relatively simple, yet somewhat hidden.

After running playbook under sudo it played along without any problem. Then I noticed that vars folder was owned by root, but tasks by user usr_ansible (under which I was running Ansible playooks). After change of owner + group it is okay and ansible-playbook works even without sudo.