天天看點

使用ansible建立和删除賬号

作者:蒜末半

linux主機增加賬号,其實是很簡單的,useradd指令即可,但如果需要批量增加賬号,那麼就需要一台台登入,這會花費較大精力。當然,你也可以自己使用python、go等新增賬号。最簡單的就是使用ansible來建立賬号了。

新增賬号

ansible all -i inventory.ini -m user -a "name=test1 state=present" --become           

預設使用admin賬号登入,而新增賬号需要使用root,如果上述不增加--become選項,就會報錯

txdocker | FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "msg": "useradd: Permission denied.\nuseradd: cannot lock /etc/passwd; try again later.\n",
    "name": "test",
    "rc": 1
}           

當然,如果賬号是存在的,不加--become也不會報任何錯,因為檢視賬号是有權限的。

使用ansible建立和删除賬号

可以使用tail -1 /etc/passwd這樣的指令去檢視增加的使用者

使用ansible建立和删除賬号

使用uid=,可以指定UID

使用ansible建立和删除賬号

檢查結果如圖:

使用ansible建立和删除賬号

删除賬号,隻要把state=absent即可

使用ansible建立和删除賬号

檢查結果如圖:

使用ansible建立和删除賬号

但是上述并不會删除$HOME目錄,見下圖:

使用ansible建立和删除賬号

如果需要删除$HOME目錄,那麼就要使用remove=yes

ansible all -i inventory.ini -m user -a "name=test state=absent remove=yes" -b

使用ansible建立和删除賬号

核查結果

ansible all -i inventory.ini -m shell -a 'ls -l /home' -b

使用ansible建立和删除賬号

不過需要注意的是,如果先使用state=absent,下一條指令再用remove,目錄是不會删除的。----這個還需要進一步确認

(myenv) ➜  163 git:(master) ✗ a=$(python -c 'import crypt,getpass;pw="123456";print(crypt.crypt(pw))')
(myenv) ➜  163 git:(master) ✗ ansible all -i inventory.ini -m user -a "name=test1  remove=yes uid=10000 password
="$a" update_password=always" -b
txdocker | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 10000,
    "home": "/home/test1",
    "name": "test1",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "stderr": "useradd: warning: the home directory already exists.\nNot copying any file from skel directory into it.\nCreating mailbox file: File exists\n",
    "stderr_lines": [
        "useradd: warning: the home directory already exists.",
        "Not copying any file from skel directory into it.",
        "Creating mailbox file: File exists"
    ],
    "system": false,
    "uid": 10000
}
tx | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 10000,
    "home": "/home/test1",
    "name": "test1",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "stderr": "useradd: warning: the home directory already exists.\nNot copying any file from skel directory into it.\nCreating mailbox file: File exists\n",
    "stderr_lines": [
        "useradd: warning: the home directory already exists.",
        "Not copying any file from skel directory into it.",
        "Creating mailbox file: File exists"
    ],
    "system": false,
    "uid": 10000
}           

測試可以登入

使用ansible建立和删除賬号
(myenv) ➜  163 git:(master) ✗ ansible all -i inventory.ini -m user -a "name=test1 state=absent  remove=yes uid=1
0000 password="$a" update_password=always" -b
txdocker | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "force": false,
    "name": "test1",
    "remove": true,
    "state": "absent"
}
tx | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "test1",
    "remove": true,
    "state": "absent"
}
(myenv) ➜  163 git:(master) ✗ ansible all -i inventory.ini -m shell -a 'ls -l /home' -b
txdocker | CHANGED | rc=0 >>
total 8
drwx------ 10 admin      admin      4096 Jul 12 16:40 admin
drwx------  6 lighthouse lighthouse 4096 Apr  4 21:24 lighthouse
tx | CHANGED | rc=0 >>
total 28
drwx------ 15 admin      admin      4096 Apr 13 17:47 admin
drwxr-xr-x  4 root       root       4096 Mar 15 11:57 jpom
drwxr-xr-x  5 root       root       4096 Mar 15 14:40 jpom-server
drwx------  3 lighthouse lighthouse 4096 May 14  2022 lighthouse
drwxr-xr-x  3 root       root       4096 Mar  8 16:57 server
-rw-r--r--  1 root       root       2908 Mar 15 10:30 wget-log
drwx------  7 zbjavakf   zbjavakf   4096 Apr 24 15:42 zbjavakf
(myenv) ➜  163 git:(master) ✗           

其它的就不一一嘗試了,功能很豐富。

-m:要執行的子產品,預設為command
-a:指定子產品的參數
-u:ssh連接配接的使用者名,預設用root,ansible.cfg中可以配置
-b,--become:變成那個使用者身份,不提示密碼
-k:提示輸入ssh登入密碼,當使用密碼驗證的時候用
-s:sudo運作
-U:sudo到哪個使用者,預設為root
-K:提示輸入sudo密碼,當不是NOPASSWD模式時使用
-C:隻是測試一下會改變什麼内容,不會真正去執行
-c:連接配接類型(default=smart)
-f:fork多少程序并發處理,預設為5個
-i:指定hosts檔案路徑,預設default=/etc/ansible/hosts
-I:指定pattern,對已比對的主機中再過濾一次
-list-host:隻列印有哪些主機會執行這個指令,不會實際執行
-M:要執行的子產品路徑,預設為/usr/share/ansible
-o:壓縮輸出,摘要輸出
--private-key:私鑰路徑
-T:ssh連接配接逾時時間,預設是10秒
-t:日志輸出到該目錄,日志檔案名以主機命名
-v:顯示詳細日志           

ansible中user子產品的參數說明

  • name: 用于指定操作的 user,必須項。
  • uid: 用于指定 user 的 UID,預設為空。
  • non_unique: 與uid參數一起使用,允許改變UID為非唯一值。
  • group: 參數用于指定使用者 主組。預設值為空,為空時建立的使用者組名跟使用者名一緻。
  • groups: 參數用于指定使用者屬組,可以在建立使用者時指定使用者屬組,也可以管理已經存在的使用者屬組。
  • append: 跟groups參數一起使用管理使用者屬組,預設為false,如果 append='yes' ,則從groups參數中增加使用者的屬組;如果 append='no' ,則使用者屬組隻設定為groups中的組,移除其他所有屬組。
  • state: 參數用于指定使用者是否存在于遠端主機中。可選值有 present、absent,預設值為 present。

預設值為 present,表示使用者存在,相當于在遠端主機建立使用者;

當設定為 absent 時表示使用者不存在,相當于在遠端主機删除使用者。

  • remove: 參數在 state=absent 時使用,等價于 userdel --remove 布爾類型,預設值為 false。
  • force: 參數在 state=absent 時使用,等價于 userdel --force,布爾類型,預設值為 false。
  • home: 參數用于指定使用者home目錄,值為路徑
  • create_home: 在使用者建立時或home目錄不存在時為使用者建立home目錄,布爾類型,預設值為 true
  • move_home: 如果設定為yes,結合home= 使用,臨時遷移使用者家目錄到特定目錄
  • comment: 參數用于指定使用者注釋資訊
  • shell: 參數用于指定使用者預設shell
  • system: 參數用于指定使用者是否是系統使用者
  • expires: 參數用于指定使用者過期時間,相當于設定 /etc/shadow 檔案中的的 第8列
  • passwd: 參數用于指定使用者密碼,但是這個密碼不能是明文密碼,而是一個對明文密碼加密後的字元串,預設為空

相當于 /etc/shadow 檔案中的密碼字段,是一個對明文密碼進行哈希後的字元串,可以使用指令生成明文密碼對應的加密字元串

openssl passwd -1 '123456'

openssl passwd -1 -salt 'abcdefg' '123456'

  • password_lock: 參數用于鎖定指定使用者,布爾類型,預設為空
  • update_password: 參數可選值有always 和 on_create,預設為always 。

    當設定為always時,password參數的值與 /etc/shadow 中密碼字元串不一緻時更新使用者的密碼;

    當設定為on_create時,password參數的值與 /etc/shadow 中密碼字元串不一緻時也不會更新使用者的密碼,但如果是新建立的使用者,則此參數即使為on_create,也會更新使用者密碼。

  • generate_ssh_key: 參數用于指定是否生成ssh密鑰對,布爾類型,預設為false。當設定為yes時,為使用者生成 ssh 密鑰對,預設在 ~/.ssh 目錄中生成名為 id_rsa私鑰 和 id_rsa.pub公鑰,如果同名密鑰已經存在,則不做任何操作。
  • sssh_key_bits: 當 generate_ssh_key=yes 時,指定生成的ssh key加密位數。
  • ssh_key_file: 當 generate_ssh_key=yes 時,使用此參數指定ssh私鑰的路徑及名稱,會在同路徑下生成以私鑰名開頭以 .pub 結尾對應公鑰。
  • ssh_key_comment: 當 generate_ssh_key=yes 時,在建立證書時,使用此參數設定公鑰中的注釋資訊。如果同名密鑰已經存在,則不做任何操作。當不指定此參數時,預設注釋資訊為"ansible-generated on $hostname”。
  • ssh_key_passphrase: 當 generate_ssh_key=yes 時,在建立證書時,使用此參數設定私鑰密碼。如果同名密鑰已經存在,則不做任何操作。
  • ssh_key_type: 當 generate_ssh_key=yes 時,在建立證書時,使用此參數指定密鑰對的類型。預設值為 rsa,如果同名密鑰已經存在,則不做任何操作。

繼續閱讀