fortinet / ansible-fortimanager-generic

8 stars 6 forks source link

update README #11

Closed chillancezen closed 4 years ago

chillancezen commented 4 years ago

added one section: customize error checking. this is also supposed to resolve the issues in https://fndn.fortinet.net/index.php?/forums/topic/2088-fortimanager-generic-module/

@frankshen01

chillancezen commented 4 years ago

playbook:

#cat test_add_device.yml
- name: Test API
  hosts: fortimanager01
  gather_facts: no
  connection: httpapi

  collections:
    - fortinet.fortimanager

  vars:
    ansible_httpapi_use_ssl: True
    ansible_httpapi_validate_certs: False
    ansible_httpapi_port: 443

  tasks:
    - name: Provisioning
      fmgr_generic:
         method: exec
         params:
            -  url: /dvm/cmd/add/device
               data:
                  adom: root
                  device:
                     desc: Provisioned by ansible
                     device action: add_model
                     mgmt_mode: fmg
                     mr: 2
                     name: fortiVM01
                     os_type: fos
                     os_ver: '6.0'
                     sn: FGVMEVHBR3TXNV04
                  flags:
                    - none
      register: provision
      ignore_errors: yes

    - name: fail the play
      fail:
        msg: "the ... fail"
      failed_when: provision.rc != 0 and provision.rc != -20010

result:

#ansible-playbook -i hosts -vvv  test_add_device.yml
ansible-playbook 2.9.5
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/dist-packages/ansible
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.6.9 (default, Nov  7 2019, 10:44:02) [GCC 8.3.0]
Using /etc/ansible/ansible.cfg as config file
host_list declined parsing /root/DEV/ansible-fortimanager-generic/hosts as it did not pass its verify_file() method
script declined parsing /root/DEV/ansible-fortimanager-generic/hosts as it did not pass its verify_file() method
auto declined parsing /root/DEV/ansible-fortimanager-generic/hosts as it did not pass its verify_file() method
Parsed /root/DEV/ansible-fortimanager-generic/hosts inventory source with ini plugin

PLAYBOOK: test_add_device.yml **************************************************************************************************************************
1 plays in test_add_device.yml

PLAY [Test API] ****************************************************************************************************************************************
META: ran handlers

