- name: Test
hosts: localhost
connection: local
tasks:
- name: run shell
shell: "ls"
register: register_shell
- name: print shell
debug:
msg: "{{ register_shell }}"
- name: Run if "Vlan 1" exist
debug:
msg: Vlan 1 exist
when: register_shell.stdout_lines is search("Vlan 1")
- name: Run if "Vlan 1" NOT exist
debug:
msg: Vlan 1 NOT exist
when: register_shell.stdout_lines is not search("Vlan 1")
Вывод
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [run shell] ***************************************************************
changed: [localhost]
TASK [print shell] *************************************************************
ok: [localhost] => {
"msg": {
"changed": true,
"cmd": "ls",
"delta": "0:00:00.004657",
"end": "2022-04-15 14:44:10.370659",
"failed": false,
"rc": 0,
"start": "2022-04-15 14:44:10.366002",
"stderr": "",
"stderr_lines": [],
"stdout": "host_vars\nprovisioning.yml\nroles\nVlan 1",
"stdout_lines": [
"host_vars",
"provisioning.yml",
"roles",
"Vlan 1"
]
}
}
TASK [Run if "Vlan 1" exist] ***************************************************
ok: [localhost] => {
"msg": "Vlan 1 exist"
}
TASK [Run if "Vlan 1" NOT exist] ***********************************************
skipping: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=4 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0