Ansible File Module

Ticker

6/recent/ticker-posts

Ansible File Module

Question  a. Create an inventory file ~/playbook/inventory on jump host and add all app servers in it.

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

c. The /home/web.txt file permission must be 0755.

d. The user/group owner of file /home/web.txt must be tony on app server 1, steve on app server 2 and banner on app server 3.



Please Note :-  Perform the below commands based on your question  server,  user name & other details might differ . So please read task carefully before executing. All the Best 👍

Solution:  

1Go through the folder mentioned in task and verified the playbook 

    ls -l /home/thor/playbook/


2.  Create a inventory file and add the app server's as per task.

stapp01 ansible_host=172.16.238.10 ansible_ssh_pass=Ir0nM@n  ansible_user=tony
stapp02 ansible_host=172.16.238.11 ansible_ssh_pass=Am3ric@ ansible_user=steve
      stapp03 ansible_host=172.16.238.12 ansible_ssh_pass=BigGr33n     ansible_user=banner

3.  Create a playbook as per the task  ( refer Video below for clarity )

- name: Create file in appservers

  hosts: stapp01, stapp02, stapp03

  become: yes

  tasks:

    - name: Create the file and set properties

      file:

        path: /home/web.txt

        owner: "{{ ansible_user }}"

        group: "{{ ansible_user }}"

        mode: "0755"

        state: touch


4.Post file saved , run below command to execute the playbook

        ansible-playbook  -i inventory playbook.yml



6. validate the task by running the below command
      ansible all -a "ls -lsd /home/web.txt" -i inventory


7. Click on Finish & Confirm to complete the task successful

Happy Learning!!!!


Apart from this if you need more clarity,  I have made a  tutorial video on this , please go through and share your comments. Like and share the knowledge






Post a Comment

2 Comments

  1. Hi,

    What's the purpose of "become: yes"? referring to https://docs.ansible.com/ansible/2.3/become.html, "become
    set to ‘true’/’yes’ to activate privilege escalation".

    So my understanding it acts as 'root', is it correct? if YES then why doesn't specify "become_user is root"?

    Thanks!

    ReplyDelete
    Replies
    1. Thanks for comment DANNIE TJ.. no its not like root.

      become
      set to yes to activate privilege escalation.

      become_user
      set to user with desired privileges — the user you become, NOT the user you login as. Does NOT imply become: yes, to allow it to be set at host level. Default value is root.

      for further reference you can have ansible doc

      Delete

Latest Posts

Create EC2 Instance Using Terraform  Kodekloud Engineer Task Success