TASK [Provisioning] ************************************************************************************************************************************
task path: /root/DEV/ansible-fortimanager-generic/test_add_device.yml:16
<192.168.190.102> ESTABLISH LOCAL CONNECTION FOR USER: root
<192.168.190.102> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-local-3729pj8ojmib/ansible-tmp-1585236904.7293122-179076395604584 `" && echo ansible-tmp-1585236904.7293122-179076395604584="` echo /root/.ansible/tmp/ansible-local-3729pj8ojmib/ansible-tmp-1585236904.7293122-179076395604584 `" ) && sleep 0'
<fortimanager01> Attempting python interpreter discovery
<192.168.190.102> EXEC /bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v '"'"'/usr/bin/python'"'"'; command -v '"'"'python3.7'"'"'; command -v '"'"'python3.6'"'"'; command -v '"'"'python3.5'"'"'; command -v '"'"'python2.7'"'"'; command -v '"'"'python2.6'"'"'; command -v '"'"'/usr/libexec/platform-python'"'"'; command -v '"'"'/usr/bin/python3'"'"'; command -v '"'"'python'"'"'; echo ENDFOUND && sleep 0'
<192.168.190.102> EXEC /bin/sh -c '/usr/bin/python && sleep 0'
Using module file /usr/local/lib/python3.6/dist-packages/ansible/modules/network/fortimanager/fmgr_generic.py
<192.168.190.102> PUT /root/.ansible/tmp/ansible-local-3729pj8ojmib/tmp_7060k0z TO /root/.ansible/tmp/ansible-local-3729pj8ojmib/ansible-tmp-1585236904.7293122-179076395604584/AnsiballZ_fmgr_generic.py
<192.168.190.102> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-local-3729pj8ojmib/ansible-tmp-1585236904.7293122-179076395604584/ /root/.ansible/tmp/ansible-local-3729pj8ojmib/ansible-tmp-1585236904.7293122-179076395604584/AnsiballZ_fmgr_generic.py && sleep 0'
<192.168.190.102> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-local-3729pj8ojmib/ansible-tmp-1585236904.7293122-179076395604584/AnsiballZ_fmgr_generic.py && sleep 0'
<192.168.190.102> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-local-3729pj8ojmib/ansible-tmp-1585236904.7293122-179076395604584/ > /dev/null 2>&1 && sleep 0'
[DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host fortimanager01 should use /usr/bin/python3, but is using /usr/bin/python for backward
compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version
2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
fatal: [fortimanager01]: FAILED! => {
    "ansible_facts": {
        "ansible_params": {
            "method": "exec",
            "params": [
                {
                    "data": {
                        "adom": "root",
                        "device": {
                            "desc": "Provisioned by ansible",
                            "device action": "add_model",
                            "mgmt_mode": "fmg",
                            "mr": 2,
                            "name": "fortiVM01",
                            "os_type": "fos",
                            "os_ver": "6.0",
                            "sn": "FGVMEVHBR3TXNV04"
                        },
                        "flags": [
                            "none"
                        ]
                    },
                    "url": "/dvm/cmd/add/device"
                }
            ]
        },
        "connected_fmgr": {
            "Admin Domain Configuration": "Disabled",
            "BIOS version": "04000002",
            "Branch Point": "0255",
            "Build": "0255",
            "Current Time": "Thu Mar 26 16:35:04 PDT 2020",
            "Daylight Time Saving": "Yes",
            "FIPS Mode": "Disabled",
            "HA Mode": "Stand Alone",
            "Hostname": "FMG-VM64",
            "License Status": "Valid",
            "Major": 6,
            "Max Number of Admin Domains": 10000,
            "Max Number of Device Groups": 10000,
            "Minor": 0,
            "Offline Mode": "Disabled",
            "Patch": 3,
            "Platform Full Name": "FortiManager-VM64",
            "Platform Type": "FMG-VM64",
            "Release Version Information": " (GA)",
            "Serial Number": "FMG-VMTM19008442",
            "Time Zone": "(GMT-8:00) Pacific Time (US & Canada).",
            "Version": "v6.0.3-build0255 181102 (GA)",
            "x86-64 Applications": "Yes"
        },
        "discovered_interpreter_python": "/usr/bin/python",
        "paramgram": {
            "method": "exec",
            "params": [
                {
                    "data": {
                        "adom": "root",
                        "device": {
                            "desc": "Provisioned by ansible",
                            "device action": "add_model",
                            "mgmt_mode": "fmg",
                            "mr": 2,
                            "name": "fortiVM01",
                            "os_type": "fos",
                            "os_ver": "6.0",
                            "sn": "FGVMEVHBR3TXNV04"
                        },
                        "flags": [
                            "none"
                        ]
                    },
                    "url": "/dvm/cmd/add/device"
                }
            ]
        },
        "response": [
            -20025,
            {
                "status": {
                    "code": -20025,
                    "message": "Unsupported device version"
                },
                "url": "/dvm/cmd/add/device"
            }
        ]
    },
    "ansible_module_results": {
        "status": {
            "code": -20025,
            "message": "Unsupported device version"
        },
        "url": "/dvm/cmd/add/device"
    },
    "changed": false,
    "invocation": {
        "module_args": {
            "method": "exec",
            "params": [
                {
                    "data": {
                        "adom": "root",
                        "device": {
                            "desc": "Provisioned by ansible",
                            "device action": "add_model",
                            "mgmt_mode": "fmg",
                            "mr": 2,
                            "name": "fortiVM01",
                            "os_type": "fos",
                            "os_ver": "6.0",
                            "sn": "FGVMEVHBR3TXNV04"
                        },
                        "flags": [
                            "none"
                        ]
                    },
                    "url": "/dvm/cmd/add/device"
                }
            ]
        }
    },
    "msg": "Operation Finished",
    "rc": -20025,
    "unreachable": false
}
...ignoring

TASK [fail the play] ***********************************************************************************************************************************
task path: /root/DEV/ansible-fortimanager-generic/test_add_device.yml:37
<192.168.190.102> ESTABLISH HTTP(S) CONNECTFOR USER: APIUser TO https://192.168.190.102:443
fatal: [fortimanager01]: FAILED! => {
    "changed": false,
    "failed_when_result": true,
    "msg": "the ... fail"
}

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

the result of first task is always ignored.