0

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

gtirloni
  • 5,746
  • 3
  • 25
  • 52
  • 2
    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. – Vladimir Botka May 16 '22 at 10:11
  • thanks for your useful answer, much appreciated – gtirloni May 17 '22 at 11:01
  • 1
    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. – Vladimir Botka May 17 '22 at 12:04
  • 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:" *` – Zeitounator May 21 '22 at 19:49

2 Answers2

1

I think shell is your best friend for this use case :

grep -ERl "your_vars|{{ your_vars }}" /path/to/your/ansible/code

May do the trick.

Wrest
  • 11
  • 1
  • 3
1

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

hille721
  • 26
  • 1