Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update playbooks_error_handling.rst #2095

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from

Conversation

S1ructure
Copy link

Add example for using own variables in conditions

Related: https://forum.ansible.com/t/using-variables-in-changed-when-failed-when-directives/10652/3

From my point of view this information was not clear yet, that
a) custom variables can be used in conditionals as well
b) the {{ }} annotation has to be avoided

Add example for using own variables in conditions
@S1ructure S1ructure force-pushed the update-playboox-error-handling branch from 356a5b8 to 473045a Compare November 3, 2024 18:40
Copy link
Contributor

@oraNod oraNod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much for adding this to the docs @S1ructure - I've made a few suggestions. I'd also wonder if this should go into the variables section of the docs, maybe here: https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#referencing-simple-variables

As I mentioned in one of my suggestions, I think putting this in the error handling section focuses too much on resolving a warning rather than promoting the best way to do something, if that makes sense.


Just like ``when`` these two conditionals do not require templating delimiters (``{{ }}``) as they are implied.
If you still use them, ansible will raise a warning ``[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should include the exact warning text here. Granted this might be helpful to users who search for the warning but it adds maintenance overhead to the docs. I also don't believe it's good practice to document errors and warnings - better to document just the behaviour.

.. note::
Notice the missing ``{{ }}`` around log_path.

Just like ``when`` these two conditionals do not require templating delimiters (``{{ }}``) as they are implied.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Just like ``when`` these two conditionals do not require templating delimiters (``{{ }}``) as they are implied.
Just like ``when`` these two conditionals do not require templating delimiters (``{{ }}``) because they are raw Jinja2 expressions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -172,9 +172,30 @@ You can also combine multiple conditions to override "changed" result.
- '"ERROR" in result.stderr'
- result.rc == 2

If you want to introduce your own variables, to avoid repeating a certain term, you can simply reference them in your conditionals
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If you want to introduce your own variables, to avoid repeating a certain term, you can simply reference them in your conditionals
You can reference simple variables in conditionals to avoid repeating certain terms, as in the following example:

.. note::
Notice the missing ``{{ }}`` around log_path.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Notice the missing ``{{ }}`` around log_path.
Notice the missing double curly braces ``{{ }}`` around the ``log_path`` variable in the ``changed_when`` statement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants