<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: panos remove address object script messes up the device group setup. in Automation/API Discussions</title>
    <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/panos-remove-address-object-script-messes-up-the-device-group/m-p/544311#M3380</link>
    <description>&lt;P&gt;It took me some time to apply some trial and error and reading through the docs. What I've been doing in the line with dg.apply() seems to be the cause on why the device group members are gone on panorama after running the code. Do not try dg.apply() in production as it seems to be destructive based on the documentation.&lt;BR /&gt;&lt;BR /&gt;Here is an updated snippet to achieve the deletion of a single address object via panorama:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from panos.panorama import Panorama, DeviceGroup
from panos.objects import AddressObject

username = "Your_Panorama_Username"
password = "Your_Panorama_Pw"
pano = Panorama(panorama, username, password)

target_dg = "test_dg_1"
addrObjName = "Sample_Address_Object"

dg = DeviceGroup(target_dg)
pano.add(dg)

# Get the address objects list.
addressObjects = AddressObject.refreshall(dg)

# Loop over the address objects to find out if the object exists or not.
# No action if the object does not exist in the current address object list.
for o in addressObjects:
    if addrObjName == o.name:

        print(f'Found {addrObjName}. Removing...')
        o.delete()

        # Perform commit and push
        pano.commit(cmd = commitMesg, sync=True)
        pano.commit_all(sync_all=True, devicegroup=target_dg)

        # Exit script after deletion of address object &amp;amp; commit + push.
        exit()

print(f'Unable to match address object {addrObjName}. No changes were made.')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;Useful methods documentation:&lt;BR /&gt;&lt;A href="https://pan-os-python.readthedocs.io/en/latest/useful-methods.html" target="_blank" rel="noopener"&gt;https://pan-os-python.readthedocs.io/en/latest/useful-methods.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 01 Jun 2023 09:24:32 GMT</pubDate>
    <dc:creator>Asurion_NetOps</dc:creator>
    <dc:date>2023-06-01T09:24:32Z</dc:date>
    <item>
      <title>panos remove address object script messes up the device group setup.</title>
      <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/panos-remove-address-object-script-messes-up-the-device-group/m-p/544273#M3379</link>
      <description>&lt;P&gt;Hi All,&lt;BR /&gt;&lt;BR /&gt;So far I'm getting the hang of python panos, which allows me to connect via panorama to make changes and push to firewalls. I've been able to create address objects and modify contents of address groups and security rules with no issues.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Recently I'm trying to create a script that would delete an address object using python panos. What happens when I run the script is panorama removes my firewalls in the device group, which is weird. I need to manually add the devices and template stack again to the device group in order to restore it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Initially I tried to use dg.delete() but it did not work for me. I replaced it with dg.remove() but the same thing happens. Maybe I'm messing up the configuration tree but I do not know which part of my code does that.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Panorama version: 10.2.3&lt;BR /&gt;pan-os-python version: 1.8.1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below are some snippets from my code:&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;LI-CODE lang="python"&gt;from panos.panorama import Panorama, DeviceGroup
from panos.objects import AddressObject

pano = Panorama(panorama, username, password)

target_dg = "test_dg_1"

dg = DeviceGroup(target_dg)
pano.add(dg)

#Find the address object within the device group
obj = dg.find("Sample_Address_Object", class_type=AddressObject)

# Remove the address object
dg.remove(obj)
dg.apply()

pano.commit(cmd = commitMesg, sync=True)
pano.commit_all(sync_all=True, devicegroup=target_dg)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be appreciated. Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2023 08:59:59 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/automation-api-discussions/panos-remove-address-object-script-messes-up-the-device-group/m-p/544273#M3379</guid>
      <dc:creator>Asurion_NetOps</dc:creator>
      <dc:date>2023-06-01T08:59:59Z</dc:date>
    </item>
    <item>
      <title>Re: panos remove address object script messes up the device group setup.</title>
      <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/panos-remove-address-object-script-messes-up-the-device-group/m-p/544311#M3380</link>
      <description>&lt;P&gt;It took me some time to apply some trial and error and reading through the docs. What I've been doing in the line with dg.apply() seems to be the cause on why the device group members are gone on panorama after running the code. Do not try dg.apply() in production as it seems to be destructive based on the documentation.&lt;BR /&gt;&lt;BR /&gt;Here is an updated snippet to achieve the deletion of a single address object via panorama:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from panos.panorama import Panorama, DeviceGroup
from panos.objects import AddressObject

username = "Your_Panorama_Username"
password = "Your_Panorama_Pw"
pano = Panorama(panorama, username, password)

target_dg = "test_dg_1"
addrObjName = "Sample_Address_Object"

dg = DeviceGroup(target_dg)
pano.add(dg)

# Get the address objects list.
addressObjects = AddressObject.refreshall(dg)

# Loop over the address objects to find out if the object exists or not.
# No action if the object does not exist in the current address object list.
for o in addressObjects:
    if addrObjName == o.name:

        print(f'Found {addrObjName}. Removing...')
        o.delete()

        # Perform commit and push
        pano.commit(cmd = commitMesg, sync=True)
        pano.commit_all(sync_all=True, devicegroup=target_dg)

        # Exit script after deletion of address object &amp;amp; commit + push.
        exit()

print(f'Unable to match address object {addrObjName}. No changes were made.')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;Useful methods documentation:&lt;BR /&gt;&lt;A href="https://pan-os-python.readthedocs.io/en/latest/useful-methods.html" target="_blank" rel="noopener"&gt;https://pan-os-python.readthedocs.io/en/latest/useful-methods.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2023 09:24:32 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/automation-api-discussions/panos-remove-address-object-script-messes-up-the-device-group/m-p/544311#M3380</guid>
      <dc:creator>Asurion_NetOps</dc:creator>
      <dc:date>2023-06-01T09:24:32Z</dc:date>
    </item>
  </channel>
</rss>

