phoenixnap / ansible-collection-pnap

Collection of Ansible modules for interacting with the Bare Metal Cloud API
4 stars 10 forks source link

`phoenixnap.bmc.ip_block` will not provision multiple ip_blocks when block size is the same and description is different. #21

Closed estenrye closed 9 months ago

estenrye commented 1 year ago

Expected Behavior

Two /28 ip blocks should be created in the ASH datacenter when description is different.

Actual Behavior

Inventory

ip_blocks:
  hosts:
    public_ip_address_block_03:
      location: ASH
      cidr_block_size: /28
    public_ip_address_block_04:
      location: ASH
      cidr_block_size: /28

Playbook

- name: Provision IP Blocks
  hosts: ip_blocks
  strategy: linear
  gather_facts: false
  vars_files:
    - ~/.pnap/config.yaml
  collections:
  - phoenixnap.bmc
  tags:
    - provision
    - ip_blocks
  tasks:
  - name: Ensure ip blocks are created and present.
    delegate_to: localhost
    connection: local
    register: result
    phoenixnap.bmc.ip_block:
      client_id: "{{ clientId }}"
      client_secret: "{{ clientSecret }}"
      description: "{{ inventory_hostname }}"
      location: "{{ hostvars[inventory_hostname].location | default('PHX') }}"
      cidr_block_size: "{{ hostvars[inventory_hostname].cidr_block_size | default('/31') }}"
      state: present
    retries: 3
    delay: 3
    until: (not result.failed) and (result.ip_blocks is defined)

  - name: print out relevant response
    ansible.builtin.debug:
      var: result
    tags:
      - debug

  - name: package result
    set_fact:
      resultdict:
        res: "{{ result }}"
        ansible_host: "{{ result.ip_blocks.0.id }}"
    when: result is defined

  - name: create results directory
    delegate_to: localhost
    connection: local
    ansible.builtin.file:
      state: directory
      path: .results
    when: result is defined

  - name: write provisioning result to file in case additional data needs to be investigated from response
    delegate_to: localhost
    connection: local
    copy:
      content: "{{ resultdict | to_nice_yaml }}"
      dest: ".results/ip-block-{{inventory_hostname}}.yaml"
    when: result is defined

.results/ip-block-public_ip_address_block_03.yaml

ansible_host: 64761857aa889829e2009faa
res:
    attempts: 1
    changed: false
    failed: false
    ip_blocks:
    -   assignedResourceId: 6476185aaa889829e2009fab
        assignedResourceType: public network
        cidr: 131.153.207.160/28
        cidrBlockSize: /28
        createdOn: '2023-05-30T15:37:59.363Z'
        description: public_ip_address_block_03
        id: 64761857aa889829e2009faa
        isBringYourOwn: false
        location: ASH
        status: assigned
        tags: []

Behavior when more than one /28 is present

TASK [Ensure ip blocks are created and present.] ****************************************************************************************************************************************************************************************************************************************************************
changed: [public_ip_address_block_04 -> localhost]

TASK [print out relevant response] ******************************************************************************************************************************************************************************************************************************************************************************
ok: [public_ip_address_block_04] => {
    "result": {
        "attempts": 1,
        "changed": true,
        "failed": false,
        "ip_blocks": [
            {
                "ipBlockId": "647a5fedeee69f7466197274",
                "result": "IP Block deleted."
            }
        ]
    }
}

Core Problem

The phoenixnap.bmc.ip_block module provides no way to disambiguate ip_blocks of the same size when creating new blocks.

Workaround

estenrye commented 1 year ago

I believe the issue is in this block

https://github.com/phoenixnap/ansible-collection-pnap/blob/93488110b2a5e69e7705bbf6c99b4a3c1c8cfe8f/plugins/modules/ip_block.py#L175-L181

on line 178 it should evaluate the description

pajuga commented 9 months ago

With the 1.14.0 version, the ip_block module supports creating multiple IP blocks (same cidr and location) based on different descriptions as well as changing existing IP block descriptions by ID. The ip_block_info module also allows searching by location, description, and state. Described behavior should resolve core problem reported in the issue.