Creating a new user and password with Ansible

后端 未结 22 1394
迷失自我
迷失自我 2020-12-22 17:00

I have an ansible task which creates a new user on ubuntu 12.04;

- name: Add deployment user
    action: user name=deployer password=mypassword
22条回答
  •  醉梦人生
    2020-12-22 17:26

    The Ansible 'user' module manages users, in the idempotent way. In the playbook below the first task declares state=present for the user. Note that 'register: newuser' in the first action helps the second action to determine if the user is new (newuser.changed==True) or existing (newuser.changed==False), to only generate the password once.

    The Ansible playbook has:

    tasks:
      - name: create deployment user
        user: 
          name: deployer 
          createhome: yes 
          state: present 
        register: newuser
    
      - name: generate random password for user only on creation
        shell: /usr/bin/openssl rand -base64 32 | passwd --stdin deployer
        when: newuser.changed
    

提交回复
热议问题