简体   繁体   中英

ansible facts with condition

I am trying to set the facts based input variable.

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

I am able to achieve the my requirement with the below method but I would like to simplify the same, can someone help me here

  - 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"

The equivalent is as follows, I think

  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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM