建立用戶

  1. 撰寫 users.yml 腳本,新增用戶 james 且附屬群組 sharegrp。
    [deyu1@kvm19 ansible]$ vim users.yml
    [deyu1@kvm19 ansible]$ cat users.yml 
    ---
    - hosts: dev
      tasks:
      - name: Ensure group "sharegrp" exists
        group:
          name: sharegrp
          state: present
      - name: Add user 'james'
        user:
          name: james
          groups: sharegrp
    
  2. ansible-playbook 執行腳本 users.yml。
    [deyu1@kvm19 ansible]$ ansible-playbook users.yml 
    
    PLAY [dev] *********************************************************************
    
    TASK [Gathering Facts] *********************************************************
    ok: [kvm11]
    
    TASK [Ensure group "sharegrp" exists] ******************************************
    ok: [kvm11]
    
    TASK [Add user 'james'] ********************************************************
    changed: [kvm11]
    
    PLAY RECAP *********************************************************************
    kvm12 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    
  3. ansible 使用 shell 模組查看主機 dev 的帳號 james。
    [deyu1@kvm19 ansible]$ ansible dev -m shell -a "getent passwd james"
    kvm11 | CHANGED | rc=0 >>
    james:x:1001:1002::/home/james:/bin/bash
    [deyu1@kvm19 ansible]$ ansible dev -m shell -a "id james"
    kvm11 | CHANGED | rc=0 >>
    uid=1001(james) gid=1002(james) groups=1002(james),1001(sharegrp)