Ansible 带条件的可解集var

Ansible 带条件的可解集var,ansible,Ansible,我要用下面的代码为提示变量设置一个值: - name: "test" hosts: localhost connection: local become: yes vars_prompt: - name: https prompt: Use https(yes/NO)? private: no default: no vars: protocol: "{{ 'https' if https

我要用下面的代码为提示变量设置一个值:

- name: "test"
  hosts: localhost
  connection: local
  become: yes

  vars_prompt:

    - name: https
      prompt: Use https(yes/NO)?
      private: no
      default: no

  vars:
    protocol: "{{ 'https' if https else 'http' }}" 

  tasks:
  - debug:
      msg: "{{ https | bool }}"

  - debug:
      msg: "{{ protocol }}"
结果总是https。。为什么?

Use https(yes/NO)? [False]: no

PLAY [test] ********************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] *******************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": false
}

TASK [debug] *******************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "https"
}

PLAY RECAP *********************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

我在这里读了很多问题,正确的方法是像我现在这样定义条件变量。。。我真的不知道使用提示变量是否有任何问题…

我找到了我自己问题的答案。。。正确的方法是使用“| bool”:

- name: "test"
  hosts: localhost
  connection: local
  become: yes

  vars_prompt:

    - name: https
      prompt: Use https(yes/NO)?
      private: no
      default: no

  vars:
    protocol: "{{ 'https' if (https | bool) else 'http' }}" 

  tasks:
  - debug:
      msg: "{{ https | bool }}"

  - debug:
      msg: "{{ protocol }}"