1) Using variable and retriving
Use variable to and retrive it to create a file
---
- hosts: ss
vars:
file: test1
tasks:
- name: Create
file using varaibel name
file:
path=/home/moham/{{ file }} state=touch
...
This way of calling using curley braces is known as
jinja2 method.
2) Using facts
We already know what is fact
and how to get facts using setup module. But here is the example on how to use
facts and retirive its value.
---
- hosts: ss
tasks:
- name: Use
remote server fact and create a file in local
local_action:
file path=/home/moham/ymls/{{ ansible_hostname }} state=touch
...
3) using local facts in playbook
---
- hosts: ss
tasks:
- name: Use
remote server local facts
local_action:
file path=/home/moham/ymls/{{ ansible_local.serverinfo.info.servertype }}
state=touch
...
[root@XXXX facts.d]# pwd
/etc/ansible/facts.d
[root@XXXX facts.d]# cat serverinfo.fact
[info]
customer_environment : test
servertype : vm-esx5
The format from retriving local fact is -
ansible_local.<local fact file name>.<groupname>.<keyname>
"ansible_facts": {
"ansible_local": {
"serverinfo": {
"info": {
"application_name": " LINUX",
"application_role": "EXPLORER",
"customer_environment": "test",
"datacenter": "",
"msp": "",
"servertype": "vm-esx5"
"ipv4": {
"address": "",
"broadcast": "",
"netmask": "",
"network": ""
---
Retriving complex variable
- hosts: ss
tasks:
- name: Use
remote server fact and create a file in local
local_action:
file path=/home/moham607/ymls/{{ ansible_hostname }} state=touch
- name: Use
remote server local facts
local_action:
file path=/home/moham607/ymls/{{ ansible_local.serverinfo.info.servertype }}
state=file
- name: print
the variable
debug:
msg="{{ ansible_eth3.ipv4.address }}"
- name: print
the variable
debug:
msg="{{ hostvars['l0202']['ansible_distribution'] }}"
Example for set fact and redirecting fact contents to a
file
---
- hosts: test
tasks:
- name: print
the variable
debug:
msg="{{ ansible_enp0s3.ipv4.address }}"
- name: check
set_fact
set_fact:
IPA :
"{{ ansible_enp0s3.ipv4.address }}"
- name: create file with variable content
local_action:
copy content={{IPA}} dest=/root/ymls/setfact
No comments:
Post a Comment