Ansible cannot make dir /$HOME/.ansible/cp

2019-04-15 03:22发布

问题:

I'm getting a very strange error when I run ansible:

GATHERING FACTS *************************************************************** 
fatal: [i-0f55b6a4] => Could not make dir /$HOME/.ansible/cp: [Errno 13] Permission denied: '/$HOME'

TASK: [Task #1] *************************************************************** 
FATAL: no hosts matched or all hosts have already failed -- aborting


PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/home/ubuntu/install.retry

i-0f55b6a4                 : ok=0    changed=0    unreachable=1    failed=0   

Normally, this playbook runs without problems, but I've recently made some changes so that the program that calls ansible is called from start-stop-daemon so that I will run as a service. The ultimate goal being to have a service that can run the playbook automatically, when it deems it necessary.

The beginning of the playbook looks like this:

---
- hosts: w_vm:main
  sudo: True
  tasks:
  - name: Task #1
    ...

sudo is set to True so I'm somewhat certain that the error is not on the target machine.

The generated invocation of ansible-playbook looks like this:

ansible-playbook -i /tmp/ansible3397486563152037600.inventory \
                    /home/ubuntu/playbooks/main_playbook.yml \
                    -e @/home/ubuntu/extra_params.json

I'm not sure if that Could not make dir /$HOME/.ansible/cp error is occurring on the server or on the remote machine, or why ansible is trying to make a directory named $HOME in /. This only happens when the program that calls ansible is called from the linux service, not when it's called explicitly from the command line.


I've asked a more specific question here: https://unix.stackexchange.com/questions/220841/start-stop-daemon-services-environment-variables-and-ansible

回答1:

Try sudo chown -R YOUR_USERNAME /home/YOUR_USERNAME/.ansible



回答2:

Late to answer but might be useful to someone. Check the ownership of ~/.ansible. The ownership of .ansible in the local machine (which runs ansible/ansible controller node) might be causing the problem. Do "chown -R username:groupname .ansible" (username:groupname should be of the user running the playbook) and try to run the playbook again

As an alternative remove this .ansible directory from controller node and rerun the playbook.



回答3:

Ansible creates temporary files in ~/.ansible on your local machine and on the remote machine. So that could be theoretically triggered from both sides.

My guess is, it is on the local machine where Ansible runs since how Ansible was started should not have an effect on the target boxes. A quick search showed programs started with start-stop-deamon do not have $HOME (or any env at all) available, but it has an -e option to set them according to your needs.

If -e is unavailable, see this answer, which suggests to additionally exec /usr/bin/env to set environment variables.



回答4:

I ran into a similar issue using Jenkins. It had a default $HOME env var set to /root/. The solution was to inject the environment variable at runtime.

HOME=/path/to/your/users/home