Trace/Debug ansible-playbook variable resolution - ansible

ansible-playbook has a fairly complex variable precedence hierarchy.
In one case I am having trouble figuring out where a variable value is being resolved from.
I'm expecting it to come from group_vars but it isn't.
Is there a simple way to print where a variable value is coming from?
Hoping For something like:
max_mem: roles/app/defaults/main.yml

Related

How to Make Constants in an Expect Script?

I would like to be able to create and set the value of constants in an Expect script.
In scripts such as Bash and a great many programming languages, one can create and assign a value to a constant. I have tried many online sites looking how to do this in Expect, but I have surprisingly not been able to come across this basic information.
Creating and assigning a variable named name in Bash: name="Bob"
Creating and assigning a constant named CONFIG_FILE in Bash: readonly CONFIG_FILE="Configuration.ini"
Creating and assigning a variable named copyPath in Expect: set copyPath "/home/bob/tmp"
Creating and assigning a constant in Expect: ?????
How does one create and set the value of constants in an Expect script?
Like Python, TCL (the scripting language used for Expect) doesn't have constants.
I would suggest going the python route. Define them in all caps, don't change them and hope everyone else gets the hint.
set COPYPATH "/home/bob/tmp"
set PI 3.14159265359
If you really think you need them, there are some hacks in the link glenn jackman posted in the comments https://wiki.tcl-lang.org/page/constants.

ansible - change precedence of role variables

I've created a role to install my apps.
This role needs specific variables defined in separate files in the vars dir of the role, depending of the app (here pdt_type), like that:
- name: Include pkg list of {{ pdt_type }}
include_vars:
file: "{{ pdt_type }}.yml"
It's working fine. But I'd like to be able to overload those variables of my role by the inventory ones. Or vars loaded by include_vars has a greater precedence of the inventory ones.
Do you know how I can do to change this behavior?
How I can defined default variables of a role based on a condition?
Thanks
I think you can't change the variable precedence in ansible and that's good.
A lot of folks may ask about how variables override another. Ultimately it’s Ansible’s philosophy that it’s better you know where to put a variable, and then you have to think about it a lot less. [...] There is only one Empire State Building. One Mona Lisa, etc. Figure out where to define a variable, and don’t make it complicated.
For defining variables on a condition see:
https://serverfault.com/questions/907164/ansible-conditionally-define-variables-in-vars-file-if-a-certain-condition-is-m
Conditionally define variable in Ansible
https://everythingshouldbevirtual.com/automation/ansible-using-set_facts-module/
For including variables conditionally from a file see:
https://docs.ansible.com/ansible/2.7/modules/include_vars_module.html

IDEA Debugger - Search variable in Variables

I debug my project and have a list of Variables. This is enough big project and my question sounds :
There is any option to find for example variable named objectColor in this Variables list ?

Is it possible to undefine a variable in freemarker?

In the freemarker template language, I can test whether a variable exists by using constructs like variable?exists or variable??. I can also cause a previously non-existent variable to exist by assigning to it, e.g., <#assign variable = "hi" />. But how can I cause a previously existing variable to no longer exist?
I have some other dude's freemarker template, with logic at various points that tests for the (non-)existence of certain variables. In my use case, it would be simplest if I could have a variable that exists at one point, then becomes undefined when including his template, then gets assigned to again later on. The alternative is to restructure things more significantly.
No, there's no directive fort that. Maybe it can be achieved with a custom directive (TemplateDirectiveModel) that can then write null into the variable through the Environment. (Unless the Environment API checks for null-s...)

How to make fmpp fail if a value cannot be found for a variable?

Is there a parameter to make FMPP fail when it is unable to find a value for a variable in the template? right now it just leaves the text intact with ${} if it cannot resolve a variable.
Something strange is going on there, because it does fail and by default even aborts the whole batch processing if you refer to an undefined variable. Also, it doesn't leave ${}-s in the output, because all the ${ and } are "parsed away" before the template could do anything. So I suspect the value of those variables is indeed the string "${}", or you have some tricky #escape in the border/footer/header settings, or something tricky like that. (If you can provide a minimalistic example to reproduce this, I can certainly spot the reason.)

Resources