Help with Variables and command/shell module

19 views
Skip to first unread message

Robert

unread,
Dec 10, 2018, 9:33:17 AM12/10/18
to ansible...@googlegroups.com
All,

I have a playbook that reads in a vars_file and creates new users and then runs the chage command to force them to change their password on first/next login.  Here is the playbook.

+++++++++++++++++++++++++++
---
- hosts: my-test-centos
  become: yes
  vars_files:
    - /home/rkruck/ansible-playbooks/users.yml
  tasks:
    - name:  Adding users from user.yml file
      user:
        name: "{{ item.name }}"
        state: present
        comment: "{{ item.comment }}"
        uid: "{{ item.uid }}"
        password: $6$QAnwjCYOtTaLJkHE$k3tKqKT0B/mFugYNBjuhY7RCVU/AwvW00wTg6p/66Gp/MoWdN2QfveJxGmsdho5Hn.xduUVCuLiXHPeZDod/o1
        update_password: on_create
        createhome: yes
      with_items: "{{ users }}"

    - name: Force password change for user test-user
        command: "chage -d 0 {{ item.name }}"
      with_items: "{{ users }}"

+++++++++++++++++++++++++++++++
The first task works fine and creates the users in the user.yml file.  However, when it gets to the password force change I get the following error:

ERROR! Syntax Error while loading YAML.
  mapping values are not allowed in this context

The error appears to have been in '/home/rkruck/ansible-playbooks/test.yml': line 19, column 16, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

    - name: Force password change for user test-user
        command: "chage -d 0 {{ item.name }}"
               ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

I'm confused as to what I am doing wrong.

Any help is appreciated

--Robert

Kai Stian Olstad

unread,
Dec 10, 2018, 9:58:17 AM12/10/18
to ansible...@googlegroups.com
On Monday, 10 December 2018 15:32:53 CET Robert wrote:
>
> - name: Force password change for user test-user
> command: "chage -d 0 {{ item.name }}"
> with_items: "{{ users }}"
>
> +++++++++++++++++++++++++++++++
> The first task works fine and creates the users in the user.yml file.
> However, when it gets to the password force change I get the following
> error:
>
> ERROR! Syntax Error while loading YAML.
> mapping values are not allowed in this context
>
> The error appears to have been in
> '/home/rkruck/ansible-playbooks/test.yml': line 19, column 16, but may
> be elsewhere in the file depending on the exact syntax problem.
>
> The offending line appears to be:
>
> - name: Force password change for user test-user
> command: "chage -d 0 {{ item.name }}"
> ^ here

You have indented command to far, remove two spaces so it align at the same level as name and with_items.

--
Kai Stian Olstad


Robert

unread,
Dec 10, 2018, 10:08:42 AM12/10/18
to ansible...@googlegroups.com
Thanks I feel utterly stupid right now....that fixed the problem.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2875697.5IrcR943cM%40x1.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages