修改檔案–單一群組

  1. 撰寫 shell1.yml 腳本,對 dev 主機的 /etc/issue,修改成特定的字串。
    [deyu1@kvm19 ansible]$ vim shell1.yml
    [deyu1@kvm19 ansible]$ cat shell1.yml 
    ---
    - hosts: all
      tasks:
        - shell: 
            "echo 'Development {{inventory_hostname}}' > /etc/issue"
          when: '"dev" in group_names'
    
  2. ansible-playbook 執行腳本 shell1.yml。
    [deyu1@kvm19 ansible]$ ansible-playbook shell1.yml 
    
    PLAY [all] *********************************************************************
    
    TASK [Gathering Facts] *********************************************************
    ok: [kvm15]
    ok: [kvm11]
    ok: [kvm14]
    ok: [kvm13]
    ok: [kvm12]
    
    TASK [shell] *******************************************************************
    skipping: [kvm12]
    skipping: [kvm15]
    skipping: [kvm13]
    skipping: [kvm14]
    changed: [kvm11]
    
    PLAY RECAP *********************************************************************
    kvm11 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    kvm12 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    kvm13 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    kvm14 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    kvm15 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    
  3. ansible 使用 shell 模組查看主機 dev 的 /etc/issue 檔案內容,符合要求。
    [deyu1@kvm19 ansible]$ ansible dev -m shell -a "cat /etc/issue"
    kvm11 | CHANGED | rc=0 >>
    Development kvm11