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"
    msg: "{{ ansible_version }}"


Which should return something like this.

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


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"
    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.


Web design by yours truely - me, myself, and I   |   |