Ansible Variables


Variables Overview

  • Variables can be found in:
    • Playbooks.
    • Inventory (group var, host var).
    • Command Line.
    • Discovered Variables (facts).

Command Line Var

  • To specify an variable in the command line use the option -e "var_name=value". For example:
ansible-playbook playbook_test.yml -e "uservar=me"

Playbook Var

  • To reference a variable in playbooks use the var_name format. For example:
- name: playbook test of variables
  hosts: routers
  remote_user: "{{ username }}"



Discovered Facts Var

  • Ansible can automatically discover a number of facts about a device. These facts are just variable and can be used in playbooks.
  • The use case is to write playbooks that gather information like IP address, version number..etc and reference these variable in the playbook without actually using absolute values.
  • To gather facts you'll need to use the setup module with a command like:
ansible-playbook playbook_test.yml -m setup
  • Learning facts doesn't work too well for networking devices, but rather for servers. At leas that's the case for now.


rating: 0+x
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License