Parvinder Raheja
Parvinder Raheja

Reputation: 55

Unable to bulk change user password with Ansible

I am trying to change password for bulk users. The script does not work when the username(s) is provided via vars. I am getting the following error ERROR! Syntax Error while loading YAML. found unacceptable key (unhashable type: 'AnsibleMapping')

here is the script

- name: Set Password
  hosts: psr
  become: yes
  ignore_errors: yes
  vars:
    users:
     - test
  tasks:
  - name: Check if user exists
    shell: id -u {{ users }}
    register: user_exists
    ignore_errors: true

  - name: Change Password
    user:
     name: {{ users }}
     password: "$1$Du3HGfHV$ny91hdJz81y.NtKw/"
     update_password: always
    when: user_exists.rc == 0

Upvotes: 2

Views: 3891

Answers (1)

error404
error404

Reputation: 2823

Use below. with - you have actually defined the users as list instead of vars I have removed the - prefix from the variable test. Also the lines between " " is treated as single command so always define commands between " ".

Removed the checking of the user name as it is not needed the name module has an attribute named state that will check and perform the action declared in the state.

- name: Set Password
  hosts: localhost
  become: yes
  ignore_errors: yes
  vars:
    users:
      test
  tasks:
    - name: Change Password
      user:
        name: "{{ users }}"
        password: "$1$Du3HGfHV$ny91hdJz81y.NtKw/"
        update_password: always
        state: present

Upvotes: 3

Related Questions