Repository: ansible-collections/community.general

Issue:
Cobbler module cobbler_system validate_certs error
**Summary**

When using the `cobbler_system` with `validate_certs` flag set to `no`.   The module is validating certificate:
```
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Connection to 'https://xxxx.domain:443/cobbler_api' failed. [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)"}
```

The bug was present in the main project. See [here](https://github.com/ansible/ansible/issues/52239)


**Issue Type**

Bug Report

**Component Name**

cobbler_system

**Ansible Version**

```paste below
ansible-playbook 2.9.14
  config file = /home/nico/Documents/tower-automation/ansible.cfg
  configured module search path = ['/home/nico/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/nico/.local/pipx/venvs/ansible/lib/python3.8/site-packages/ansible
  executable location = /home/nico/.local/bin/ansible-playbook
  python version = 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0]
Using /home/nico/Documents/tower-automation/ansible.cfg as config file

```


**Configuration**

```paste below
default conf. No changes
```


**OS / Environment**

Ubuntu 20.04

**Steps To Reproduce**

When using the `cobbler_system` with `validate_certs` flag set to `no`
```yml
    - name: Create a system
      cobbler_system:
        host: terre.gre.hpecorp.net
        validate_certs: no
        use_ssl: true
        username: me
        password: mypass
        name: siam014.bck
        properties:
          hostname: siam014.gre.hpecorp.net
          profile: HOE-CENTOS8.3-SPP2009-x86_64-NOPARTNUMBER-RC1
        interfaces:
          eth0:
            macaddress: 00:50:56:a6:3a:33
            ipaddress: 16.17.88.14
            dhcptag: cms_vlan152
            dnsname: siam014.gre.hpecorp.net
        sync: false
      delegate_to: localhost
```


**Expected Results**

System created without any error

**Actual Results**

The ssl cert check is not skipped.
```paste below
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Connection to 'https://terre.gre.hpecorp.net:443/cobbler_api' failed. [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)"}
```





Discussion:
Files identified in the description:
* [`plugins/modules/remote_management/cobbler/cobbler_system.py`](https://github.com/ansible-collections/community.general/blob/master/plugins/modules/remote_management/cobbler/cobbler_system.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @dagwieers
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/plugins/modules/remote_management/cobbler/cobbler_sync.py b/plugins/modules/remote_management/cobbler/cobbler_sync.py
--- a/plugins/modules/remote_management/cobbler/cobbler_sync.py
+++ b/plugins/modules/remote_management/cobbler/cobbler_sync.py
@@ -106,12 +106,14 @@ def main():
 
     ssl_context = None
     if not validate_certs:
-        try:  # Python 2.7.9 and newer
-            ssl_context = ssl.create_unverified_context()
-        except AttributeError:  # Legacy Python that doesn't verify HTTPS certificates by default
-            ssl._create_default_context = ssl._create_unverified_context
-        else:  # Python 2.7.8 and older
-            ssl._create_default_https_context = ssl._create_unverified_https_context
+        try:
+            ssl_context = ssl._create_unverified_context()
+        except AttributeError:
+            # Legacy Python that doesn't verify HTTPS certificates by default
+            pass
+        else:
+            # Handle target environment that doesn't support HTTPS verification
+            ssl._create_default_https_context = ssl._create_unverified_context
 
     url = '{proto}://{host}:{port}/cobbler_api'.format(**module.params)
     if ssl_context:
diff --git a/plugins/modules/remote_management/cobbler/cobbler_system.py b/plugins/modules/remote_management/cobbler/cobbler_system.py
--- a/plugins/modules/remote_management/cobbler/cobbler_system.py
+++ b/plugins/modules/remote_management/cobbler/cobbler_system.py
@@ -229,12 +229,14 @@ def main():
 
     ssl_context = None
     if not validate_certs:
-        try:  # Python 2.7.9 and newer
-            ssl_context = ssl.create_unverified_context()
-        except AttributeError:  # Legacy Python that doesn't verify HTTPS certificates by default
-            ssl._create_default_context = ssl._create_unverified_context
-        else:  # Python 2.7.8 and older
-            ssl._create_default_https_context = ssl._create_unverified_https_context
+        try:
+            ssl_context = ssl._create_unverified_context()
+        except AttributeError:
+            # Legacy Python that doesn't verify HTTPS certificates by default
+            pass
+        else:
+            # Handle target environment that doesn't support HTTPS verification
+            ssl._create_default_https_context = ssl._create_unverified_context
 
     url = '{proto}://{host}:{port}/cobbler_api'.format(**module.params)
     if ssl_context:


Test patch: