Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I get a list of Ansible pre-defined variables?

I see that Ansible provide some pre-defined variables that we can use in playbooks and template files. For example, the host IP address is ansible_eth0.ipv4.address. Googleing and searching the docs I couldn't find a list of all available variables.
Would someone list them for me?

like image 549
neves Avatar asked Sep 17 '13 00:09

neves


People also ask

Where are Ansible variables stored?

The host variable file is usually stored in the host_vars directory which is usually specified in /etc/ansible/ path. The second section can be defined as a file in the group_vars directory as shown. This should be in the /etc/ansible directory. Remember that the name of the file should be the same as the group name.

How do I see Ansible environment variables?

You can access the environment variables available on the local server via the 'lookup' plugins, which allow you to access the system data, and 'env' plugin which is written for accessing the environment variables.

How many types of variables are there in Ansible?

Variable Scopes Ansible has 3 main scopes: Global: this is set by config, environment variables and the command line. Play: each play and contained structures, vars entries, include_vars, role defaults and vars. Host: variables directly associated to a host, like inventory, facts or registered task outputs.


2 Answers

ansible -m setup hostname 

Only gets the facts gathered by the setup module.

Gilles Cornu posted a template trick to list all variables for a specific host.

Template (later called dump_variables):

HOSTVARS (ANSIBLE GATHERED, group_vars, host_vars) :  {{ hostvars[inventory_hostname] | to_yaml }}  PLAYBOOK VARS:  {{ vars | to_yaml }} 

Playbook to use it:

- hosts: all   tasks:   - template:       src: templates/dump_variables       dest: /tmp/ansible_variables   - fetch:       src: /tmp/ansible_variables       dest: "{{inventory_hostname}}_ansible_variables" 

After that you have a dump of all variables on every host, and a copy of each text dump file on your local workstation in your tmp folder. If you don't want local copies, you can remove the fetch statement.

This includes gathered facts, host variables and group variables. Therefore you see ansible default variables like group_names, inventory_hostname, ansible_ssh_host and so on.

like image 35
Enno Gröper Avatar answered Sep 29 '22 13:09

Enno Gröper


From the FAQ:

How do I see a list of all of the ansible_ variables?

Ansible by default gathers “facts” about the machines under management, and these facts can be accessed in playbooks and in templates. To see a list of all of the facts that are available about a machine, you can run the setup module as an ad hoc action:

ansible -m setup hostname 

This will print out a dictionary of all of the facts that are available for that particular host. You might want to pipe the output to a pager.This does NOT include inventory variables or internal ‘magic’ variables. See the next question if you need more than just ‘facts’.

Here is the output for my vagrant virtual machine called scdev:

scdev | success >> {     "ansible_facts": {                                                                                                          "ansible_all_ipv4_addresses": [                                                                                             "10.0.2.15",                                                                                                            "192.168.10.10"                                                                                                     ],                                                                                                                      "ansible_all_ipv6_addresses": [                                                                                             "fe80::a00:27ff:fe12:9698",                                                                                             "fe80::a00:27ff:fe74:1330"                                                                                          ],                                                                                                                      "ansible_architecture": "i386",                                                                                         "ansible_bios_date": "12/01/2006",                                                                                      "ansible_bios_version": "VirtualBox",                                                                                   "ansible_cmdline": {                                                                                                        "BOOT_IMAGE": "/vmlinuz-3.2.0-23-generic-pae",                                                                          "quiet": true,                                                                                                          "ro": true,                                                                                                             "root": "/dev/mapper/precise32-root"                                                                                },                                                                                                                      "ansible_date_time": {                                                                                                      "date": "2013-09-17",                                                                                                   "day": "17",                                                                                                            "epoch": "1379378304",                                                                                                  "hour": "00",                                                                                                           "iso8601": "2013-09-17T00:38:24Z",                                                                                      "iso8601_micro": "2013-09-17T00:38:24.425092Z",                                                                         "minute": "38",                                                                                                         "month": "09",                                                                                                          "second": "24",                                                                                                         "time": "00:38:24",                                                                                                     "tz": "UTC",                                                                                                            "year": "2013"                                                                                                      },                                                                                                                      "ansible_default_ipv4": {                                                                                                   "address": "10.0.2.15",                                                                                                 "alias": "eth0",                                                                                                        "gateway": "10.0.2.2",                                                                                                  "interface": "eth0",                                                                                                    "macaddress": "08:00:27:12:96:98",                                                                                      "mtu": 1500,                                                                                                            "netmask": "255.255.255.0",                                                                                             "network": "10.0.2.0",                                                                                                  "type": "ether"                                                                                                     },                                                                                                                      "ansible_default_ipv6": {},                                                                                             "ansible_devices": {                                                                                                        "sda": {                                                                                                                    "holders": [],                                                                                                          "host": "SATA controller: Intel Corporation 82801HM/HEM (ICH8M/ICH8M-E) SATA Controller [AHCI mode] (rev 02)",                                                                                                                                 "model": "VBOX HARDDISK",                                                                                               "partitions": {                                                                                                             "sda1": {                                                                                                                   "sectors": "497664",                                                                                                    "sectorsize": 512,                                                                                                      "size": "243.00 MB",                                                                                                    "start": "2048"                                                                                                     },                                                                                                                      "sda2": {                                                                                                                   "sectors": "2",                                                                                                         "sectorsize": 512,                                                                                                      "size": "1.00 KB",                                                                                                      "start": "501758"                                                                                                   },                                                                                                                  },                                                                                                                      "removable": "0",                                                                                                       "rotational": "1",                                                                                                      "scheduler_mode": "cfq",                                                                                                "sectors": "167772160",                                                                                                 "sectorsize": "512",                                                                                                    "size": "80.00 GB",                                                                                                     "support_discard": "0",                                                                                                 "vendor": "ATA"                                                                                                     },                                                                                                                      "sr0": {                                                                                                                    "holders": [],                                                                                                          "host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)",                                            "model": "CD-ROM",                                                                                                      "partitions": {},                                                                                                       "removable": "1",                                                                                                       "rotational": "1",                                                                                                      "scheduler_mode": "cfq",                                                                                                "sectors": "2097151",                                                                                                   "sectorsize": "512",                                                                                                    "size": "1024.00 MB",                                                                                                   "support_discard": "0",                                                                                                 "vendor": "VBOX"                                                                                                    },                                                                                                                      "sr1": {                                                                                                                    "holders": [],                                                                                                          "host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)",                                            "model": "CD-ROM",                                                                                                      "partitions": {},                                                                                                       "removable": "1",                                                                                                       "rotational": "1",                                                                                                      "scheduler_mode": "cfq",                                                                                                "sectors": "2097151",                                                                                                   "sectorsize": "512",                                                                                                    "size": "1024.00 MB",                                                                                                   "support_discard": "0",                                                                                                 "vendor": "VBOX"                                                                                                    }                                                                                                                   },                                                                                                                      "ansible_distribution": "Ubuntu",                                                                                       "ansible_distribution_release": "precise",                                                                              "ansible_distribution_version": "12.04",                                                                                "ansible_domain": "",                                                                                                   "ansible_eth0": {                                                                                                           "active": true,                                                                                                         "device": "eth0",                                                                                                       "ipv4": {                                                                                                                   "address": "10.0.2.15",                                                                                                 "netmask": "255.255.255.0",                                                                                             "network": "10.0.2.0"                                                                                               },                                                                                                                      "ipv6": [                                                                                                                   {                                                                                                                           "address": "fe80::a00:27ff:fe12:9698",                                                                                  "prefix": "64",                                                                                                         "scope": "link"                                                                                                     }                                                                                                                   ],                                                                                                                      "macaddress": "08:00:27:12:96:98",                                                                                      "module": "e1000",                                                                                                      "mtu": 1500,                                                                                                            "type": "ether"                                                                                                     },                                                                                                                      "ansible_eth1": {                                                                                                           "active": true,                                                                                                         "device": "eth1",                                                                                                       "ipv4": {                                                                                                                   "address": "192.168.10.10",                                                                                             "netmask": "255.255.255.0",                                                                                             "network": "192.168.10.0"                                                                                           },                                                                                                                      "ipv6": [                                                                                                                   {                                                                                                                           "address": "fe80::a00:27ff:fe74:1330",                                                                                  "prefix": "64",                                                                                                         "scope": "link"                                                                                                     }                                                                                                                   ],                                                                                                                      "macaddress": "08:00:27:74:13:30",                                                                                      "module": "e1000",                                                                                                      "mtu": 1500,                                                                                                            "type": "ether"                                                                                                     },                                                                                                                      "ansible_form_factor": "Other",                                                                                         "ansible_fqdn": "scdev",                                                                                                "ansible_hostname": "scdev",                                                                                            "ansible_interfaces": [                                                                                                     "lo",                                                                                                                   "eth1",                                                                                                                 "eth0"                                                                                                              ],                                                                                                                      "ansible_kernel": "3.2.0-23-generic-pae",                                                                               "ansible_lo": {                                                                                                             "active": true,                                                                                                         "device": "lo",                                                                                                         "ipv4": {                                                                                                                   "address": "127.0.0.1",                                                                                                 "netmask": "255.0.0.0",                                                                                                 "network": "127.0.0.0"                                                                                              },                                                                                                                      "ipv6": [                                                                                                                   {                                                                                                                           "address": "::1",                                                                                                       "prefix": "128",                                                                                                        "scope": "host"                                                                                                     }                                                                                                                   ],                                                                                                                      "mtu": 16436,                                                                                                           "type": "loopback"                                                                                                  },                                                                                                                      "ansible_lsb": {                                                                                                            "codename": "precise",                                                                                                  "description": "Ubuntu 12.04 LTS",                                                                                      "id": "Ubuntu",                                                                                                         "major_release": "12",                                                                                                  "release": "12.04"                                                                                                  },                                                                                                                      "ansible_machine": "i686",                                                                                              "ansible_memfree_mb": 23,                                                                                               "ansible_memtotal_mb": 369,                                                                                             "ansible_mounts": [                                                                                                         {                                                                                                                           "device": "/dev/mapper/precise32-root",                                                                                 "fstype": "ext4",                                                                                                       "mount": "/",                                                                                                           "options": "rw,errors=remount-ro",                                                                                      "size_available": 77685088256,                                                                                          "size_total": 84696281088                                                                                           },                                                                                                                      {                                                                                                                           "device": "/dev/sda1",                                                                                                  "fstype": "ext2",                                                                                                       "mount": "/boot",                                                                                                       "options": "rw",                                                                                                        "size_available": 201044992,                                                                                            "size_total": 238787584                                                                                             },                                                                                                                      {                                                                                                                           "device": "/vagrant",                                                                                                   "fstype": "vboxsf",                                                                                                     "mount": "/vagrant",                                                                                                    "options": "uid=1000,gid=1000,rw",                                                                                      "size_available": 42013151232,                                                                                          "size_total": 484145360896                                                                                          }                                                                                                                   ],                                                                                                                      "ansible_os_family": "Debian",                                                                                          "ansible_pkg_mgr": "apt",                                                                                               "ansible_processor": [                                                                                                      "Pentium(R) Dual-Core  CPU      E5300  @ 2.60GHz"                                                                   ],                                                                                                                      "ansible_processor_cores": "NA",                                                                                        "ansible_processor_count": 1,                                                                                           "ansible_product_name": "VirtualBox",                                                                                   "ansible_product_serial": "NA",                                                                                         "ansible_product_uuid": "NA",                                                                                           "ansible_product_version": "1.2",                                                                                       "ansible_python_version": "2.7.3",          "ansible_selinux": false,          "ansible_swapfree_mb": 766,          "ansible_swaptotal_mb": 767,          "ansible_system": "Linux",          "ansible_system_vendor": "innotek GmbH",          "ansible_user_id": "neves",          "ansible_userspace_architecture": "i386",          "ansible_userspace_bits": "32",          "ansible_virtualization_role": "guest",          "ansible_virtualization_type": "virtualbox"     },      "changed": false } 

The current documentation now has a complete chapter about Discovering variables: facts and magic variables.

like image 112
neves Avatar answered Sep 29 '22 14:09

neves