# Simple ansible script Error

**URL:** <https://forum.ansible.com/t/simple-ansible-script-error/25385>\
**Category:** Ansible Project\
**Created:** [August 31, 2017, 2:17pm UTC](https://forum.ansible.com/t/simple-ansible-script-error/25385 "2017-08-31T14:17:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Konrad\_Kosewski](https://avatars.discourse-cdn.com/v4/letter/k/59ef9b/32.png) [@Konrad\_Kosewski](https://forum.ansible.com/u/Konrad_Kosewski)\
**Post date:** [August 31, 2017, 2:17pm UTC](https://forum.ansible.com/t/simple-ansible-script-error/25385/1 "2017-08-31T14:17:06Z")

</div>

I have errors Could You help me 😃

- hosts: “{{ variable\_hosts | default(‘all’) }}”  
remote\_user: ansible  
become: yes  
tasks:

- include\_vars: users.yml

- name: Make sure we have a {{ item.groupname }} group  
group:  
name:{{item.groupname}} with\_items: ‘{{groups}}’  
state: present

- name: Allow {{ item.groupname }} to have passwordless sudo  
lineinfile:  
dest: /etc/sudoers  
state: present  
regexp: ‘^%{{ item.groupname }}’  
line: ‘%{{ item.groupname }} ALL=(ALL) NOPASSWD: ALL’  
when: ‘{{ item.use\_sudo }} == True’  
with\_items:‘{{groups}}’

- name: Add sudoers users to wheel group  
user:  
name: “{{ item.username }}”  
groups: ‘jtendo’  
append: yes  
state: present  
createhome: yes  
system: yes  
comment: “Ansible Automation Tool”  
with\_items: ‘{{users}}’

- name: Sudoers or update sudoers file and validate  
lineinfile: “dest=/etc/sudoers  
insertafter=EOF  
line=‘{{ item.username }} ALL=(ALL) NOPASSWD: ALL’  
regexp=‘^{{ item.username }} .\*’  
state=present”  
when: ‘{{ item.use\_sudo }} == True’  
with\_items:‘{{users}}’

where users.yml

groups:

- groupname: aenndo  
use\_sudo: no  
users:
- username: ertzpi7  
use\_sudo: no

Syntax Error while loading YAML.

The error appears to have been in ‘/home/ansible/tasks/create\_users’: line 20, column 5, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: Add sudoers users to wheel group  
^ here

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 31, 2017, 3:34pm UTC](https://forum.ansible.com/t/simple-ansible-script-error/25385/2 "2017-08-31T15:34:32Z")

</div>

> I have errors Could You help me 😃
> 
> - hosts: "{{ variable\_hosts | default('all') }}"  
> &nbsp;&nbsp;remote\_user: ansible  
> &nbsp;&nbsp;become: yes  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- include\_vars: users.yml  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Make sure we have a {{ item.groupname }} group  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name:{{item.groupname}} with\_items: '{{groups}}'

You need a space after the colon and you are missing quotes around the  
variable.  
with\_items must be on it own line and indented the same level as group:,  
with\_items is a task parameter and not a group module parameter.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Allow {{ item.groupname }} to have passwordless sudo  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /etc/sudoers  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^%{{ item.groupname }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: '%{{ item.groupname }} ALL=(ALL) NOPASSWD: ALL'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: '{{ item.use\_sudo }} == True'

You can't use curly brackets in when, just remove them since they are implied  
in when:.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:'{{groups}}'

You are missing a space after colon.

> &nbsp;&nbsp;&nbsp;&nbsp;- name: Add sudoers users to wheel group  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item.username }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups: 'jtendo'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;append: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;createhome: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;comment: "Ansible Automation Tool"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: '{{users}}'
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Sudoers or update sudoers file and validate  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lineinfile: "dest=/etc/sudoers  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insertafter=EOF  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line='{{ item.username }} ALL=(ALL) NOPASSWD: ALL'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp='^{{ item.username }} .\*'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state=present"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: '{{ item.use\_sudo }} == True'

Remove the curly brackets.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:'{{users}}'

Missing space after colon.

---

<div class="post-metadata">

**Author:** ![Konrad\_Kosewski](https://avatars.discourse-cdn.com/v4/letter/k/59ef9b/32.png) [@Konrad\_Kosewski](https://forum.ansible.com/u/Konrad_Kosewski)\
**Post date:** [September 1, 2017, 6:55am UTC](https://forum.ansible.com/t/simple-ansible-script-error/25385/3 "2017-09-01T06:55:20Z")

</div>

Thank You very much, but i still have problems with my first project:

- hosts: “{{ variable\_hosts | default(‘all’) }}”  
remote\_user: ansible  
become: yes  
tasks:

- include\_vars: users.yml

- name: Make sure we have a group existing  
group:  
name: “{{item.groupname}}”  
state: present  
with\_items: ‘{{group\_list}}’

- name: Allow {{ item.groupname }} to have passwordless sudo  
lineinfile:  
dest: /etc/sudoers  
state: present  
regexp: ‘^%{{ item.groupname }}’  
line: ‘%{{ item.groupname }} ALL=(ALL) NOPASSWD: ALL’  
when: ’ item.use\_sudo == True’  
with\_items: ‘{{group\_list}}’

- name: Add sudoers users to wheel group  
user:  
name: “{{ item.username }}”  
groups: ‘jtendo’  
append: yes  
state: present  
createhome: yes  
system: yes  
comment: “Ansible Automation Tool”  
with\_items: ‘{{user\_list}}’

- name: Sudoers or update sudoers file and validate  
lineinfile: “dest=/etc/sudoers  
insertafter=EOF  
line=‘{{ item.username }} ALL=(ALL) NOPASSWD: ALL’  
regexp=‘^{{ item.username }} .\*’  
state=present”  
when: ’ item.use\_sudo == True’  
with\_items: ‘{{users\_list}}’

[ansible@repo tasks]$ vi users.yml  
group\_list:

- groupname: aaadndo  
use\_sudo: no  
user\_list:

- username: jdsadsazpi7  
use\_sudo: no

- username: r123mat  
use\_sudo: no

When I try run:

TASK [Make sure we have a group existing] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [ommp-internal]: FAILED! =\> {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appeavariable that is undefined. The error was: ‘ansible.vars.unsafe\_proxy.AnsibleUnsafeText object’ has no attribu\nThe error appears to have been in ‘/home/ansible/tasks/create\_users’: line 6, column 7, but may\nbe elsewherpending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - include\_vars: users.yml\n ure we have a group existing\n ^ here\n”}

[ansible@repo tasks]$ ansible-lint create\_users  
[ANSIBLE0011] All tasks should be named  
create\_users:5  
Task/Handler: include\_vars users.yml

[ANSIBLE0002] Trailing whitespace  
create\_users:8  
name: “{{item.groupname}}”

[ANSIBLE0002] Trailing whitespace  
create\_users:39  
with\_items: ‘{{users\_list}}’

---

<div class="post-metadata">

**Author:** ![Konrad\_Kosewski](https://avatars.discourse-cdn.com/v4/letter/k/59ef9b/32.png) [@Konrad\_Kosewski](https://forum.ansible.com/u/Konrad_Kosewski)\
**Post date:** [September 1, 2017, 7:05am UTC](https://forum.ansible.com/t/simple-ansible-script-error/25385/4 "2017-09-01T07:05:56Z")

</div>

OK it works now:

[ansible@repo tasks]$ vi create\_users

- hosts: “{{ variable\_hosts | default(‘all’) }}”  
remote\_user: ansible  
become: yes  
tasks:

- name: Loading input file users  
include\_vars:  
file: users.yml

- name: Make sure we have a group existing  
group:  
name: “{{item.groupname}}”  
state: present  
with\_items: ‘{{group\_list}}’

- name: Allow {{ item.groupname }} to have passwordless sudo  
lineinfile:  
dest: /etc/sudoers  
state: present  
regexp: ‘^%{{ item.groupname }}’  
line: ‘%{{ item.groupname }} ALL=(ALL) NOPASSWD: ALL’  
when: ’ item.use\_sudo == True’  
with\_items: ‘{{group\_list}}’

- name: Add sudoers users to wheel group  
user:  
name: “{{ item.username }}”  
groups: ‘jtendo’  
append: yes  
state: present  
createhome: yes  
system: yes  
comment: “Ansible Automation Tool”  
with\_items: ‘{{user\_list}}’

- name: Sudoers or update sudoers file and validate  
lineinfile: “dest=/etc/sudoers  
insertafter=EOF  
line=‘{{ item.username }} ALL=(ALL) NOPASSWD: ALL’  
regexp=‘^{{ item.username }} .\*’  
state=present”  
when: ’ item.use\_sudo == True’  
with\_items: ‘{{user\_list}}’

---

<div class="post-metadata">

**Author:** ![Konrad\_Kosewski](https://avatars.discourse-cdn.com/v4/letter/k/59ef9b/32.png) [@Konrad\_Kosewski](https://forum.ansible.com/u/Konrad_Kosewski)\
**Post date:** [September 1, 2017, 9:15am UTC](https://forum.ansible.com/t/simple-ansible-script-error/25385/5 "2017-09-01T09:15:38Z")

</div>

Still have problem: How it should look like?

…

- name: Add users to group  
user:  
name: “{{ item.0.username }}”  
groups: “{{ item.1.groupname }}”  
append: yes  
state: present  
createhome: yes  
system: yes  
comment: “Ansible Automation Tool”  
with\_subelements:
- ‘{{group\_list}}’
- ‘{{user\_list}}’  
…

where

[ansible@repo tasks]$ cat users.yml  
group\_list:

- groupname: avho  
use\_sudo: no  
user\_list:

- username: jdsdi7  
use\_sudo: no

- username: raaafsmat  
use\_sudo: no
