How to get Ansible playbook to terminate When one task fails

You could also use the ansible.builtin.meta module, which allows you to end_play. This affects all inventory hosts in the play, so ansible will stop here.

I also think you might benefit from using block: rescue: and always: for handling what to do when your first command fails or succeeds. If the first command to check for VLAN succeeds on any inventory host, we want to abort the entire play correct? Then add meta: end_play in the block. If the play doesn’t end here, it continues to the rescue step.

---
- name: Gather VLAN facts from Cisco devices
  hosts: cisco
  gather_facts: false
  
  vars_files:
    - /var/MIKROTIK/cisco/inventory

  vars_prompt:
    # - name: "username"
    #   prompt: "Enter User Name"
    #   private: no

    # - name: "password"
    #   prompt: "Enter your password"
    #   private: no
      
    - name: "VLAN"
      prompt: "Enter VLAN ID to add"
      private: no 

    - name: "NAME"
      prompt: "Enter VLAN ID Name"
      private: no    

  # vars:
  #   - ansible_user:
  #   - ansible_password: []

  tasks:
    - name: Fail if VLAN already exists
      block:
        - name: Check if VLAN exists
          nxos_command:
            commands: 
              - show vlan id {{ VLAN }}
          register: vlan_output

        - name: Debug vlan_output variable
          debug:
            var: vlan_output

        - name: Debug etherports variable
          debug:
            var: etherports

        - name: Send notification if VLAN already exists
          community.general.mattermost:
            text: "VLAN {{ VLAN }} already exists on {{ ansible_host }}. Aborting VLAN addition."

        - name: End Play if VLAN exists
          meta: end_play

      rescue:
        - name: Debug vlan_output variable
          debug:
            var: vlan_output

        - name: Debug etherports variable
          debug:
            var: etherports

        - name: Adding VLAN ID to Database
          nxos_vlans:
            config:
              - vlan_id: "{{ VLAN }}"
                name: "{{ NAME }}"
                state: active
          register: vlan_added

        - name: Merge provided configuration with device configuration
          cisco.ios.ios_l2_interfaces:
            config:
              - name: "{{ item }}"
                mode: trunk
                trunk:
                  allowed_vlans: "{{ VLAN }}"
            state: merged
          loop: "{{ etherports }}"

        - name: Send notification message via Mattermost if VLAN is added
          community.general.mattermost:
            text: |
              {% if vlan_added.changed %}
              VLAN {{ VLAN }} added successfully!
              Has been tagged to {{ ansible_host }} by User {{ ansible_user }} on the following interfaces:
              {{ etherports }}
              {% else %}
              VLAN {{ VLAN }} was not added as it already exist on host {{ ansible_host }}. Please use a new VLAN_ID, thank you!!
              {% endif %}
2 Likes