|
24 | 24 | ansible.builtin.debug: |
25 | 25 | msg: "Found {{ prefixes_query.json.count }} prefixes tagged with '{{ subnet_pool_tag }}' in location '{{ site_location }}'" |
26 | 26 |
|
27 | | -- name: Assert prefixes were found |
28 | | - ansible.builtin.assert: |
29 | | - that: |
30 | | - - prefixes_query.json.count > 0 |
31 | | - fail_msg: "No prefixes found with tag '{{ subnet_pool_tag }}' in location '{{ site_location }}' in Nautobot" |
| 27 | +- name: Skip subnet pool creation when no prefixes were found |
| 28 | + ansible.builtin.debug: |
| 29 | + msg: >- |
| 30 | + No prefixes found with tag '{{ subnet_pool_tag }}' in location |
| 31 | + '{{ site_location }}'; skipping OpenStack subnet pool creation |
| 32 | + when: prefixes_query.json.count | int == 0 |
32 | 33 |
|
33 | | -- name: Group prefixes by subnet pool name |
34 | | - ansible.builtin.set_fact: |
35 | | - subnet_pools: | |
36 | | - {{ |
37 | | - prefixes_query.json.results |
38 | | - | selectattr('custom_fields.subnet_pool_name', 'defined') |
39 | | - | groupby('custom_fields.subnet_pool_name') |
40 | | - | map(attribute='1') |
41 | | - | map('list') |
42 | | - }} |
| 34 | +- name: Create OpenStack subnet pools from matching prefixes |
| 35 | + when: prefixes_query.json.count | int > 0 |
| 36 | + block: |
| 37 | + - name: Group prefixes by subnet pool name |
| 38 | + ansible.builtin.set_fact: |
| 39 | + subnet_pools: | |
| 40 | + {{ |
| 41 | + prefixes_query.json.results |
| 42 | + | selectattr('custom_fields.subnet_pool_name', 'defined') |
| 43 | + | groupby('custom_fields.subnet_pool_name') |
| 44 | + | map(attribute='1') |
| 45 | + | map('list') |
| 46 | + }} |
43 | 47 |
|
44 | | -- name: Log discovered subnet pools |
45 | | - ansible.builtin.debug: |
46 | | - msg: | |
47 | | - Discovered subnet pools: |
48 | | - {{ subnet_pools | map(attribute='0.custom_fields.subnet_pool_name') | list }} |
| 48 | + - name: Log discovered subnet pools |
| 49 | + ansible.builtin.debug: |
| 50 | + msg: | |
| 51 | + Discovered subnet pools: |
| 52 | + {{ subnet_pools | map(attribute='0.custom_fields.subnet_pool_name') | list }} |
49 | 53 |
|
50 | | -- name: Create or update OpenStack subnet pools |
51 | | - openstack.cloud.subnet_pool: |
52 | | - state: present |
53 | | - name: "{{ pool_item[0].custom_fields.subnet_pool_name }}" |
54 | | - prefixes: "{{ pool_item | map(attribute='prefix') | list }}" |
55 | | - default_prefix_length: "{{ pool_item[0].custom_fields.subnet_pool_default_prefixlen }}" |
56 | | - minimum_prefix_length: "{{ pool_item[0].custom_fields.subnet_pool_min_prefixlen }}" |
57 | | - maximum_prefix_length: "{{ pool_item[0].custom_fields.subnet_pool_max_prefixlen }}" |
58 | | - is_shared: true |
59 | | - loop: "{{ subnet_pools }}" |
60 | | - loop_control: |
61 | | - loop_var: pool_item |
62 | | - label: "{{ pool_item[0].custom_fields.subnet_pool_name }}" |
63 | | - register: subnet_pool_results |
| 54 | + - name: Create or update OpenStack subnet pools |
| 55 | + openstack.cloud.subnet_pool: |
| 56 | + state: present |
| 57 | + name: "{{ pool_item[0].custom_fields.subnet_pool_name }}" |
| 58 | + prefixes: "{{ pool_item | map(attribute='prefix') | list }}" |
| 59 | + default_prefix_length: "{{ pool_item[0].custom_fields.subnet_pool_default_prefixlen }}" |
| 60 | + minimum_prefix_length: "{{ pool_item[0].custom_fields.subnet_pool_min_prefixlen }}" |
| 61 | + maximum_prefix_length: "{{ pool_item[0].custom_fields.subnet_pool_max_prefixlen }}" |
| 62 | + is_shared: true |
| 63 | + loop: "{{ subnet_pools }}" |
| 64 | + loop_control: |
| 65 | + loop_var: pool_item |
| 66 | + label: "{{ pool_item[0].custom_fields.subnet_pool_name }}" |
| 67 | + register: subnet_pool_results |
64 | 68 |
|
65 | | -- name: Log subnet pool creation results |
66 | | - ansible.builtin.debug: |
67 | | - msg: "Subnet pool '{{ item.pool_item[0].custom_fields.subnet_pool_name }}': {{ 'created' if item is changed else 'unchanged' }}" |
68 | | - loop: "{{ subnet_pool_results.results }}" |
69 | | - when: item is not skipped |
| 69 | + - name: Log subnet pool creation results |
| 70 | + ansible.builtin.debug: |
| 71 | + msg: "Subnet pool '{{ item.pool_item[0].custom_fields.subnet_pool_name }}': {{ 'created' if item is changed else 'unchanged' }}" |
| 72 | + loop: "{{ subnet_pool_results.results }}" |
| 73 | + when: item is not skipped |
70 | 74 |
|
71 | | -- name: Verify subnet pools were created |
72 | | - ansible.builtin.debug: |
73 | | - msg: "Successfully created/updated {{ subnet_pool_results.results | length }} subnet pools" |
| 75 | + - name: Verify subnet pools were created |
| 76 | + ansible.builtin.debug: |
| 77 | + msg: "Successfully created/updated {{ subnet_pool_results.results | length }} subnet pools" |
0 commit comments