FreeKB - Ansible Getting Started with Magic Variables
Ansible - Getting Started with Magic Variables

A fact is some piece of information about a remote system, such as the IP address or hostname of a managed node. vars or the set_fact module can be used to create a variable / fact.

A magic variable (also known as special variables) is some piece of information about the control node (that's the Ansible server), such as the version of Ansible or the hosts in inventory. A magic variable is one of the three types of special variables. The other two special variables are facts and connection variables. For example, the ansible_version magic variable contains the version of Ansible, like this.

- name: "output the content of the 'ansible_version' magic variable"
  debug:
    msg: "{{ ansible_version }}"

 

Which should return something like this.

TASK [output the content of the 'ansible_version' magic variable]
ok: [server1.example.com] => {
    "msg": {
        "full": "2.4.2.0", 
        "major": 2, 
        "minor": 4, 
        "revision": 2, 
        "string": "2.4.2.0"
    }
}

 

Magic variables are reserved. When using the vars or set_fact module, you shouldn't create a variable that has the same name as a magic variable, such as "ansible_version". Don't do this:

- name: "set_fact ansible_version"
  set_fact:
    ansible_version: "123"

 

Two of the most commonly used magic variables are inventory_hostname and hostvars.



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter abd7e in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |