Skip to main content

Command Palette

Search for a command to run...

5.Create Files on App Servers using Ansible

Updated
โ€ข2 min read
5.Create Files on App Servers using Ansible
T

Cloud & SRE specializing in AWS and DevOps. I share my ongoing learning journey through practical tutorials and insights. Let's grow together.

Lab Information

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 /home/opt.txt on all app servers.

c. Set the permissions of the /home/opt.txt file to 0744.

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

Note: Validation will execute the playbook using the command ansible-playbook -i inventory playbook.yml, so ensure the playbook functions correctly without any additional arguments.

Lab Instructions

Step-by-Step Ansible Lab Instructions

๐ŸŸฉ Step 1 โ€” Create the playbook directory

mkdir -p ~/playbook

๐ŸŸฉ Step 2 โ€” Create the inventory file

vi ~/playbook/inventory

Add the following content:

[app_servers]
stapp01 ansible_user=tony ansible_password=Ir0nM@n owner_name=tony
stapp02 ansible_user=steve ansible_password=Am3ric@ owner_name=steve
stapp03 ansible_user=banner ansible_password=BigGr33n owner_name=banner

โœ” This defines all app servers โœ” It also maps each host to the correct owner

๐ŸŸฉ Step 3 โ€” Create the playbook file

vi ~/playbook/playbook.yml

Add the following:

---
- name: Create /home/opt.txt on all app servers
  hosts: app_servers
  become: yes

  tasks:
    - name: Ensure /home/opt.txt exists with correct permissions and ownership
      file:
        path: /home/opt.txt
        state: touch
        mode: "0744"
        owner: "{{ owner_name }}"
        group: "{{ owner_name }}"

๐ŸŸฉ Step 4 โ€” Verify your files Inventory: cat ~/playbook/inventory

Playbook: cat ~/playbook/playbook.yml

๐ŸŸฉ Step 5 โ€” Run the playbook (Validation will do this automatically)

ansible-playbook -i inventory playbook.yml

This will:

โœ” Create /home/opt.txt on all app servers โœ” Set file permissions to 0744 โœ” Assign correct owner/group per server:

stapp01 โ†’ tony

stapp02 โ†’ steve

stapp03 โ†’ banner

Image description


Resources & Next Steps
๐Ÿ“ฆ Full Code Repository: KodeKloud Learning Labs
๐Ÿ“– More Deep Dives: Whispering Cloud Insights - Read other technical articles
๐Ÿ’ฌ Join Discussion: DEV Community - Share your thoughts and questions
๐Ÿ’ผ Let's Connect: LinkedIn - I'd love to connect with you

Credits
โ€ข All labs are from: KodeKloud
โ€ข I sincerely appreciate your provision of these valuable resources.

More from this blog

W

Whispering Cloud Insights

88 posts

Documenting my path to cloud journey. Sharing lessons, tutorials, and insights from my continuous learning journey. Learn with me.