Hello guys
Im having an issue with custom remote facts that are not working, only local on the ansible server , this is what I have.
Querying remote server:
ansible mdo -m setup -a “filter=ansible_local”
mdo | SUCCESS => {
“ansible_facts”: {
“ansible_local”: {},
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false
}
On Local
ansible ansible -m setup -a “filter=ansible_local”
ansible | SUCCESS => {
“ansible_facts”: {
“ansible_local”: {
“preferences”: {
“general”: {
“application”: “ansible”,
“environment”: “production”
}
}
},
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false
}
Remote facts file
cat /etc/ansible/facts.d/preferences.fact
[general]
environment=test
application=test
ansible 2.8.2
config file = /etc/ansible/ansible.cfg
configured module search path = [u’/home/ansible/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /bin/ansible
python version = 2.7.5 (default, Jun 20 2019, 20:27:34) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
On the playbook I include to reload the facts
- name: Referesh Facts
setup: filter=ansible_local
Any hint appreciated
Regards
Are you sure that the file is on the remote, mdo, and readable to the user logging in?
This should confirm that:
ansible mdo -m shell -a "ls -l /etc/ansible/facts.d/preferences.fact;cat /etc/ansible/facts.d/preferences.fact"
You are right, but the playbook set permission the same for all server, but it seems there are not alike
[ansible@ansible ~]$ ansible mdo -m shell -a “ls -l /etc/ansible/facts.d/preferences.fact;cat /etc/ansible/facts.d/preferences.fact”
mdo | FAILED | rc=1 >>
ls: cannot access /etc/ansible/facts.d/preferences.fact: Permission denied
cat: /etc/ansible/facts.d/preferences.fact: Permission deniednon-zero return code
[ansible@ansible ~]$ ansible ansible -m shell -a “ls -l /etc/ansible/facts.d/preferences.fact;cat /etc/ansible/facts.d/preferences.fact”
ansible | CHANGED | rc=0 >>
-rw-r–r-- 1 root root 53 Jul 26 19:25 /etc/ansible/facts.d/preferences.fact
[general]
environment=production
application=ansible
-
name: Create Ansible Fact Directory
file:
path: /etc/ansible/facts.d
owner: root
group: root
mode: ‘0755’
state: directory
-
name: Copy custom facts
copy:
src: files/mdo-preferences.fact
dest: /etc/ansible/facts.d/preferences.fact
owner: root
group: root
mode: ‘0644’
when: ansible_hostname == ‘mdo-test-instance’
-
name: Copy custom facts
copy:
src: files/ansible-preferences.fact
dest: /etc/ansible/facts.d/preferences.fact
owner: root
group: root
mode: ‘0644’
when: ansible_hostname == ‘ansible’
Then it's probably the permissions on /etc/ansible (or /etc but not very likely)
The user need read permission to all directories in the tree to be allowed access to the file.
Maybe I should set the facts under ansible home directory instead? is that a good practice ?
thanks
regrads
I don't think anything is good or bad practice, it's just configuration.
Since Ansible support changing the default path with fact_path in ansible.cfg it's all up to you.
Hi
It was a directory permission issue, now I can read remote facts
[ansible@ansible playbooks]$ ansible mdo -m shell -a “ls -l /etc/ansible/facts.d/preferences.fact;cat /etc/ansible/facts.d/preferences.fact”
mdo | CHANGED | rc=0 >>
-rw-r–r-- 1 root root 63 Jul 27 12:19 /etc/ansible/facts.d/preferences.fact
[general]
environment=test
application=test
customhostname=mdo
But: the when condition is not working, maybe Im not referencing it correctly?
-
name: Copy custom facts
copy:
src: files/mdo-preferences.fact
dest: /etc/ansible/facts.d/preferences.fact
#owner: root
#group: root
#mode: ‘0755’
when: application == ‘test’
-
name: Referesh Facts
setup: filter=ansible_local
Error:
fatal: [mdo]: FAILED! => {“msg”: "The conditional check ‘application == ‘test’’ failed. The error was: error while evaluating conditional (application == ‘test’): ‘application’
Thanks!
Regards
I think you have left out crucial part of the error message.
I also think you have the chicken and the egg problem.
If application is the same that you have in the local fact the first time you use it will fail, since the variable doesn't exist.
To reference you local fact you need to use ansible_local, since your file is called preferences.fact the variables is stored under preferences.
And in the file you have a section call general so the variables is stored under that too, so you need
ansible_local.preferences.general.application
to reference application in your local fact.
yup, that did the trick! thank you much!
Regards