Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- - name: Verify and create VLANs
- # VLAN should only be created, if it does not exit on the hosts (Cisco NEXUS 9000v)
- hosts: LEAF2
- gather_facts: no
- vars:
- vlan_exists: false
- vlans:
- - { vlan_id: 2, name: TEST }
- #vars_files:
- # - vlans.dat
- tasks:
- - name: Get Nexus facts
- cisco.nxos.nxos_facts:
- gather_network_resources: vlans
- register: data
- #- debug: var=data.ansible_facts.vlan_list verbosity=0
- - debug: var=ansible_network_resources['vlans'][1] verbosity=0
- - debug:
- # dict
- msg: "{{ 'ansible_network_resources : ' + ansible_network_resources|type_debug }}"
- - debug:
- # list
- msg: "{{ 'ansible_network_resources[vlan] : ' + ansible_network_resources['vlans']|type_debug }}"
- - debug:
- msg: "{{ 'vlans : ' + vlans|type_debug }}"
- - name: Store VLAN facts
- copy:
- content: "{{ ansible_network_resources | to_nice_yaml }}"
- dest: "{{ playbook_dir }}/host_vars/{{ inventory_hostname }}"
- - name: DOES VLAN EXIST
- set_fact:
- vlan_exists: true
- when: "vlans in ansible_network_resources['vlans']"
- - debug:
- msg: "{{ 'VLAN exists : ' + vlan_exists|string }}"
- - name: Create new VLANs only
- cisco.nxos.nxos_vlan:
- vlan_id: "{{ item.vlan_id }}"
- name: "{{ item.name }}"
- #state: "{{ item.state | default('present') }}"
- loop: "{{ vlans }}"
- when: '{{ vlan_exists == false }}'
- [root@centos playbooks]#
- # ==== Output
- [root@centos playbooks]# ansible-playbook check_vlan_present.yml
- PLAY [Verify and create VLANs] *****************************************************************************************************************************************************************************************************************************************************************************
- TASK [Get Nexus facts] *************************************************************************************************************************************************************************************************************************************************************************************
- ok: [LEAF2]
- TASK [debug] ***********************************************************************************************************************************************************************************************************************************************************************************************
- ok: [LEAF2] => {
- "ansible_network_resources['vlans'][1]": {
- "enabled": true,
- "mode": "ce",
- "name": "TEST",
- "state": "active",
- "vlan_id": 2
- }
- }
- TASK [debug] ***********************************************************************************************************************************************************************************************************************************************************************************************
- ok: [LEAF2] => {
- "msg": "ansible_network_resources : dict"
- }
- TASK [debug] ***********************************************************************************************************************************************************************************************************************************************************************************************
- ok: [LEAF2] => {
- "msg": "ansible_network_resources[vlan] : list"
- }
- TASK [debug] ***********************************************************************************************************************************************************************************************************************************************************************************************
- ok: [LEAF2] => {
- "msg": "vlans : list"
- }
- TASK [Store VLAN facts] ************************************************************************************************************************************************************************************************************************************************************************************
- ok: [LEAF2]
- TASK [DOES VLAN EXIST] *************************************************************************************************************************************************************************************************************************************************************************************
- skipping: [LEAF2]
- TASK [debug] ***********************************************************************************************************************************************************************************************************************************************************************************************
- ok: [LEAF2] => {
- "msg": "VLAN exists : False"
- }
- TASK [Create new VLANs only] *******************************************************************************************************************************************************************************************************************************************************************************
- [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ vlan_exists == false }}
- ok: [LEAF2] => (item={'vlan_id': 2, 'name': 'TEST'})
- PLAY RECAP *************************************************************************************************************************************************************************************************************************************************************************************************
- LEAF2 : ok=8 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
- [root@centos playbooks]#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement