Creating a user and setting password only when getent returns a null value from passwd database.

1,869 views
Skip to first unread message

michael....@gmail.com

unread,
Apr 4, 2017, 4:59:53 PM4/4/17
to Ansible Project
i am trying to get this to work, but its skipping over user creation in this current form when getent returns a null answer


 --- 
 - hosts: test
   remote_user: ansible
   become: yes 
   gather_facts: yes 
   vars: 
     user_name: yourusername

   vars_prompt: 
     - name: "encrypted_user_password" 
       prompt: "Enter New Password" 
       private: yes 
       encrypt: "md5_crypt" 
       confirm: yes 
       salt_size: 7 

tasks: 
   - name: Get information about the user 
     getent: 
       key={{user_name}} 
       database=passwd 
       fail_key=true 

 - name: Create user account
   user: name={{ user_name }}
         password={{ encrypted_user_password }}
         state=present
         append=yes
         groups=sudo
         shell="/bin/bash"
         update_password=always
   when: getent_passwd[user_name] != none

michael....@gmail.com

unread,
Apr 4, 2017, 5:02:38 PM4/4/17
to Ansible Project
*Edit* fail_key=false


Kai Stian Olstad

unread,
Apr 5, 2017, 6:05:56 AM4/5/17
to ansible...@googlegroups.com
Here you say run user module when getent_passwd[user_name] is not
none/null, aka the user already exist.
Change it to "==" instead of "!=" and it should work.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages