Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Ansible - Variable

Back


Variable

# 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


Variable Types


Variable Precedence


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


ansible-playbook playbook.yml --extra-vars "dns_server=10.5.5.6"

all hosts included in this playbook: 10.5.5.6


Register: Output a varialbe

- 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

Variable Scope


Magic variable

Magic variable - hostvars

# /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
- name: Print dns server
  hosts: all
  task:
    - debug:
        msg: ""
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

Magic variable - 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 Fact


Lab: Get Fact Data

# 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"
# }