将精确的字符串与数组/列表匹配 - Ansible

Match exact string against an array / list - Ansible

service_name 变量打印以下内容。

"msg": [
    "BLAH PEX",
    "BLAH AEX non prod",
    "BLAH BIND",
    "BLAH DBFactory"
]

我想匹配包含字符串 BLAH BIND 的变量 BSA。我试过这样的事情:

- debug:
    msg: "Match found"
  when: "BSA in service_name"

有效!但问题是,如果 BSA 包含字符串 BLAH,它也会匹配。我希望它仅在完全匹配时匹配。我猜现在它是逐字匹配的。

预期输出:它应该跳过调试任务,因为当 BSA 包含字符串 BLAH 时,它不应匹配 service_name 列表中的任何字符串。

任何帮助如何实现匹配数组中的整个精确字符串。

我正在使用 ansible 2.7

Q: "Expected Output: It should skip the debug task because when BSA contains the string BLAH, it should not match any strings in the service_name list."

A:一切正常。字符串 "BLAH" 不匹配,调试任务被跳过。

- hosts: localhost
  vars:
    service_name: [ "BLAH PEX", "BLAH AEX non prod", "BLAH BIND", "BLAH DBFactory" ]
    BSA: "BLAH"

  tasks:
    - debug:
        var: service_name
    - debug:
        var: BSA
    - debug:
        msg: "Match found"
      when: "BSA in service_name"
    - debug:
        msg: "Continue"

给予

shell> ansible-playbook playbook.yml

PLAY [localhost] ***

TASK [debug] ***
ok: [localhost] => {
    "service_name": [
        "BLAH PEX", 
        "BLAH AEX non prod", 
        "BLAH BIND", 
        "BLAH DBFactory"
    ]
}

TASK [debug] ***
ok: [localhost] => {
    "BSA": "BLAH"
}

TASK [debug] ***
skipping: [localhost]

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

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