All technological notes.
Variable
Can be defined in
Sematic
# define
vars:
var_name: var_values
# reference var
line: "nameserver " # use Jinja2 Templating format
# define in variable file
http_port: 8081
snmp_port: 161-162
inter_ip_range: 192.0.2.0
# playbook file
name: Set firewall configuration
hosts: web
task:
- firewalld:
service: https
permanent: true
state: enabled
- firewalld:
port: ""/tcp
permanent: true
state: disabled
- firewalld:
port: ""/tcp
permanent: true
state: disabled
- firewalld:
source: ""/tcp
permanent: internal
state: enabled
True 'true', 't', 'yes', 'y', 'on', '1', 1, 1.0False 'false', 'f', 'no', 'n', 'off', '0', 0, 0.0user.nameWhen same variable is defined in multiple places
Precedence:(highest at the bottom)
ref:
web1 ansible_host=172.20.1.100
web2 ansible_host=172.20.1.101 dns_server=10.5.5.4 # define for this host
web3 ansible_host=172.20.1.102
[web_servers]
web1
Web2
web3
[web_servers:vars]
dns_server=10.5.5.3 # variable defined for groups to be applied for each host
dns_server of web1 and web2: 10.5.5.3 dns_server of web2: 10.5.5.4
- name: Configure DNS server
hosts: all
vars:
dns_server: 10.5.5.5 # play level
task:
- nsupdate:
server: ""
dns_server of all web host: 10.5.5.5
--extra-varsansible-playbook playbook.yml --extra-vars "dns_server=10.5.5.6"
all hosts included in this playbook: 10.5.5.6
register: var_name- name: check /etc/hosts file
hosts: all
tasks:
- shell: cat /etc/hosts
register: result # capture the output in var result
- debug:
var: result # assign the result to var, this print all message
# var: result.stdout # print only the stdout message
-v
ansible-playbook -i inventory playbook.yml -vansible-playbook playbook.yml --extra-vars "ntp_server=10.0.0.1"magic variables
hostvarsAnsible process explanation
for the following inventroy
# /etc/ansible/hosts
web1 ansible_host=172.20.1.100
web2 ansible_host=172.20.1.101 dns_server=10.5.5.4
web3 ansible_host=172.20.1.102
As a result, the host varialbes dns_server are not available for others, because it exists only within the subprocess web2.
hostvars can be used to share variable only in subprocess web2.- name: Print dns server
hosts: all
task:
- debug:
msg: ""
hostvars| Parameter | Desc |
|---|---|
hostvars['host_name'].ansible_host |
Get the hostname/IP of a host |
hostvars['host_name'].ansible_facts.architecture |
Get the architecture of a host’s fact |
hostvars['host_name'].ansible_facts.devices |
Get the devices of a host’s fact |
hostvars['host_name'].ansible_facts.mounts |
Get the mounts of a host’s fact |
hostvars['host_name'].ansible_facts.processor |
Get the processor of a host’s fact |
groups| Magic variable | Desc |
|---|---|
groups['group_name'] |
Get all hosts under a given group |
group_names |
Get the group name of current host |
inventory_hostname |
Get the name in the inventory for the current host |
Ansible facts
Ref:
setup modulegather_facts
truegather_facts: falsegathering = implicit, gether facts automaticallygathering = explicit, not gether facts when executing.
gather_facts: trueansible_facts variable# inventory.yml
tee > "inventory.yaml" <<EOF
all:
hosts:
server1:
ansible_host: 192.168.100.107
ansible_user: ubuntuadmin
EOF
ansible -i inventory.yaml all -m ping
tee >get_fact_variable.yaml << EOF
- name: Ansible Fact Lab Playbook
hosts: all
tasks:
- name: Print all facts
debug:
msg: ""
- name: Print host facts
debug:
msg: "Memory: MB, Distribution: , Architecture: "
EOF
ansible-playbook -i inventory.yaml get_fact_variable.yaml
# TASK [Print host facts] **************************************************************************
# ok: [server1] => {
# "msg": "Memory: 7892 MB, Distribution: Ubuntu, Architecture: x86_64"
# }