*Shell 命令

  1. 切換工作目錄到 /home/deyu1/ansible。
    [deyu1@kvm19 ~]$ cd ansible/
    
  2. 執行 ansible ad-hoc 命令,將群組 dev 的主機重開機,出現錯誤。
    [deyu1@kvm19 ansible]$ ansible dev -a "/sbin/reboot"
    kvm11 | FAILED | rc=1 >>
    Failed to set wall message, ignoring: Interactive authentication required.
    Failed to reboot system via logind: Interactive authentication required.
    Failed to open initctl fifo: Permission denied
    Failed to talk to init daemon.non-zero return code
    
  3. 執行 ansible ad-hoc 命令,sudo 將群組 dev 的主機重開機,成功執行,出現的錯誤訊息是因關機時 ssh 無法連線。
    [deyu1@kvm19 ansible]$ ansible dev -a "sudo /sbin/reboot"
    kvm11 | UNREACHABLE! => {
        "changed": false,
        "msg": "Failed to connect to the host via ssh: Shared connection to 
    kvm11 closed.",
        "unreachable": true
    }
    
  4. 群組 prod 有 kvm13 及 kvm14 兩台主機都成功重開機,出現的錯誤訊息是因關機時 ssh 無法連線。
    [deyu1@kvm19 ansible]$ ansible prod -a "sudo /sbin/reboot"
    kvm13 | FAILED | rc=-1 >>
    Failed to connect to the host via ssh: ssh: connect to host kvm13
     port 22: Connection refused
    kvm14 | FAILED | rc=-1 >>
    Failed to connect to the host via ssh: ssh: connect to host kvm14
     port 22: Connection refused
    
  5. ansible 執行時會同時開啟多個 bash 子程式同步執行,預設數量是 5。
    [deyu1@kvm19 ansible]$ grep forks /etc/ansible/ansible.cfg 
    #forks          = 5
    
  6. 如果群組的主機很多,可使用選項 -f 指定 bash 子程式數量同時執行,以下是同時開啟 10 個 bash,將受管主機重開機。
    [deyu1@kvm19 ansible]$ ansible prod -a "sudo /sbin/reboot" -f 10
    
  7. -u root 指定以用戶 root 執行命令,因為 deyu1 無法免密碼 ssh 連線 kvm11 的 root 用戶,所以出現無法連線的訊息。
    [deyu1@kvm19 ansible]$ ansible dev -a "/sbin/reboot" -u root
    kvm11 | UNREACHABLE! => {
        "changed": false,
        "msg": "Failed to connect to the host via ssh: root@kvm11:
     Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
        "unreachable": true
    }
    
  8. --ask-pass 要求輸入連線密碼,輸入後成功重新啟動 kvm11,出現的訊息一樣是因關機 ssh 連線中斷。
    [deyu1@kvm19 ansible]$ ansible dev -a "/sbin/reboot" -u root --ask-pass
    SSH password: 
    kvm11 | FAILED | rc=-1 >>
    Failed to connect to the host via ssh: ssh: connect to host kvm11
     port 22: Connection reset by peer