【问题标题】:ansible facts with condition有条件的事实
【发布时间】:2019-05-29 06:44:11
【问题描述】:

我正在尝试设置基于事实的输入变量。

if input protocol = to TCP/UDP 
  then service name will be TCP-443/UDP-443 
else service name will be ALL

我可以用下面的方法达到我的要求,但我想简化一下,有人可以帮我吗

  - name: setting fact for service name
    set_fact:
      servicename: TCP-{{port}}
    when: >
          protocol == "TCP" or protocol == "tcp"

  - name: setting fact for service name
    set_fact:
      servicename: UDP-{{ port }}
    when: >
          protocol == "UDP" or protocol == "udp"

  - name: setting fact for service name
    set_fact:
      servicename: "ALL"
    when: >
          protocol == "all" or protocol == "ALL"

  - name: setting fact for service name
    set_fact:
      protocolname: "tcp_udp_sctp"
    when: >
          protocol == "TCP" or protocol == "tcp"

  - name: setting fact for service name
    set_fact:
      protocolname: "tcp_udp_sctp"
    when: >
          protocol == "UDP" or protocol == "udp"

  - name: setting fact for service name
    set_fact:
      protocolname: "ALL"
    when: >
          protocol == "all" or protocol == "ALL"

【问题讨论】:

    标签: ansible ansible-facts


    【解决方案1】:

    等效如下,我认为

      vars:
        port: 443
      tasks:
        - name: setting fact for service name
          set_fact:
            servicename: "{{ protocol|upper }}-{{ port }}"
            protocolname: "tcp_udp_sctp"
          when: protocol|upper == "TCP" or
                protocol|upper == "UDP"
    
        - name: setting fact for service name
          set_fact:
            servicename: "ALL"
            protocolname: "ALL"
          when: protocol|upper == "ALL"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      • 2017-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 2013-09-16
      相关资源
      最近更新 更多