A
A
Anatoly Gruzdev2022-04-15 09:05:30
Cisco
Anatoly Gruzdev, 2022-04-15 09:05:30

How to set a condition in a playbook (Ansible)?

How to set a condition in the playbook in which the task will not be executed only if the string "Vlan 1" is found in the sh_ip_int_br_result.stdout output?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mureevms, 2022-04-15
@radzeus

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

Conclusion
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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question