Ansible - 当条件

Ansible - when condition

我对 ansible 还很陌生。我需要帮助 when condition.

请帮助更正以下剧本。在 url.

中接受输入和显示输出的位置
    - hosts: all
      gather_facts: False
      vars_prompt:
      - name: "region"
        prompt: "Which region"
      tasks:
      - name: url_name
        set_fact:
          - url: "google.com"
            when: {{ region }} == "IN"
          - url: "facebook.com"
            when: {{ region }} == "UK"
          - url: "Whosebug"
            when: {{ region }} == "US"
          - url: "LinkedIn"
            when: {{ region }} == "AZ"
      - debug:
          msg: url

与其输入 all if else,我建议你创建一个字典并使用该区域作为索引:

- hosts: localhost
  gather_facts: False
  vars:
    regions:
      IN: google.com
      US: Whosebug        
      AZ: LinkedIn        
      UK: facebook.com 
                  
  vars_prompt:
  - name: "region"
        prompt: "Which region in {{ regions.keys() }}"
        private: no  #to hide the answer typed set yes   
  tasks:
    - debug:
        msg: "{{regions}}" 

    - name: url_name
      set_fact:
        url: "{{ regions[region] }}"

    - debug:
        var: url

一些有趣的修复: 如果您添加过滤器上限,您可以接受下限或上限答案:

  set_fact:
    url: "{{ regions[region|upper] }}"