The service_name
variable prints the following.
"msg": [
"BLAH PEX",
"BLAH AEX non prod",
"BLAH BIND",
"BLAH DBFactory"
]
I want to match the variable BSA
which contains the string BLAH BIND
. I tried something like this:
- debug:
msg: "Match found"
when: "BSA in service_name"
It works! But the problem is, it also matches if the BSA
contains the string BLAH
. I want it to only match if there is a exact match. Right now its matching word by word I guess.
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.
Any help how to achieve to match the entire exact string in the array.
I am using 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: It's working as expected. String "BLAH" doesn't match and the debug task is skipped.
- 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"
gives
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
Update
No quotation is needed in the equivalent below play
shell> cat pb.yml
- 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
and the callback community.general.yaml also needs no quotes to display YAML
shell> ANSIBLE_STDOUT_CALLBACK=yaml ansible-playbook pb.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
Hint: You can use ansible-lint to remove redundant quotations from the code. For example,
shell> ansible-lint playbook.yml --fix