Empty tag values when trying to create object in ansible

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Please sign in to see details of an important advisory in our Customer Advisories area.

Empty tag values when trying to create object in ansible

L1 Bithead

In the examples i have seen

 

    - name: Create address objects
      paloaltonetworks.panos.panos_address_object:
        ip_address: "{{inventory_hostname}}"
        username: " {{ username }} "
        password: " {{ password }}"
        name: '{{ item.name }}'
        value: '{{ item.value }}'
        description: '{{ item.description }}'
        tag: '{{ item.tag | default([]) }}'
        commit: false

This seems to work, but when i try to create object with a loop with objects i have saved i get this error.

 

failed: [] (item={'value': 192.168.0.1/32', 'description': None, 'tag': None, 'name': 192.168.0.1_SOLARWINDS', 'address_type': 'ip-netmask'}) => {"ansible_loop_var": "item", "changed": false, "item": {"address_type": "ip-netmask", "description": null, "name": "192.168.0.1_SOLARWINDS", "tag": null, "value": "192.168.0.1/32"}, "msg": "Failed create: 192.168.0.1_SOLARWINDS -> tag is invalid"}

This is how the variable look that i try to use with a loop

 

"gathered": [
{
"address_type": "ip-netmask",
"description": null,
"name": "192.168.0.1_SOLARWINDS",
"tag": null,
"value": "192.168.0.1/32"
},

 

Do i miss anything?

1 accepted solution

Accepted Solutions

L5 Sessionator

Hi @zol123,

 

The default() empty set is not being used, it is passing through the "null" from your input variables. If you want to not set a tag if the input variable has tag=null, try omit for the default(), and ensure it gets used in the second parameter for the null use case (ref).

 

Also, ip_address/username/password are being deprecated, so please update your playbooks to use the provider parameter.

 

Example:

---
- name: Address objects with a loop
  hosts: '{{ target | default("host_labfw") }}'
  connection: local

  vars:
    device:
      ip_address: "{{ ip_address }}"
      username: "{{ username | default(omit) }}"
      password: "{{ password | default(omit) }}"
      api_key: "{{ api_key | default(omit) }}"
    address_objects:
      - {
        "address_type": "ip-netmask",
        "description": null,
        "name": "192.168.0.1_SOLARWINDS",
        "tag": "tag-one",
        "value": "192.168.0.1/32"
      }
      - {
        "address_type": "ip-netmask",
        "description": null,
        "name": "192.168.0.2_SOLARWINDS",
        "tag": null,
        "value": "192.168.0.2/32"
      }

  collections:
    - paloaltonetworks.panos

  tasks:
    - name: Create address objects
      paloaltonetworks.panos.panos_address_object:
        provider: '{{ device }}'
        name: '{{ item.name }}'
        value: '{{ item.value }}'
        description: '{{ item.description }}'
        tag: '{{ item.tag | default(omit, omit) }}'
        commit: false
      with_items: '{{ address_objects }}'

 

Result:

TASK [Create address objects] *******************************************************************************************************************************
changed: [host_labfw] => (item={'address_type': 'ip-netmask', 'description': None, 'name': '192.168.0.1_SOLARWINDS', 'tag': 'tag-one', 'value': '192.168.0.1/32'})
changed: [host_labfw] => (item={'address_type': 'ip-netmask', 'description': None, 'name': '192.168.0.2_SOLARWINDS', 'tag': None, 'value': '192.168.0.2/32'})

PLAY RECAP **************************************************************************************************************************************************
host_labfw                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

 

 

Hope that helps!

Help the community: "Like" helpful comments, and click "Accept as Solution" if you found your answer 🙂

View solution in original post

1 REPLY 1

L5 Sessionator

Hi @zol123,

 

The default() empty set is not being used, it is passing through the "null" from your input variables. If you want to not set a tag if the input variable has tag=null, try omit for the default(), and ensure it gets used in the second parameter for the null use case (ref).

 

Also, ip_address/username/password are being deprecated, so please update your playbooks to use the provider parameter.

 

Example:

---
- name: Address objects with a loop
  hosts: '{{ target | default("host_labfw") }}'
  connection: local

  vars:
    device:
      ip_address: "{{ ip_address }}"
      username: "{{ username | default(omit) }}"
      password: "{{ password | default(omit) }}"
      api_key: "{{ api_key | default(omit) }}"
    address_objects:
      - {
        "address_type": "ip-netmask",
        "description": null,
        "name": "192.168.0.1_SOLARWINDS",
        "tag": "tag-one",
        "value": "192.168.0.1/32"
      }
      - {
        "address_type": "ip-netmask",
        "description": null,
        "name": "192.168.0.2_SOLARWINDS",
        "tag": null,
        "value": "192.168.0.2/32"
      }

  collections:
    - paloaltonetworks.panos

  tasks:
    - name: Create address objects
      paloaltonetworks.panos.panos_address_object:
        provider: '{{ device }}'
        name: '{{ item.name }}'
        value: '{{ item.value }}'
        description: '{{ item.description }}'
        tag: '{{ item.tag | default(omit, omit) }}'
        commit: false
      with_items: '{{ address_objects }}'

 

Result:

TASK [Create address objects] *******************************************************************************************************************************
changed: [host_labfw] => (item={'address_type': 'ip-netmask', 'description': None, 'name': '192.168.0.1_SOLARWINDS', 'tag': 'tag-one', 'value': '192.168.0.1/32'})
changed: [host_labfw] => (item={'address_type': 'ip-netmask', 'description': None, 'name': '192.168.0.2_SOLARWINDS', 'tag': None, 'value': '192.168.0.2/32'})

PLAY RECAP **************************************************************************************************************************************************
host_labfw                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

 

 

Hope that helps!

Help the community: "Like" helpful comments, and click "Accept as Solution" if you found your answer 🙂
  • 1 accepted solution
  • 1459 Views
  • 1 replies
  • 0 Likes
Like what you see?

Show your appreciation!

Click Like if a post is helpful to you or if you just want to show your support.

Click Accept as Solution to acknowledge that the answer to your question has been provided.

The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!

These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!

The LIVEcommunity thanks you for your participation!