Simple ansible script Error

I have errors Could You help me :smiley:

  • 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

I have errors Could You help me :smiley:

- 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}}'

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.

        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'

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

      with_items:'{{groups}}'

You are missing a space after colon.

    - 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'

Remove the curly brackets.

      with_items:'{{users}}'

Missing space after colon.

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}}’

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}}’

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