1.5 Create Files on App Servers using Ansible || level 1 -- Task 5

Question:

The Nautilus DevOps team is testing various Ansible modules on servers in Stratos DC. They’re currently focusing on file creation on remote hosts using Ansible. Here are the details:

a. Create an inventory file ~/playbook/inventory on jump host and include all app servers.

b. Create a playbook ~/playbook/playbook.yml to create a blank file /opt/code.txt on all app servers.

c. Set the permissions of the /opt/code.txt file to 0755.

d. Ensure the user/group owner of the /opt/code.txt file is tony on app server 1, steve on app server 2 and banner on app server 3

Playbook Solution:


  • name: Create /opt/code.txt file on all app servers with specific ownership and permissions
    hosts: app_servers
    become: yes # escalate privilege for all tasks

    vars:
    file_owner: >
    {% if inventory_hostname == ‘stapp01’ %}
    tony
    {% elif inventory_hostname == ‘stapp02’ %}
    steve
    {% elif inventory_hostname == ‘stapp03’ %}
    banner
    {% else %}
    root
    {% endif %}

    tasks:

    • name: Create blank file /opt/code.txt with correct permissions and ownership
      file:
      path: /opt/code.txt
      state: touch
      mode: ‘0755’
      owner: “{{ file_owner | trim }}”
      group: “{{ file_owner | trim }}”

Hi @rknethinti

You have not created an inventory file to include all the servers.
As the task requires to set user/group to the file to that of user of the respective server, you can use ansible_user special variable.