Search This Blog

Sunday, 9 May 2021

Ansible playbook to create a file on remote host and change the permissions

 Task:

The Nautilus DevOps team is working to test several Ansible modules on servers in Stratos DC. Recently they wanted to test file creation on remote hosts using Ansible. More details about the task aregiven below. Please proceed with the same:

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 /opt/opt.txt on all app servers.
c. The /opt/opt.txt file permission must be 0777.
d. The user/group owner of file /opt/opt.txt must be tony on app server 1, steve on app server 2 and banner on app server 3.
Note: Validation will try to run playbook using command ansible-playbook -i inventory playbook.yml, so please make sure playbook works this way, without passing any extra arguments.

Step 1) Create an Inventory File

thor@jump_host ~/playbook$ cat inventory 
stapp01 ansible_connection=ssh ansible_user=tony
stapp02 ansible_connection=ssh ansible_user=steve
stapp03 ansible_connection=ssh ansible_user=banner

Step 2) Create a playbook

thor@jump_host ~/playbook$ cat playbook.yml 
---
- name: create a blank file
  hosts: all
  become: true
  tasks:

    - name: Create a file
      shell: touch /opt/opt.txt

    - name: Change file ownership, group and permissions to tony
      file:
        path: /opt/opt.txt
        owner: tony
        group: tony
        mode: '0777'
      when: (ansible_user == "tony")

    - name: Change file ownership, group and permissions to steve
      file:
        path: /opt/opt.txt
        owner: steve
        group: steve
        mode: '0777'
      when: (ansible_user == "steve")

    - name: Change file ownership, group and permissions to banner
      file:
        path: /opt/opt.txt
        owner: banner
        group: banner
        mode: '0777'
      when: (ansible_user == "banner")


Step 3) Run the playbook

thor@jump_host ~/playbook$ ansible-playbook -i inventory playbook.yml

PLAY [create a blank file] *********************************************************************************

TASK [Gathering Facts] *************************************************************************************
ok: [stapp01]
ok: [stapp03]
ok: [stapp02]

TASK [Create an ansible file] ******************************************************************************
[WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If you need to use
command because file is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [stapp01]
changed: [stapp02]
changed: [stapp03]

TASK [Change file ownership, group and permissions for user tony] ******************************************
skipping: [stapp02]
skipping: [stapp03]
changed: [stapp01]

TASK [Change file ownership, group and permissions for user steve] *****************************************
skipping: [stapp01]
skipping: [stapp03]
changed: [stapp02]

TASK [Change file ownership, group and permissions for user banner] ****************************************
skipping: [stapp01]
skipping: [stapp02]
changed: [stapp03]

PLAY RECAP *************************************************************************************************
stapp01                    : ok=3    changed=2    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
stapp02                    : ok=3    changed=2    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
stapp03                    : ok=3    changed=2    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   

Step 4) Validate 

thor@jump_host ~/playbook$ ssh tony@stapp01
Last login: Sun May  9 19:07:08 2021 from jump_host.devops-ansible-file_app_net
[tony@stapp01 ~]$ cd /opt/
-rwxrwxrwx 1 tony tony 0 May  9 19:07 opt.txt
[tony@stapp01 opt]$ exit
logout
Connection to stapp01 closed.

thor@jump_host ~/playbook$ ssh steve@stapp02
Last login: Sun May  9 19:07:08 2021 from jump_host.devops-ansible-file_app_net
[steve@stapp02 ~]$ ls -rlt /opt/opt.txt 
-rwxrwxrwx 1 steve steve 0 May  9 19:07 /opt/opt.txt
[steve@stapp02 ~]$ exit
logout
Connection to stapp02 closed.

thor@jump_host ~/playbook$ ssh banner@stapp03
Last login: Sun May  9 19:07:09 2021 from jump_host.devops-ansible-file_app_net
[banner@stapp03 ~]$ ls -lrt /opt/opt.txt 
-rwxrwxrwx 1 banner banner 0 May  9 19:07 /opt/opt.txt