根据条件更新Ansible中的布尔变量

时间:2019-01-25 18:35:30

标签: ansible ansible-2.x

我最近开始使用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,有人可以帮我吗?预先感谢

1 个答案:

答案 0 :(得分:0)

按照@clockworknet的建议,以下内容对我有用。

final_result和False始终为False,此外,只有在您知道任务失败时才运行set_fact任务。因此,您一开始将final_result:设置为True,然后仅在任务失败的情况下才重新访问,此时您需要做的只是final_result:False