Score:0

Automatic way to find where variables are coming from in Ansible

in flag

Whenever I have to change something in our Ansible repository, I have to check all the places where a variable could be set and that's very time consuming.

Is there something that would show me something like "the varialbe X being used in this template file is defined is all these places for host Y"?

br flag
No. It is not. It's up to you to keep track of [where the variables are coming from](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable). In fact, asking this question indicates that something is wrong with the structure of the data.
in flag
thanks for your useful answer, much appreciated
br flag
Post your [mre](https://stackoverflow.com/help/minimal-reproducible-example) problem, instead. There might be options on how to improve the structure of the data or how to efficiently debug issues.
Zeitounator avatar
fr flag
Using an IDE with decent search functionalities should make this a breeze. Example for pycharm which I usually use for ansible development. Hitting `ctrl-shift-F`, selecting 'In project" and entering "my_variable_name:" (note the ending column) immediately gives me the list of files and the lines where that variable is set. The documentation site proposes a [list of IDEs and plugins](https://docs.ansible.com/ansible/latest/community/other_tools_and_programs.html#popular-editors). Looking with grep is also a simple option: from your project root => `grep -Rn "my_variable_name:" *`
Score:1
fi flag

We had a similar problem in our company. Very large inventory where it was hard to keep track where a variable is defined and what "wins" (https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#understanding-variable-precedence)

I have written a simple tool to solve that: https://github.com/hille721/ansible-variables

But this only works for host context variables yet, thus not for playbook or role variables

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.