Ansible fixed the bug as misunderstood and alsogather_facts
a play directive, it does not work on tasks.
then whats the way to get localhost facts?
-
create role and role YML file with remote hosts defined as
-
create tasks file which actually needs to gather fact of localhost
-
run the role yml file
role file :-
-----------------------------
---
- name: My role name
hosts: windows
gather_facts: yes
vars_files:
- ./roles/myrole/vars/main.yml
roles:
- { role: myrole, tags: ["myrole"] }
------------------------------------------------------------
Tasks file :-
------------------------------------------------------------
---
#---------------------------------------------------------------------------
- name: Windows OS User home directory
delegate_to: localhost
gather_facts: true
set_fact:
userhomedir: '{{ ansible_user_dir }}\.m2'
register: userhomedirwin
when: ansible_os_family == "Windows"
tags:
- winuserhome
- name: DEBUG Windows OS User home directory
debug:
var: userhomedirwin
verbosity: 3
when: ansible_os_family == "Windows"
- name: Linux OS User home directory
delegate_to: localhost
gather_facts: true
set_fact:
userhomedir: "{{ ansible_user_dir }}/.m2"
when: ansible_os_family != "Windows"
register: userhomedirlin
tags:
- linuserhome
- name: DEBUG Linux OS User home directory
debug:
var: userhomedirlin
verbosity: 3
when: ansible_os_family != "Windows"
- name: Creating Maven settting config file from template
local_action: template src={{ srcSettingsXml }} dest={{ destSettingsXml }}
register: settingxml
tags:
- settingxml
- name: DEBUG Creating Maven Setting xml config file from template
debug:
var: settingxml
verbosity: 3
#---------------------------------------------------------------------------
`
TASK [cobrowse : Windows OS User home directory] ************************************************************************************
task path: /root/svdevops/roles/myrole/tasks/Pre_Setup.yml:57
ok: [192.168.100.222 -> localhost] => {
"ansible_facts": {
"userhomedir": "C:\Users\Administrator\.m2"
},
"changed": false,
"failed": false
}
Read vars_file './roles/myrole/vars/main.yml'
TASK [cobrowse : DEBUG Windows OS User home directory] ******************************************************************************
task path: /root/svdevops/roles/myrole/tasks/Pre_Setup.yml:66
ok: [192.168.100.222 ] => {
"userhomedirwin": {
"ansible_facts": {
"userhomedir": "C:\Users\Administrator\.m2"
},
"ansible_facts_cacheable": false,
"changed": false,
"failed": false
}
}
Read vars_file './roles/myrole/vars/main.yml'
Read vars_file './roles/myrole/vars/main.yml'
TASK [cobrowse : Linux OS User home directory] **************************************************************************************
task path: /root/svdevops/roles/myrole/tasks/Pre_Setup.yml:71
skipping: [192.168.100.222 ] => {
"changed": false,
"skip_reason": "Conditional result was False",
"skipped": true
}
Read vars_file './roles/myrole/vars/main.yml'
TASK [cobrowse : DEBUG Linux OS User home directory] ********************************************************************************
task path: /root/svdevops/roles/myrole/tasks/Pre_Setup.yml:80
skipping: [192.168.100.222 ] => {
"skip_reason": "Conditional result was False"
}
Read vars_file './roles/myrole/vars/main.yml'
Read vars_file './roles/myrole/vars/main.yml'
`