Why playbook is not making file?

Hi @juliettet @Tej-Singh-Rana @akshayyw
I’ve assigned a task to create a playbook to make an empty file on all app servers with some more configurations.
But playbook is not making it!
If I use “state: present” it shows you don’t have permissions.
Please check the below SS.

1 Like

Hi @Ashu27,

The task mentions that all app servers need to be added to the playbook.yaml file.

Try something a little more along the lines of this:

sudo vi inventory

=> make sure to add [all] to this file so that it can be used later in your yaml file

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

=> make/set inventory as default inventory file => file now takes precedence over settings in /etc/ansible/hosts

sudo vi ansible.cfg

add:

[defaults]
host_key_checking = False
inventory = ./inventory
remote_user=thor

sudo vi playbook.yml

- name: Create an blank file on each App Server
  hosts: all
  become: yes
  tasks:
    -
        copy:
          dest: /home/nfsshare.txt
          content: ""
          owner: tony
          group: tony
          backup: yes
          mode: '0777'
        when: 'ansible_host=="172.16.238.10"'
    -
        copy:
           dest: /home/nfsshare.txt
           ...repeat above for app server 2...

    -
        copy:
            dest: /home/nfsshare.txt
           ...repeat above for app server 3...

…see if this helps:-)

Hello, @Ashu27
Define state under the file module.

it shows you don’t have permissions.

Add become below hosts.
Because we know that normal users can’t create a file under slash (/).

1 Like

Hi @juliettet @Tej-Singh-Rana

This is my playbook

- hosts: appserver1

become: yes
tasks:
- file:
path: /home/nfsshare.txt
state: touch
owner: tony
group: tony
mode: ‘0777’

  • hosts: appserver2
    become: yes
    tasks:

    • file:
      path: /home/nfsshare.txt
      state: touch
      owner: steve
      group: steve
      mode: ‘0777’
  • hosts: appserver3
    become: yes
    tasks:

    • file:
      path: /home/nfsshare.txt
      state: touch
      owner: banner
      group: banner
      mode: ‘0777’

and this is my inventory

[appserver1]

stapp01 ansible_host=172.16.238.10 ansible_ssh_pass=Ir0nM@n ansible_user=tony ansible_connection=ssh
[appserver2]
stapp02 ansible_host=172.16.238.11 ansible_ssh_pass=Am3ric@ ansible_user=steve ansible_connection=ssh
[appserver3]
stapp03 ansible_host=172.16.238.12 ansible_ssh_pass=BigGr33n ansible_user=banner ansible_connection=ssh

It is executed successfully,
and I’ve used thor user under ansible cfg file
become_user=thor

Is this all okay as per task?

Looks fine and it will work without adding become_user=thor . After executed successfully. Do cross check for one file.

ssh tony@stapp01 "ls -l /home/"

Look for permission and ownership.
After that you can submit the task.