ansible user模块

ansible user模块

  • 几乎与命令/bin/sh相同,但通过远程节点上的shell运行命令

用户管理模块

image

修改密码

更改单个用户密码

---
- hosts: test
  gather_facts: false
  tasks:
  - name: Change password
    user: name={{ name1 }}  password={{ chpass | password_hash('sha512') }}  update_password=always

执行:

ansible-playbook userpass2.yaml -e "name1=jacky chpass=jacky@123" 

修改多个用户密码

//修改主机,账号,密码
---
- hosts: zksit
  gather_facts: false
  tasks:
  - name: change user passwd
    user: name={{ item.name }} password={{ item.chpass | password_hash('sha512') }}  update_password=always
    with_items:
     - { name: 'jacky', chpass: 'jacky@123' }

执行

//指定host_all机器列表信息,执行yml模版
ansible-playbook -i host_all userpass.yaml

创建用户

创建属于多个组的用户

---
 - hosts: host_all
   gather_facts: no
   tasks:
   - name: add group
     group:
       name: lwc
       state: present
   - name: Add a new user
     user:
       name: lwc
       commpent: "用户的描述"
	   password: "{{ '123456'|password_hash('sha512') }}"
       groups: lwc,root
       append: yes
       shell: /bin/bash

创建用户的时候创建密钥对

---
- name: 创建用户
  hosts: all
  tasks:
    - name: 创建用户的密钥对
      user:
      	name: user1
        generate_ssh_key: yes
        ssh_key_bits: 2040
        ssh_key_file: .ssh/id_my_rsa

删除用户

批量删除用户

---
- hosts: all
  tasks:
    - name: 批量删除用户
      user:
        name: "{{item.name}}"
        state: absent
        remove: yes
      with_items:
        - {name: "omaidb"}
        - {name: "jemes"}
        - {name: "xiaoming"}
posted @ 2023-03-08 19:55  liwenchao1995  阅读(155)  评论(0编辑  收藏  举报