Advertisement
Asteryx

check_vlan_present.yml

Jun 17th, 2022
1,953
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ---
  2. - name: Verify and create VLANs
  3.   # VLAN should only be created, if it does not exit on the hosts (Cisco NEXUS 9000v)
  4.   hosts: LEAF2
  5.   gather_facts: no
  6.   vars:
  7.     vlan_exists: false
  8.     vlans:
  9.       - { vlan_id: 2, name: TEST }
  10.   #vars_files:
  11.   #  - vlans.dat
  12.   tasks:
  13.  
  14.     - name: Get Nexus facts
  15.       cisco.nxos.nxos_facts:
  16.         gather_network_resources: vlans
  17.       register: data
  18.  
  19.     #- debug: var=data.ansible_facts.vlan_list verbosity=0
  20.  
  21.     - debug: var=ansible_network_resources['vlans'][1] verbosity=0
  22.  
  23.     - debug:
  24.         # dict
  25.         msg:  "{{ 'ansible_network_resources : ' + ansible_network_resources|type_debug }}"
  26.  
  27.     - debug:
  28.         # list
  29.         msg:  "{{ 'ansible_network_resources[vlan] : ' + ansible_network_resources['vlans']|type_debug }}"
  30.  
  31.     - debug:
  32.         msg:  "{{ 'vlans : ' + vlans|type_debug }}"
  33.  
  34.     - name: Store VLAN facts
  35.       copy:
  36.         content: "{{ ansible_network_resources | to_nice_yaml }}"
  37.         dest: "{{ playbook_dir }}/host_vars/{{ inventory_hostname }}"
  38.  
  39.     - name: DOES VLAN EXIST
  40.       set_fact:
  41.         vlan_exists: true
  42.       when: "vlans in ansible_network_resources['vlans']"
  43.      
  44.     - debug:
  45.        msg: "{{ 'VLAN exists : ' + vlan_exists|string }}"
  46.  
  47.        
  48.     - name: Create new VLANs only
  49.       cisco.nxos.nxos_vlan:
  50.         vlan_id: "{{ item.vlan_id }}"
  51.         name: "{{ item.name }}"
  52.         #state:   "{{ item.state | default('present') }}"
  53.       loop: "{{ vlans }}"
  54.       when: '{{ vlan_exists == false }}'
  55.  
  56. [root@centos playbooks]#
  57.  
  58.  
  59.  
  60. # ==== Output
  61.  
  62.  
  63. [root@centos playbooks]# ansible-playbook check_vlan_present.yml
  64.  
  65. PLAY [Verify and create VLANs] *****************************************************************************************************************************************************************************************************************************************************************************
  66.  
  67. TASK [Get Nexus facts] *************************************************************************************************************************************************************************************************************************************************************************************
  68. ok: [LEAF2]
  69.  
  70. TASK [debug] ***********************************************************************************************************************************************************************************************************************************************************************************************
  71. ok: [LEAF2] => {
  72.     "ansible_network_resources['vlans'][1]": {
  73.         "enabled": true,
  74.         "mode": "ce",
  75.         "name": "TEST",
  76.         "state": "active",
  77.         "vlan_id": 2
  78.     }
  79. }
  80.  
  81. TASK [debug] ***********************************************************************************************************************************************************************************************************************************************************************************************
  82. ok: [LEAF2] => {
  83.     "msg": "ansible_network_resources : dict"
  84. }
  85.  
  86. TASK [debug] ***********************************************************************************************************************************************************************************************************************************************************************************************
  87. ok: [LEAF2] => {
  88.     "msg": "ansible_network_resources[vlan] : list"
  89. }
  90.  
  91. TASK [debug] ***********************************************************************************************************************************************************************************************************************************************************************************************
  92. ok: [LEAF2] => {
  93.     "msg": "vlans : list"
  94. }
  95.  
  96. TASK [Store VLAN facts] ************************************************************************************************************************************************************************************************************************************************************************************
  97. ok: [LEAF2]
  98.  
  99. TASK [DOES VLAN EXIST] *************************************************************************************************************************************************************************************************************************************************************************************
  100. skipping: [LEAF2]
  101.  
  102. TASK [debug] ***********************************************************************************************************************************************************************************************************************************************************************************************
  103. ok: [LEAF2] => {
  104.     "msg": "VLAN exists : False"
  105. }
  106.  
  107. TASK [Create new VLANs only] *******************************************************************************************************************************************************************************************************************************************************************************
  108. [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ vlan_exists == false }}
  109. ok: [LEAF2] => (item={'vlan_id': 2, 'name': 'TEST'})
  110.  
  111. PLAY RECAP *************************************************************************************************************************************************************************************************************************************************************************************************
  112. LEAF2                      : ok=8    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0  
  113.  
  114. [root@centos playbooks]#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement