ansible user模塊


查看模塊的功能和選項,使用ansible-doc命令

ansible-doc  
options:
  -l #查看所有可用的模塊
  -m #查看模塊的路徑
  -v #查看版本
  -t TYPE #查看插件,插件:'become', 'cache','callback', 'cliconf', 'connection', 'httpapi','inventory', 'lookup', 'shell', 'module', 'strategy','vars'
  -s #以簡短格式顯示用法
eg:
#ansible-doc user -s
name:                  # (required) Name of the user to create, remove or modify #這種帶有required字樣的表示為必須選項

 

user模塊

options:

 

示例:

場景1、新增用戶。

需求描述:新增用戶dba,使用BASH Shell,附加組為admins,dbagroup,家目錄為/home/dba,注意:附加組必須為已經存在的組。

掌握技能點:

(1)groups設定,groups=group1,group2.。。。

(2)增量添加屬組,append=yes

(3)狀態,state=present

(4)家目錄:home=/home/dba

(5)shell:shell=/bash/shell

//創建用戶
#ansible hadoop -m user -a "name=dba groups=admins,dbagroup append=yes home=/home/dba shell=/bash/shell state=present" 192.168.4.50 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 1014, "groups": "admins,dbagroup", "home": "/home/dba", "name": "dba", "shell": "/bash/shell", "state": "present", "system": false, "uid": 1012 }
//查看信息

#ansible hadoop -m shell -a "id dba"
192.168.4.50 | CHANGED | rc=0 >>
uid=1012(dba) gid=1014(dba) groups=1014(dba),1012(admins),1013(dbagroup)

 

場景2、修改用戶屬組。

需求描述:修改dba用戶附加組為dbagroups(既刪除admins組)

掌握技能:

  全量變更組屬性:append=no(默認就是no)

//執行命令
#ansible hadoop -m user -a "name=dba groups=dbagroup" 192.168.4.50 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "append": false, "changed": true, "comment": "", "group": 1014, "groups": "dbagroup", "home": "/home/dba", "move_home": false, "name": "dba", "shell": "/bash/shell", "state": "present", "uid": 1012 }
//查看信息
#ansible hadoop -m shell -a "id dba" 192.168.4.50 | CHANGED | rc=0 >> uid=1012(dba) gid=1014(dba) groups=1014(dba),1013(dbagroup)

場景3、設置dba用戶過期時間

ansible hadoop -m user -a "name=dba expire=1591113600"  #過期時間設置為2020-06-03

場景4、刪除用戶

ansible hadoop -m user -a "name=dba remove=yes state=absent"   #remove相當於Linux下刪除命令時帶的remove參數,表示同時刪除家目錄和郵件。

場景5、更新用戶密碼。

ansible hadoop -m user -a "name=dba password=$6$GD8Q update_password=always"   #password 后面接的是加密以后的密碼。

對密碼加密可以使用python的crypt和passlib,passlib需要安裝 pip install passlib

進入到python的交互式里面
第一種: import crypt crypt.crypt(
"密碼")
第二種:
from passlib.hash import sha512_crypt
sha512_crypt.hash("密碼")

使用playboox創建用戶

---
- hosts: hadoop
  remote_user: root
  vars_prompt:
    - name: user_name
      prompt: Enter Username
      private: no
    - name: user_passwd
      prompt: Enter Password
      encrypt: "sha512_crypt"
      confirm: yes
  tasks:
    - name: create user
      user:
        name: "{{user_name}}"
        password: "{{user_passwd}}"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM