RHCE8 练习题:修改文件内容

按照下方所述,创建名为/home/student/ansible/issue.yml的playbook:

  • 该playbook将在所有清单主机上运行
  • 该playbook会将 /etc/issue 的内容替换为下方所示的一行文本:
    • 在 dev 主机组中的主机上,这行文本显示为: Development
    • 在 test 主机组中的主机上,这行文本显示为: Test
    • 在 prod 主机组中的主机上,这行文本显示为: Production

Answer:

vim /home/student/ansible/issue.yml
---
- name: modify issue
  hosts: all 
  tasks:
    - name: modify issue
      copy:
        content: 'Development'
        dest: /etc/issue
      when: inventory_host in groups.dev
    - name: modify issue
      copy:
        content: 'Test'
        dest: /etc/issue
      when: inventory_host in groups.test
    - name: modify issue
      copy:
        content: 'Production'
        dest: /etc/issue
      when: inventory_host in groups.prod
ansible-playbook issue.yml

验证

ansible all -a 'cat /etc/issue'

file