根据条件更新 Ansible 中的布尔变量
Update a boolean variable in Ansible upon a condition
我最近开始使用 Ansible,我有一个条件,我必须根据条件更新变量的值。我试过查找它,但找不到好的方法。
#I am defining the final_result variable here, which would be updated after executing every single step. Sample step given below.
- name: Define variable
set_fact:
fianl_result: True
- name: First of the N steps to be executed.
command: "my shell command here"
ignore_errors: yes
register: test_result
- name: Updating final_result variable
set_fact:
fianl_result: final_result and False
when: test_result.rc == 0 and test_result.stderr.find("Error':' flag needs an argument") == -1
- name: Second of the N steps to be executed.
command: "my shell command here"
ignore_errors: yes
register: test_result
- name: Updating final_result variable
set_fact:
fianl_result: final_result and False
when: test_result.rc == 0 and test_result.stderr.find("Error':' flag needs an argument") == -1
基本上我正在尝试在 python 中做这样的事情:
final_result = True
if test_result == False:
final_result = final_result and False
我想在每一步后更新变量 final_result,谁能帮我解决这个问题。提前致谢
正如@clockworknet 所建议的,以下对我有用。
final_result 和 False 将始终评估为 False,而且当您知道任务失败时,您只是 运行 set_fact 任务。所以你在开始时设置 final_result: True ,然后只有在任务失败时才重新访问,此时你需要做的就是 final_result: False
我最近开始使用 Ansible,我有一个条件,我必须根据条件更新变量的值。我试过查找它,但找不到好的方法。
#I am defining the final_result variable here, which would be updated after executing every single step. Sample step given below.
- name: Define variable
set_fact:
fianl_result: True
- name: First of the N steps to be executed.
command: "my shell command here"
ignore_errors: yes
register: test_result
- name: Updating final_result variable
set_fact:
fianl_result: final_result and False
when: test_result.rc == 0 and test_result.stderr.find("Error':' flag needs an argument") == -1
- name: Second of the N steps to be executed.
command: "my shell command here"
ignore_errors: yes
register: test_result
- name: Updating final_result variable
set_fact:
fianl_result: final_result and False
when: test_result.rc == 0 and test_result.stderr.find("Error':' flag needs an argument") == -1
基本上我正在尝试在 python 中做这样的事情:
final_result = True
if test_result == False:
final_result = final_result and False
我想在每一步后更新变量 final_result,谁能帮我解决这个问题。提前致谢
正如@clockworknet 所建议的,以下对我有用。
final_result 和 False 将始终评估为 False,而且当您知道任务失败时,您只是 运行 set_fact 任务。所以你在开始时设置 final_result: True ,然后只有在任务失败时才重新访问,此时你需要做的就是 final_result: False