"Fossies" - the Fresh Open Source Software Archive

Member "ansible-2.9.27/lib/ansible/modules/network/aci/aci_config_snapshot.py" (11 Oct 2021, 9830 Bytes) of package /linux/misc/ansible-2.9.27.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "aci_config_snapshot.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.9.24_vs_4.3.0.

    1 #!/usr/bin/python
    2 # -*- coding: utf-8 -*-
    3 
    4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
    5 
    6 from __future__ import absolute_import, division, print_function
    7 __metaclass__ = type
    8 
    9 ANSIBLE_METADATA = {'metadata_version': '1.1',
   10                     'status': ['preview'],
   11                     'supported_by': 'certified'}
   12 
   13 DOCUMENTATION = r'''
   14 ---
   15 module: aci_config_snapshot
   16 short_description: Manage Config Snapshots (config:Snapshot, config:ExportP)
   17 description:
   18 - Manage Config Snapshots on Cisco ACI fabrics.
   19 - Creating new Snapshots is done using the configExportP class.
   20 - Removing Snapshots is done using the configSnapshot class.
   21 version_added: '2.4'
   22 options:
   23   description:
   24     description:
   25     - The description for the Config Export Policy.
   26     type: str
   27     aliases: [ descr ]
   28   export_policy:
   29     description:
   30     - The name of the Export Policy to use for Config Snapshots.
   31     type: str
   32     aliases: [ name ]
   33   format:
   34     description:
   35     - Sets the config backup to be formatted in JSON or XML.
   36     - The APIC defaults to C(json) when unset.
   37     type: str
   38     choices: [ json, xml ]
   39   include_secure:
   40     description:
   41     - Determines if secure information should be included in the backup.
   42     - The APIC defaults to C(yes) when unset.
   43     type: bool
   44   max_count:
   45     description:
   46     - Determines how many snapshots can exist for the Export Policy before the APIC starts to rollover.
   47     - Accepted values range between C(1) and C(10).
   48     - The APIC defaults to C(3) when unset.
   49     type: int
   50   snapshot:
   51     description:
   52     - The name of the snapshot to delete.
   53     type: str
   54   state:
   55     description:
   56     - Use C(present) or C(absent) for adding or removing.
   57     - Use C(query) for listing an object or multiple objects.
   58     type: str
   59     choices: [ absent, present, query ]
   60     default: present
   61 extends_documentation_fragment: aci
   62 notes:
   63 - The APIC does not provide a mechanism for naming the snapshots.
   64 - 'Snapshot files use the following naming structure: ce_<config export policy name>-<yyyy>-<mm>-<dd>T<hh>:<mm>:<ss>.<mss>+<hh>:<mm>.'
   65 - 'Snapshot objects use the following naming structure: run-<yyyy>-<mm>-<dd>T<hh>-<mm>-<ss>.'
   66 seealso:
   67 - module: aci_config_rollback
   68 - name: APIC Management Information Model reference
   69   description: More information about the internal APIC classes B(config:Snapshot) and B(config:ExportP).
   70   link: https://developer.cisco.com/docs/apic-mim-ref/
   71 author:
   72 - Jacob McGill (@jmcgill298)
   73 '''
   74 
   75 EXAMPLES = r'''
   76 - name: Create a Snapshot
   77   aci_config_snapshot:
   78     host: apic
   79     username: admin
   80     password: SomeSecretPassword
   81     state: present
   82     export_policy: config_backup
   83     max_count: 10
   84     description: Backups taken before new configs are applied.
   85   delegate_to: localhost
   86 
   87 - name: Query all Snapshots
   88   aci_config_snapshot:
   89     host: apic
   90     username: admin
   91     password: SomeSecretPassword
   92     state: query
   93   delegate_to: localhost
   94   register: query_result
   95 
   96 - name: Query Snapshots associated with a particular Export Policy
   97   aci_config_snapshot:
   98     host: apic
   99     username: admin
  100     password: SomeSecretPassword
  101     export_policy: config_backup
  102     state: query
  103   delegate_to: localhost
  104   register: query_result
  105 
  106 - name: Delete a Snapshot
  107   aci_config_snapshot:
  108     host: apic
  109     username: admin
  110     password: SomeSecretPassword
  111     export_policy: config_backup
  112     snapshot: run-2017-08-24T17-20-05
  113     state: absent
  114   delegate_to: localhost
  115 '''
  116 
  117 RETURN = r'''
  118 current:
  119   description: The existing configuration from the APIC after the module has finished
  120   returned: success
  121   type: list
  122   sample:
  123     [
  124         {
  125             "fvTenant": {
  126                 "attributes": {
  127                     "descr": "Production environment",
  128                     "dn": "uni/tn-production",
  129                     "name": "production",
  130                     "nameAlias": "",
  131                     "ownerKey": "",
  132                     "ownerTag": ""
  133                 }
  134             }
  135         }
  136     ]
  137 error:
  138   description: The error information as returned from the APIC
  139   returned: failure
  140   type: dict
  141   sample:
  142     {
  143         "code": "122",
  144         "text": "unknown managed object class foo"
  145     }
  146 raw:
  147   description: The raw output returned by the APIC REST API (xml or json)
  148   returned: parse error
  149   type: str
  150   sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
  151 sent:
  152   description: The actual/minimal configuration pushed to the APIC
  153   returned: info
  154   type: list
  155   sample:
  156     {
  157         "fvTenant": {
  158             "attributes": {
  159                 "descr": "Production environment"
  160             }
  161         }
  162     }
  163 previous:
  164   description: The original configuration from the APIC before the module has started
  165   returned: info
  166   type: list
  167   sample:
  168     [
  169         {
  170             "fvTenant": {
  171                 "attributes": {
  172                     "descr": "Production",
  173                     "dn": "uni/tn-production",
  174                     "name": "production",
  175                     "nameAlias": "",
  176                     "ownerKey": "",
  177                     "ownerTag": ""
  178                 }
  179             }
  180         }
  181     ]
  182 proposed:
  183   description: The assembled configuration from the user-provided parameters
  184   returned: info
  185   type: dict
  186   sample:
  187     {
  188         "fvTenant": {
  189             "attributes": {
  190                 "descr": "Production environment",
  191                 "name": "production"
  192             }
  193         }
  194     }
  195 filter_string:
  196   description: The filter string used for the request
  197   returned: failure or debug
  198   type: str
  199   sample: ?rsp-prop-include=config-only
  200 method:
  201   description: The HTTP method used for the request to the APIC
  202   returned: failure or debug
  203   type: str
  204   sample: POST
  205 response:
  206   description: The HTTP response from the APIC
  207   returned: failure or debug
  208   type: str
  209   sample: OK (30 bytes)
  210 status:
  211   description: The HTTP status from the APIC
  212   returned: failure or debug
  213   type: int
  214   sample: 200
  215 url:
  216   description: The HTTP url used for the request to the APIC
  217   returned: failure or debug
  218   type: str
  219   sample: https://10.11.12.13/api/mo/uni/tn-production.json
  220 '''
  221 
  222 from ansible.module_utils.basic import AnsibleModule
  223 from ansible.module_utils.network.aci.aci import ACIModule, aci_argument_spec
  224 
  225 
  226 def main():
  227     argument_spec = aci_argument_spec()
  228     argument_spec.update(
  229         description=dict(type='str', aliases=['descr']),
  230         export_policy=dict(type='str', aliases=['name']),  # Not required for querying all objects
  231         format=dict(type='str', choices=['json', 'xml']),
  232         include_secure=dict(type='bool'),
  233         max_count=dict(type='int'),
  234         snapshot=dict(type='str'),
  235         state=dict(type='str', choices=['absent', 'present', 'query'], default='present'),
  236     )
  237 
  238     module = AnsibleModule(
  239         argument_spec=argument_spec,
  240         supports_check_mode=False,
  241         required_if=[
  242             ['state', 'absent', ['export_policy', 'snapshot']],
  243             ['state', 'present', ['export_policy']],
  244         ],
  245     )
  246 
  247     aci = ACIModule(module)
  248 
  249     description = module.params['description']
  250     export_policy = module.params['export_policy']
  251     file_format = module.params['format']
  252     include_secure = aci.boolean(module.params['include_secure'])
  253     max_count = module.params['max_count']
  254     if max_count is not None:
  255         if max_count in range(1, 11):
  256             max_count = str(max_count)
  257         else:
  258             module.fail_json(msg="Parameter 'max_count' must be a number between 1 and 10")
  259     snapshot = module.params['snapshot']
  260     if snapshot is not None and not snapshot.startswith('run-'):
  261         snapshot = 'run-' + snapshot
  262     state = module.params['state']
  263 
  264     if state == 'present':
  265         aci.construct_url(
  266             root_class=dict(
  267                 aci_class='configExportP',
  268                 aci_rn='fabric/configexp-{0}'.format(export_policy),
  269                 module_object=export_policy,
  270                 target_filter={'name': export_policy},
  271             ),
  272         )
  273 
  274         aci.get_existing()
  275 
  276         aci.payload(
  277             aci_class='configExportP',
  278             class_config=dict(
  279                 adminSt='triggered',
  280                 descr=description,
  281                 format=file_format,
  282                 includeSecureFields=include_secure,
  283                 maxSnapshotCount=max_count,
  284                 name=export_policy,
  285                 snapshot='yes',
  286             ),
  287         )
  288 
  289         aci.get_diff('configExportP')
  290 
  291         # Create a new Snapshot
  292         aci.post_config()
  293 
  294     else:
  295         # Prefix the proper url to export_policy
  296         if export_policy is not None:
  297             export_policy = 'uni/fabric/configexp-{0}'.format(export_policy)
  298 
  299         aci.construct_url(
  300             root_class=dict(
  301                 aci_class='configSnapshotCont',
  302                 aci_rn='backupst/snapshots-[{0}]'.format(export_policy),
  303                 module_object=export_policy,
  304                 target_filter={'name': export_policy},
  305             ),
  306             subclass_1=dict(
  307                 aci_class='configSnapshot',
  308                 aci_rn='snapshot-{0}'.format(snapshot),
  309                 module_object=snapshot,
  310                 target_filter={'name': snapshot},
  311             ),
  312         )
  313 
  314         aci.get_existing()
  315 
  316         if state == 'absent':
  317             # Build POST request to used to remove Snapshot
  318             aci.payload(
  319                 aci_class='configSnapshot',
  320                 class_config=dict(
  321                     name=snapshot,
  322                     retire="yes",
  323                 ),
  324             )
  325 
  326             if aci.existing:
  327                 aci.get_diff('configSnapshot')
  328 
  329                 # Mark Snapshot for Deletion
  330                 aci.post_config()
  331 
  332     aci.exit_json()
  333 
  334 
  335 if __name__ == "__main__":
  336     main()