4

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

Community
  • 1
  • 1
FrustratedWithFormsDesigner
  • 25,116
  • 30
  • 128
  • 188
  • I'm voting to close this question as off-topic because I've asked a better (I hope!) question here: http://unix.stackexchange.com/questions/220841/start-stop-daemon-services-environment-variables-and-ansible – FrustratedWithFormsDesigner Aug 07 '15 at 18:02

4 Answers4

4

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

Justas
  • 516
  • 6
  • 18
3

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.

Antony
  • 31
  • 3
1

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.

Community
  • 1
  • 1
udondan
  • 48,050
  • 17
  • 168
  • 161
  • You're right, this appears to be a problem calling Ansible from a program that is called by `start-stop-daemon`. I tried that trick in the answer you linked, but alas, it does not seem to work :( – FrustratedWithFormsDesigner Aug 06 '15 at 15:39
  • It's a dirty hack but what if you actually create /$HOME, maybe as symlink to /tmp? ln -s /tmp /$HOME (maybe escape the $. Sorry, on mobile right now and can't test) – udondan Aug 06 '15 at 15:49
  • I'm not *that* desparate. Yet... :/ – FrustratedWithFormsDesigner Aug 06 '15 at 18:00
  • 2
    Actually, I think the problem is related to ansible not being able to find its configuration when it is executed as a service. The `remote_tmp` variable is set to `/$HOME/.ansible/tmp` by default (http://docs.ansible.com/ansible/intro_configuration.html#remote-tmp) and I wonder if that may also affect what it's trying to set here, if ansible-run-as-service can't determine what `$HOME` is. I've tried to override this in `/etc/ansible/ansible.cfg` but it doesn't seem to work... – FrustratedWithFormsDesigner Aug 06 '15 at 20:56
  • Looks like this is the source of my grief: https://github.com/ansible/ansible/blob/5ce3988d8693357f671f3fbec43b2d3b862db5f6/v1/ansible/runner/connection_plugins/ssh.py#L56 – FrustratedWithFormsDesigner Aug 07 '15 at 13:54
1

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
modle13
  • 824
  • 1
  • 11
  • 12
  • I also had this issue. For people running as a command, you can use the `env` utility on unix-like systems: `env HOME=/path/to/home ansible ...` – glmdev Dec 21 '20 at 21:14