I have three variables to be register based on condition, it will run one task and register one variable, how can i use testing variable for respective task ?
---
- name: Test1
command: "echo HAHA"
register: testing
when: HAHA is defined
- name: Test2
command: "echo BLABLA"
register: testing
when: BLABLA is defined
- name: Test3
command: "echo DODO"
register: testing
when: DODO is defined
- name: Verify Tests
command: "execute {{ testing.stdout }}"
when: TEST is defined
I got an error like undefined varible like below:
{"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'nnThe error appears to be in '/home/ubuntu/tests/': line 3, column 3, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn- name: Verify Tests n ^ heren"
Could you please suggest ?
2
Answers
It’s always a good idea to use
debug
task to verify that your variables contain the content you think they do.When you use
register
in a task, it always sets the named variable, whether the task runs or not. This is because the registered value includes information about the execution status of the task. For example, if variableDODO
is not set, then in your final task, the variabletesting
will contain:As you can see, there is no
stdout
attribute here. For the behavior you want, you could rewrite you playbook like this:Running this with no variables defined results in:
But if we define the target variables:
Or:
If multiple variables are set, you will get the value from the first task to execute:
If more information about the overall Use Case is provided, better solutions could be provided. Be that as it may, based on the question and given information it can be done in a single task.
A minimal example playbook
will result into an output of
Similar Q&A