Skip to main content

Keep track of Ansible host context variables

Project description

ansible-variables

PyPI version PyPI platforms pre-commit

The Ansible inventory provides a very powerful framework to declare variables in a hierarchical manner. There a lof of different places where a variable can be defined (inventory, host_vars, groups_vars, ...) and Ansible will merge them in a specific order (variable precedence).

ansible-variables will help to keep track of your host context variables:

  • inventory file or script group vars
  • inventory group_vars/all
  • inventory group_vars/*
  • inventory file or script host vars
  • inventory host_vars/*

Based on one host it will return a list with all variables, values and variable type.

Tested with ansible-core 2.11 - 2.16.

Installation

pip install ansible-variables

Usage

The command line usage is similar to the official Ansible CLI tools, especially like ansible-inventory, thus to see all possible commands and options run

ansible-variables --help

Get all variables for a host

The basic usage is pretty simple, you only need to pass the Ansible hostname to it:

ansible-variables mywebserver

This results in following simple rich formatted output command_simple

The verbosity can be increased Ansible like with -v, -vvv, ...

With -v the inventory files where the variable is defined, will be printed. The last file wins.

command_simple_verbose

With -vvv it will also print all files which were considered to look for the variable.

Get one specific variables for a host

If you are only interested in one variable you can specify it with --var:

ansible-variables mywebserver --var foo

Same es above, the verbosity can also increase here.

More customization

With --help you will see which further arguments are possible, e.g. you can set the path to your inventory with -i

ansible-variables mywebserver -i /path/to/inventory

Implementation

This tool is tightly coupled to the Ansible library (ansible-core) and simple reuses what is already there. The whole structure and implementation was inspired and oriented by the implementation of ansible-inventory.

To get the source and the inventory files in which Ansible will look for a variable, we are using a debug flag in Ansible's get_vars method.

As as result, the output of ansible-variables can be fully trusted as it uses the same methods as Ansible to get the variable precedence.

Limitations

  • as written in the description, this tool only shows host context variables and does not know anything about playbook or role variables or command line options.

Credits

  • the screenshots used in this README where created with termshot

License

This project is licensed under the GNU General Public License v3.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ansible-variables-0.6.1.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ansible_variables-0.6.1-py3-none-any.whl (21.1 kB view details)

Uploaded Python 3

File details

Details for the file ansible-variables-0.6.1.tar.gz.

File metadata

  • Download URL: ansible-variables-0.6.1.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for ansible-variables-0.6.1.tar.gz
Algorithm Hash digest
SHA256 ead982b83fee6bfe040439288f424df9b2c7b3dff68eea6531b4a8cc412f4162
MD5 6e1fb927424a654397a216b14f24e239
BLAKE2b-256 ceeae5830bf84e89a1f383ab208535c03f1c5c497491824576e15272ae406879

See more details on using hashes here.

File details

Details for the file ansible_variables-0.6.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ansible_variables-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f3e5b4de823f936c4f54bed73f6e0c3961572c339810da83f3e34daec4b12d03
MD5 3572c1b6671e3954d3f6370e6dbb678a
BLAKE2b-256 1d049529504e2404ca4299bceda217f4a3e2f6ed80f390fd0e15acd5edf223a9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page