Reputation: 528
I would like to check if the variable in previous task is defined and if it was, what is its value.
In order to do that I have to first check if the variable exists and then later check its value or the task crashes.
This is what I have so far (but not as if inside if and obviously not working):
- name: "Checking other server availbility"
uri:
url: http://www.google.pl
timeout: 5
register: command_result_2
when: command_result.status != 200
- name: "Setting default server"
command: echo http://www.google.pl
register: SERVER_PATH
when:
- command_result_2 is defined
- command_result_2.status == 200
In my code the first task may or may not register a command_result_2
variable.
Upvotes: 0
Views: 105
Reputation: 68469
I'm afraid you are trying to solve a problem with your solution, not your real problem, but here is an answer to the question.
You can use the default
filter to return a value when the variable is not defined (in the example below 0 will never equal 200, so the task will run only when command_result_2
is defined and equal to 200):
- name: "Setting default server"
command: echo http://www.google.pl
register: SERVER_PATH
when: command_result_2|default(0) == 200
Please mind that the above task (command: echo
+ register
) makes little sense in Ansible. And even if you used more appropriate set_fact
, I have no clue why you'd want to run such a task in the first place.
Upvotes: 2