Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible permission issue

I have a simple ansible task that creates a file:

- name: create fake file 
  file:
    name: /opt/refdata/PROD02/roman.delete
    state: touch

I generated the public/private keys and added public one to authorized_keys2 for the user I am running as on the target host.

When I try to run it I get the following error:

failed: [experiment01] => {"failed": true, "parsed": false}
Traceback (most recent call last):
  File "/home/acplus_uat01/.ansible/tmp/ansible-tmp-1441921944.69-3869708445827/file", line 1999, in <module>
    main()
  File "/home/acplus_uat01/.ansible/tmp/ansible-tmp-1441921944.69-3869708445827/file", line 372, in main
    open(path, 'w').close()
IOError: [Errno 2] No such file or directory: '/opt/refdata/PROD02/roman.delete'

So, to see if I have issues with ssh or python I tried this - I created a python file with one line:

open('/opt/refdata/PROD02/roman.delete', 'w').close()

and ran this from the same place and the same user as I run ansible:

cat test2.py | ssh -i ~/.ssh/myPrivateKey -q target_user@targethost python -

and it created the file.

So, my question is - where is the problem, why can't it create a file?

the way I run the playbook is this:

ansible-playbook -i inventory/prod/ acc.yml -v --vault-password-file=~/.ansible-vault-pw --private-key ~/.ssh/myPrivateKey

I also tried to create a file in /tmp/ and ansible worked.

Edit: So, another update - I made the directory I am writing the file into world writable ( 777 ) and it created the file. So, the question is - what is different in Ansible that

 cat test2.py | ssh -i ~/.ssh/myPrivateKey -q target_user@targethost python -

works and doing essentially the same thing through Ansible doesn't.

like image 649
Roman Goyenko Avatar asked Sep 10 '15 22:09

Roman Goyenko


1 Answers

If /opt/refdata/PROD02/ doesn't exist you should create directory first

file:
  name: /opt/refdata/PROD02
  state: directory
  recurse: yes
  mode: 0755

Ansible documentation says:

recurse - Set the specified file attributes (applies only to state=directory)

So Ansible cannot create file and all the directories in it's path at one command.

Then with second command you should create the file itself.

name: create fake file 
  file:
    name: /opt/refdata/PROD02/roman.delete
    state: touch
like image 171
Nick Roz Avatar answered Oct 28 '22 14:10

Nick Roz