Hello Chaz,
QUESTION: How can we load or import an env/ini file that is located in a file on the Remote/Target server?
GOAL:
We have an env/ini file on the remote/target server.
We want to read in that file and use the values as variables for the tasks on that machine.( FYI: Our process does not allow for saving these variables in the Inventory - even though that would be nice. )
Here is an example of what our ini/env file looks like:
Path: /devops/properties/Admin.propertiesusername = FooAdminpassword = bar123XYZactivate = no(Basically property format or ini format without any sections)
Can't you link/transform your file to be a local facts file? Take
a look at
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#local-facts-facts-d
| |
Hi Chaz,
I like the idea of this suggestion.Is local-facts-facts-d usable as Non-Root?
They come up as part of the dictionary returned by the setup
module as ansible_local, so yes.
Can facts be loaded from the user's $HOME/.ansible/ , like $HOME/.ansible/facts.d or something like that?
I see for windows there is fact_path does that work for Linux?
Reading https://docs.ansible.com/ansible/2.4/intro_configuration.html#fact-path it has precisely that use case (a user's HOME). I don't see that it should not work for Linux. You should try it on a host and see.
Now, back to what you're trying to do. Your variable file is almost an INI file, it just needs a section.
I was curious, so I tried something like what you're trying to
do:
[root@gladys facts.d]# pwd
/etc/ansible/facts.d
[root@gladys facts.d]# ls -l
total 4
-rwxr-xr-x. 1 root root 106 Jan 14 18:47 factfile.fact
[root@gladys facts.d]# cat factfile.fact
#!/bin/bash
# An ini file must have a section
echo '[general]'
cat /home/management/thishasvariables.txt
[root@gladys facts.d]# ls -l /home/management/thishasvariables.txt
-rw-r--r--. 1 management management 11 Jan 14 18:48 /home/management/thishasvariables.txt
[root@gladys facts.d]# cat /home/management/thishasvariables.txt
spam=eggs
my_user@mybox:~$ ansible -u management -i 'gladys, ' all -m setup -a "filter=ansible_local"
gladys | SUCCESS => {
"ansible_facts": {
"ansible_local": {
"factfile": {
"general": {
"spam": "eggs"
}
}
}
},
"changed": false
}
This works. Whether it is secure or not, that's a different matter, it does take a user-supplied file and present it as facts for the host.
Cheers,
Hugo