'Create Ansibles remote_tmp from config file variable
I want to fix the following warning by manually creating the path:
Wednesday 22 January 2020 12:50:41 +0100 (0:00:05.878) 0:20:16.431 *****
[WARNING]: Module remote_tmp /home/db2inst1/.ansible/tmp did not exist and was created with a mode of 0700, this may cause issues when running as another user. To avoid this, create the remote_tmp dir with the correct permissions
manually
To keep things DRY, I don't want to just use the pattern in a file
directive for creation. Instead, I'd like to access the remote_tmp
variable inside my playbook to dynamically fetch the path, even when it's changed by environment variables or the ansible.cfg
file.
I couldn't find a documentation about this variable and tried:
- debug:
#var: remote_tmp
#var: hostvars[remote_tmp]
var: ansible_remote_tmp
but always got an error that the variable is not defined.
How can I get the remote_tmp
variable from the config inside a playbook?
Solution 1:[1]
Please see below, if this is what you are looking for.
---
- hosts: "localhost"
tasks:
- name: ansible conf file
shell: cat /etc/ansible/ansible.cfg | grep remote_tmp | awk '{print $3}'
register: remote_tmp
- name: print
debug:
msg: "{{remote_tmp.stdout}}"
Output:
TASK [print]
****************************************************
ok: [localhost] => {
"msg": "~/.ansible/tmp"
}
Solution 2:[2]
As far as I understand you just have to define
allow_world_readable_tmpfiles: true
in your vars.yml
file,
so somehow this solution works in my case.
Solution 3:[3]
Yes, there is no way to access the remote_tmp
value directly as you can with other variables from Ansible.
I confirmed this by dumping all variables with -vvv -m setup
. There is no remote_tmp
in the output.
This is with Ansible 2.10.x
My guess is this is because remote_tmp
is Ansible's temp directory for Ansible's internal use. Not for us to store things in it. :) Because Ansible cleans up after itself but it wouldn't know to clean up our files.
Nevertheless, you could open an issue requesting to see the value of remote_tmp
as an Ansible variable at https://github.com/ansible/ansible/issues/new/choose
Also, that warning isn't anything to worry about -- Ansible is just letting you know it created a temp directory that's only writable by that user. That is a responsible and secure thing to do. Unless you have specific use case that requires different permissions on the remote_tmp directory, the default is most likely fine!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Smily |
Solution 2 | Twissell |
Solution 3 | Aleksey Tsalolikhin |