User Tools

Site Tools


ansible:ansible_variables

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
ansible:ansible_variables [2021/08/25 22:37]
manu
ansible:ansible_variables [2025/05/21 16:24] (current)
manu
Line 1: Line 1:
 ====== Ansible variables ====== ====== Ansible variables ======
  
-Understanding variable precedence+===== Understanding variable precedence ​=====
  
 Ansible does apply variable precedence, and you might have a use for it. Here is the order of precedence from least to greatest (the last listed variables override all other variables): Ansible does apply variable precedence, and you might have a use for it. Here is the order of precedence from least to greatest (the last listed variables override all other variables):
Line 29: Line 29:
  
 In general, Ansible gives precedence to variables that were defined more recently, more actively, and with more explicit scope. Variables in the defaults folder inside a role are easily overridden. Anything in the vars directory of the role overrides previous versions of that variable in the namespace. Host and/or inventory variables override role defaults, but explicit includes such as the vars directory or an include_vars task override inventory variables. In general, Ansible gives precedence to variables that were defined more recently, more actively, and with more explicit scope. Variables in the defaults folder inside a role are easily overridden. Anything in the vars directory of the role overrides previous versions of that variable in the namespace. Host and/or inventory variables override role defaults, but explicit includes such as the vars directory or an include_vars task override inventory variables.
 +
 +===== Example in a playbook =====
 +
 +Ex: yml file
 +<​code>​
 +- hosts: webservers
 +  vars:
 +    http_port: 80
 +    base_path: http://​ansibile.local/​wiki
 +    app_path: {{ base_path }}/{{ http_port }}
 +</​code>​
 +
 +===== variables: facts =====
 +
 +Facts are variable collected each time you contact a client. It contains IP adresses, OS type, level, hardware....
 +
 +To improve you ansible command, you can use cache on server into /​etc/​ansible/​ansible.cfg (here cache is valid for 1 hour)
 +<​code>​
 +[defaults]
 +fact_caching = jsonfile
 +fact_caching_timeout = 3600
 +fact_caching_connection = /​tmp/​myfacts
 +</​code>​
 +
 +You can call them on the ansible server by using the command (xml output):
 +<cli prompt='#'>​
 +opensuse:~ # ansible -m setup localhost ​
 + ​localhost | SUCCESS => {
 +    "​ansible_facts":​ {
 +        "​ansible_all_ipv4_addresses":​ [
 +            "​10.1.3.22"​
 +        ],
 +        "​ansible_all_ipv6_addresses":​ [
 +            "​fe80::​209a:​12e0:​e8a7:​8ea"​
 +        ],
 +        "​ansible_apparmor":​ {
 +            "​status":​ "​enabled"​
 +        },
 +        "​ansible_architecture":​ "​x86_64",​
 +        "​ansible_cmdline":​ {
 +            "​BOOT_IMAGE":​ "/​boot/​vmlinuz-4.12.14-lp151.28.91-default",​
 +
 +        },
 +        "​ansible_date_time":​ {
 +            "​date":​ "​2021-09-13",​
 +            "​day":​ "​13",​
 +            "​epoch":​ "​1631520866",​
 +            "​hour":​ "​10",​
 +            "​minute":​ "​14",​
 +            "​month":​ "​09",​
 +            "​second":​ "​26",​
 +            "​time":​ "​10:​14:​26",​
 +            "​tz":​ "​CEST",​
 +            "​tz_offset":​ "​+0200",​
 +            "​weekday":​ "​Monday",​
 +            "​weekday_number":​ "​1",​
 +            "​weeknumber":​ "​37",​
 +            "​year":​ "​2021"​
 +        },
 +...
 +       "​ansible_distribution":​ "​openSUSE Leap",
 +        "​ansible_distribution_file_parsed":​ true,
 +        "​ansible_distribution_file_path":​ "/​etc/​os-release",​
 +        "​ansible_distribution_file_variety":​ "​SUSE",​
 +        "​ansible_distribution_major_version":​ "​15",​
 +        "​ansible_distribution_release":​ "​1",​
 +        "​ansible_distribution_version":​ "​15.1",​
 +</​cli>​
 +
 +Example of facts on AIX
 +<​code>​
 +{
 +    "​_ansible_facts_gathered":​ true,
 +    "​ansible_all_ipv4_addresses":​ [
 +        "​10.10.10.100"​
 +    ],
 +    "​ansible_all_ipv6_addresses":​ [
 +        "::​1%1"​
 +    ],
 +    "​ansible_apparmor":​ {
 +        "​status":​ "​disabled"​
 +    },
 +    "​ansible_architecture":​ "​chrp",​
 +    "​ansible_date_time":​ {
 +...
 +    },
 +    "​ansible_distribution":​ "​AIX",​
 +    "​ansible_distribution_major_version":​ "​7",​
 +    "​ansible_distribution_release":​ "​2",​
 +    "​ansible_distribution_version":​ "​7.2",​
 +    "​ansible_nodename":​ "​myaixhost",​
 +    "​ansible_os_family":​ "​AIX",​
 +    "​ansible_pkg_mgr":​ "​yum",​
 +    "​ansible_processor":​ "​PowerPC_POWER8",​
 +    "​ansible_processor_cores":​ 8,
 +    "​ansible_processor_count":​ 1,
 +    "​ansible_product_name":​ "​IBM,​8284-22A",​
 +    "​ansible_product_serial":​ "​21xxxxV",​
 +
 +</​code>​
 +
 +Example on linux ppc64le
 +<​code>​
 +{
 +    "​_ansible_facts_gathered":​ true,
 +    "​ansible_all_ipv4_addresses":​ [
 +        "​10.x.x.x0"​
 +    ],
 +    "​ansible_all_ipv6_addresses":​ [
 +        "​fe80::​5b6c:​b599:​xxx9:​xxxc"​
 +    ],
 +    "​ansible_apparmor":​ {
 +        "​status":​ "​disabled"​
 +    },
 +    "​ansible_architecture":​ "​ppc64le",​
 +    "​ansible_bios_date":​ "​NA",​
 +    "​ansible_bios_vendor":​ "​NA",​
 +    "​ansible_bios_version":​ "​NA",​
 +    "​ansible_board_asset_tag":​ "​NA",​
 +    "​ansible_board_name":​ "​NA",​
 +    "​ansible_board_serial":​ "​NA",​
 +    "​ansible_board_vendor":​ "​NA",​
 +    "​ansible_board_version":​ "​NA",​
 +    "​ansible_chassis_asset_tag":​ "​NA",​
 +    "​ansible_chassis_serial":​ "​NA",​
 +    "​ansible_chassis_vendor":​ "​NA",​
 +    "​ansible_chassis_version":​ "​NA",​
 +    "​ansible_cmdline":​ {
 +        "​BOOT_IMAGE":​ "/​vmlinuz-4.18.0-240.el8.ppc64le",​
 +    "​ansible_distribution":​ "​CentOS",​
 +    "​ansible_distribution_file_parsed":​ true,
 +    "​ansible_distribution_file_path":​ "/​etc/​redhat-release",​
 +    "​ansible_distribution_file_variety":​ "​RedHat",​
 +    "​ansible_distribution_major_version":​ "​8",​
 +    "​ansible_distribution_release":​ "​NA",​
 +    "​ansible_distribution_version":​ "​8.4",​
 +    "​ansible_nodename":​ "​mylnxhost",​
 +    "​ansible_os_family":​ "​RedHat",​
 +
 +</​code>​
 +
 +==== How to Create and Use Custom Facts in Ansible ====
 +
 +Custom facts (local facts) are the variables which are declared on ansible managed host. Custom facts are declared in ini or json file in the /​etc/​ansible/​facts.d directory on managed host. File names of custom facts must have .fact extension.
 +
 +https://​www.linuxtechi.com/​create-use-custom-facts-in-ansible/​
 +
 +
 +Get the value of a variable Ex: var1
 +<cli prompt='#'>​
 +opensuse:~ # ansible -i "​node2,"​ all -e "​var1=toto"​ -m debug -a '​msg={{ var1 }}'
 +</​cli>​
 +
 +===== Ansible inventory =====
 +
 +<cli prompt='​$'>​
 +$ ansible --version | grep '​config file'
 +  config file = /​home/​tux/​.ansible.cfg
 +$ ansible-config dump --only-changed
 +DEFAULT_BECOME(/​home/​tux/​.ansible.cfg) = True
 +DEFAULT_BECOME_METHOD(/​home/​tux/​.ansible.cfg) = sudo
 +DEFAULT_HOST_LIST(/​home/​tux/​.ansible.cfg) = ['/​home/​tux/​
 +inventory'​]
 +DEFAULT_REMOTE_USER(/​home/​tux/​.ansible.cfg) = ansible
 +</​cli>​
 +
 +List all hosts, use the keyword **all**. To specify all host not in a group use **ungrouped**
 +<cli prompt='​$'>​
 +$ ansible all --list-hosts
 +  hosts (21):
 +    green.example.com
 +    blue.example.com
 +    192.168.100.1
 +    192.168.100.10
 +    alpha.example.org
 +    beta.example.org
 +    www001.example.com
 +    www002.example.com
 +    www003.example.com
 +    db-99-node.example.com
 +    db-100-node.example.com
 +    db-101-node.example.com
 +</​cli>​
 +
 +List the group webservers ​
 +<cli prompt='​$'>​
 +$ ansible webservers --list-hosts
 +  hosts (10):
 +    alpha.example.org
 +    beta.example.org
 +    192.168.1.100
 +    192.168.1.110
 +    www001.example.com
 +    www002.example.com
 +    www003.example.com
 +</​cli>​
 +
 +Print inventory in YML format
 +<cli prompt='​$'>​
 +$ ansible-inventory --list --yaml
 +all:
 +  children:
 +    ungrouped:
 +      hosts:
 +        172.16.120.161:​ {}
 +        172.16.120.185:​ {}
 +        172.16.120.188:​ {}
 +</​cli>​
 +
 +===== Custom variable =====
 +
 +==== Time / date ====
 +
 +<​code>​
 +- hosts: test
 +  vars:
 +    time: "{{ lookup('​pipe',​ 'date -d \"1 day ago\" +\"​%Y%m%d\"'​) }}"
 +</​code>​
 +
 +
ansible/ansible_variables.1629923847.txt.gz ยท Last modified: 2021/08/25 22:37 by manu