Red_Hat
EX294 · Question #11
11. Modifying File Content Create a playbook named /home/greg/ansible/issue.yml according to the following instructions: - This playbook will run on all inventory hosts. - The playbook will replace…
This playbook modifies the /etc/issue file on all inventory hosts, setting its content conditionally based on the host's group membership.
Submitted by priya_blr· Apr 18, 2026Manage Variables, Facts, and Content
Question
- Modifying File Content Create a playbook named /home/greg/ansible/issue.yml according to the following instructions: - This playbook will run on all inventory hosts. - The playbook will replace the content of /etc/issue with a single line of text as shown below: - On hosts in the dev host group, this line of text will display as: Development - On hosts in the test host group, this line of text will display as: Test - On hosts in the prod host group, this line of text will display as: Production Correct Answer: See the below explanation Explanation Explanation/Reference: Solution: [greg@control ansible]$ ansible dev -m debug -a 'var=inventory_hostname' [greg@control ansible]$ ansible dev -m debug -a 'var=groups' [greg@control ansible]$ ansible dev -m debug -a 'var=groups.dev' [greg@control ansible]$ ansible-doc copy [greg@control ansible]$ ansible-doc stat [greg@control ansible]$ vim /home/greg/ansible/issue.yml --- - name: modify file hosts: all tasks: - name: Content 1 ansible.builtin.copy: content: 'Development' dest: /etc/issue when: inventory_hostname in groups.dev - name: Content 2 ansible.builtin.copy: content: 'Test' dest: /etc/issue when: inventory_hostname in groups.test - name: Content 3 ansible.builtin.copy: content: 'Production' dest: /etc/issue when: inventory_hostname in groups.prod [greg@control ansible]$ ansible-navigator run issue.yml -m stdout # Verification (mandatory operation) [greg@control ansible]$ ansible dev -a 'cat /etc/issue' [greg@control ansible]$ ansible test -a 'cat /etc/issue' [greg@control ansible]$ ansible prod -a 'cat /etc/issue' This playbook will run on managed nodes in the dev host group. Create a directory /webdev with the following requirements: Owner is webdev group. Regular permissions: Owner: read+write+execute, Group: read+write+execute, Other: read+execute Special permissions: Set Group ID Create a symbolic link /var/www/html/webdev pointing to /webdev Create a file /webdev/index.html with a single-line content as shown below: Development Correct Answer: See the below explanation Explanation Explanation/Reference: Solution: [greg@control ansible]$ ansible-doc file [greg@control ansible]$ ansible-doc copy [greg@control ansible]$ ansible dev -a 'ls -ldZ /var/www/html' -rw-r--r--. 1 root root system_u:object_r:
httpd_sys_content_t:s0 11 Apr 14 07:11 /var/www/html [greg@control ansible]$ vim /home/greg/ansible/webcontent.yml - name: Create Web Directory hosts: dev roles: - apache tasks: - name: Create directory ansible.builtin.file: path: /webdev state: directory group: webdev mode: '2775' - name: Create link ansible.builtin.file: src: /webdev dest: /var/www/html/webdev state: link - name: Copy content ansible.builtin.copy: content: 'Development' dest: /webdev/index.html setype: httpd_sys_content_t [greg@control ansible]$ ansible-navigator run webcontent.yml -m stdout
Explanation
This playbook modifies the /etc/issue file on all inventory hosts, setting its content conditionally based on the host's group membership.
Concept tested. Conditional file content modification
Reference. https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html
Topics
#Playbooks#Conditionals#File Management#Host Groups
Community Discussion
No community discussion yet for this question.