From ad0d2c17470725ca2c5a1026145e680d9846ef4f Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Mon, 11 Jan 2016 12:47:21 -0800 Subject: [PATCH] Update for modules which import json. Some do not use the json module directly so don't need import json. Some needed to fallback to simplejson with no traceback if neither was installed Fixes #1298 --- .../modules/extras/cloud/amazon/ecs_task.py | 3 +-- .../extras/cloud/amazon/ecs_taskdefinition.py | 3 +-- .../modules/extras/cloud/amazon/route53_facts.py | 1 - lib/ansible/modules/extras/cloud/vmware/vca_nat.py | 1 - lib/ansible/modules/extras/clustering/consul.py | 5 ----- lib/ansible/modules/extras/clustering/consul_kv.py | 5 ----- lib/ansible/modules/extras/database/misc/riak.py | 7 ++++++- .../modules/extras/monitoring/boundary_meter.py | 10 +++++++++- .../modules/extras/monitoring/sensu_check.py | 14 +++++++++----- .../modules/extras/monitoring/stackdriver.py | 10 ++++++++-- .../modules/extras/monitoring/uptimerobot.py | 10 +++++++++- lib/ansible/modules/extras/network/ipify_facts.py | 7 ++++++- .../modules/extras/packaging/language/composer.py | 7 ++++++- .../modules/extras/packaging/language/npm.py | 7 ++++++- lib/ansible/modules/extras/packaging/os/pacman.py | 1 - lib/ansible/modules/extras/packaging/os/pkgin.py | 1 - lib/ansible/modules/extras/packaging/os/pkgng.py | 1 - .../modules/extras/packaging/os/portinstall.py | 1 - lib/ansible/modules/extras/packaging/os/urpmi.py | 1 - .../modules/extras/source_control/github_hooks.py | 10 +++++++++- lib/ansible/modules/extras/system/puppet.py | 7 ++++++- .../modules/extras/web_infrastructure/jira.py | 10 +++++++++- 22 files changed, 85 insertions(+), 37 deletions(-) diff --git a/lib/ansible/modules/extras/cloud/amazon/ecs_task.py b/lib/ansible/modules/extras/cloud/amazon/ecs_task.py index d8c15fca9c..55e376fe70 100644 --- a/lib/ansible/modules/extras/cloud/amazon/ecs_task.py +++ b/lib/ansible/modules/extras/cloud/amazon/ecs_task.py @@ -95,7 +95,6 @@ task: sample: "TODO: include sample" ''' try: - import json import boto import botocore HAS_BOTO = True @@ -120,7 +119,7 @@ class EcsExecManager: module.fail_json(msg="Region must be specified as a parameter, in EC2_REGION or AWS_REGION environment variables or in boto configuration file") self.ecs = boto3_conn(module, conn_type='client', resource='ecs', region=region, endpoint=ec2_url, **aws_connect_kwargs) except boto.exception.NoAuthHandlerFound, e: - self.module.fail_json(msg=str(e)) + module.fail_json(msg="Can't authorize connection - "+str(e)) def list_tasks(self, cluster_name, service_name, status): response = self.ecs.list_tasks( diff --git a/lib/ansible/modules/extras/cloud/amazon/ecs_taskdefinition.py b/lib/ansible/modules/extras/cloud/amazon/ecs_taskdefinition.py index 4a044ed3c6..5f183654db 100644 --- a/lib/ansible/modules/extras/cloud/amazon/ecs_taskdefinition.py +++ b/lib/ansible/modules/extras/cloud/amazon/ecs_taskdefinition.py @@ -93,7 +93,6 @@ taskdefinition: type: dict inputs plus revision, status, taskDefinitionArn ''' try: - import json import boto import botocore HAS_BOTO = True @@ -118,7 +117,7 @@ class EcsTaskManager: module.fail_json(msg="Region must be specified as a parameter, in EC2_REGION or AWS_REGION environment variables or in boto configuration file") self.ecs = boto3_conn(module, conn_type='client', resource='ecs', region=region, endpoint=ec2_url, **aws_connect_kwargs) except boto.exception.NoAuthHandlerFound, e: - self.module.fail_json(msg=str(e)) + module.fail_json(msg="Can't authorize connection - "+str(e)) def describe_task(self, task_name): try: diff --git a/lib/ansible/modules/extras/cloud/amazon/route53_facts.py b/lib/ansible/modules/extras/cloud/amazon/route53_facts.py index 16034acb51..153377aba8 100644 --- a/lib/ansible/modules/extras/cloud/amazon/route53_facts.py +++ b/lib/ansible/modules/extras/cloud/amazon/route53_facts.py @@ -160,7 +160,6 @@ EXAMPLES = ''' ''' try: - import json import boto import botocore HAS_BOTO = True diff --git a/lib/ansible/modules/extras/cloud/vmware/vca_nat.py b/lib/ansible/modules/extras/cloud/vmware/vca_nat.py index 88fc24a20f..2a464673e5 100644 --- a/lib/ansible/modules/extras/cloud/vmware/vca_nat.py +++ b/lib/ansible/modules/extras/cloud/vmware/vca_nat.py @@ -130,7 +130,6 @@ EXAMPLES = ''' ''' import time -import json import xmltodict VALID_RULE_KEYS = ['rule_type', 'original_ip', 'original_port', diff --git a/lib/ansible/modules/extras/clustering/consul.py b/lib/ansible/modules/extras/clustering/consul.py index 609dce8922..627f7fb66a 100644 --- a/lib/ansible/modules/extras/clustering/consul.py +++ b/lib/ansible/modules/extras/clustering/consul.py @@ -190,11 +190,6 @@ EXAMPLES = ''' import sys -try: - import json -except ImportError: - import simplejson as json - try: import consul from requests.exceptions import ConnectionError diff --git a/lib/ansible/modules/extras/clustering/consul_kv.py b/lib/ansible/modules/extras/clustering/consul_kv.py index bb7dea3ad3..b61c0ee184 100644 --- a/lib/ansible/modules/extras/clustering/consul_kv.py +++ b/lib/ansible/modules/extras/clustering/consul_kv.py @@ -122,11 +122,6 @@ EXAMPLES = ''' import sys -try: - import json -except ImportError: - import simplejson as json - try: import consul from requests.exceptions import ConnectionError diff --git a/lib/ansible/modules/extras/database/misc/riak.py b/lib/ansible/modules/extras/database/misc/riak.py index 453e6c15f3..1f1cd11e92 100644 --- a/lib/ansible/modules/extras/database/misc/riak.py +++ b/lib/ansible/modules/extras/database/misc/riak.py @@ -100,10 +100,15 @@ EXAMPLES = ''' import time import socket import sys + try: import json except ImportError: - import simplejson as json + try: + import simplejson as json + except ImportError: + # Let snippet from module_utils/basic.py return a proper error in this case + pass def ring_check(module, riak_admin_bin): diff --git a/lib/ansible/modules/extras/monitoring/boundary_meter.py b/lib/ansible/modules/extras/monitoring/boundary_meter.py index 99cb74f870..ef681704f0 100644 --- a/lib/ansible/modules/extras/monitoring/boundary_meter.py +++ b/lib/ansible/modules/extras/monitoring/boundary_meter.py @@ -22,7 +22,15 @@ You should have received a copy of the GNU General Public License along with Ansible. If not, see . """ -import json +try: + import json +except ImportError: + try: + import simplejson as json + except ImportError: + # Let snippet from module_utils/basic.py return a proper error in this case + pass + import datetime import base64 import os diff --git a/lib/ansible/modules/extras/monitoring/sensu_check.py b/lib/ansible/modules/extras/monitoring/sensu_check.py index 9a004d372e..09edae6381 100644 --- a/lib/ansible/modules/extras/monitoring/sensu_check.py +++ b/lib/ansible/modules/extras/monitoring/sensu_check.py @@ -174,16 +174,20 @@ EXAMPLES = ''' sensu_check: name=check_disk_capacity state=absent ''' +try: + import json +except ImportError: + try: + import simplejson as json + except ImportError: + # Let snippet from module_utils/basic.py return a proper error in this case + pass + def sensu_check(module, path, name, state='present', backup=False): changed = False reasons = [] - try: - import json - except ImportError: - import simplejson as json - stream = None try: try: diff --git a/lib/ansible/modules/extras/monitoring/stackdriver.py b/lib/ansible/modules/extras/monitoring/stackdriver.py index 65e88740e3..a446fa83d9 100644 --- a/lib/ansible/modules/extras/monitoring/stackdriver.py +++ b/lib/ansible/modules/extras/monitoring/stackdriver.py @@ -92,10 +92,16 @@ EXAMPLES = ''' # =========================================== # Stackdriver module specific support methods. # + try: - import json + import json except ImportError: - import simplejson as json + try: + import simplejson as json + except ImportError: + # Let snippet from module_utils/basic.py return a proper error in this case + pass + def send_deploy_event(module, key, revision_id, deployed_by='Ansible', deployed_to=None, repository=None): """Send a deploy event to Stackdriver""" diff --git a/lib/ansible/modules/extras/monitoring/uptimerobot.py b/lib/ansible/modules/extras/monitoring/uptimerobot.py index 58abe3946c..2fafd9b9c3 100644 --- a/lib/ansible/modules/extras/monitoring/uptimerobot.py +++ b/lib/ansible/modules/extras/monitoring/uptimerobot.py @@ -64,7 +64,15 @@ EXAMPLES = ''' ''' -import json +try: + import json +except ImportError: + try: + import simplejson as json + except ImportError: + # Let snippet from module_utils/basic.py return a proper error in this case + pass + import urllib import time diff --git a/lib/ansible/modules/extras/network/ipify_facts.py b/lib/ansible/modules/extras/network/ipify_facts.py index 8f509dd278..95bf549be9 100644 --- a/lib/ansible/modules/extras/network/ipify_facts.py +++ b/lib/ansible/modules/extras/network/ipify_facts.py @@ -59,7 +59,12 @@ ipify_public_ip: try: import json except ImportError: - import simplejson as json + try: + import simplejson as json + except ImportError: + # Let snippet from module_utils/basic.py return a proper error in this case + pass + class IpifyFacts(object): diff --git a/lib/ansible/modules/extras/packaging/language/composer.py b/lib/ansible/modules/extras/packaging/language/composer.py index 95b0eb3a94..5d1ec7b101 100644 --- a/lib/ansible/modules/extras/packaging/language/composer.py +++ b/lib/ansible/modules/extras/packaging/language/composer.py @@ -128,7 +128,12 @@ import re try: import json except ImportError: - import simplejson as json + try: + import simplejson as json + except ImportError: + # Let snippet from module_utils/basic.py return a proper error in this case + pass + def parse_out(string): return re.sub("\s+", " ", string).strip() diff --git a/lib/ansible/modules/extras/packaging/language/npm.py b/lib/ansible/modules/extras/packaging/language/npm.py index a52b7599d8..43fa1f325f 100644 --- a/lib/ansible/modules/extras/packaging/language/npm.py +++ b/lib/ansible/modules/extras/packaging/language/npm.py @@ -107,7 +107,12 @@ import os try: import json except ImportError: - import simplejson as json + try: + import simplejson as json + except ImportError: + # Let snippet from module_utils/basic.py return a proper error in this case + pass + class Npm(object): def __init__(self, module, **kwargs): diff --git a/lib/ansible/modules/extras/packaging/os/pacman.py b/lib/ansible/modules/extras/packaging/os/pacman.py index 1f955fa269..7aa5bf45ea 100644 --- a/lib/ansible/modules/extras/packaging/os/pacman.py +++ b/lib/ansible/modules/extras/packaging/os/pacman.py @@ -109,7 +109,6 @@ EXAMPLES = ''' - pacman: name=baz state=absent force=yes ''' -import json import shlex import os import re diff --git a/lib/ansible/modules/extras/packaging/os/pkgin.py b/lib/ansible/modules/extras/packaging/os/pkgin.py index 0f2714b6c7..cdba6a9218 100644 --- a/lib/ansible/modules/extras/packaging/os/pkgin.py +++ b/lib/ansible/modules/extras/packaging/os/pkgin.py @@ -63,7 +63,6 @@ EXAMPLES = ''' ''' -import json import shlex import os import sys diff --git a/lib/ansible/modules/extras/packaging/os/pkgng.py b/lib/ansible/modules/extras/packaging/os/pkgng.py index f1fe3c460b..a78bda27df 100644 --- a/lib/ansible/modules/extras/packaging/os/pkgng.py +++ b/lib/ansible/modules/extras/packaging/os/pkgng.py @@ -85,7 +85,6 @@ EXAMPLES = ''' ''' -import json import shlex import os import re diff --git a/lib/ansible/modules/extras/packaging/os/portinstall.py b/lib/ansible/modules/extras/packaging/os/portinstall.py index b4e3044167..a5d0e51097 100644 --- a/lib/ansible/modules/extras/packaging/os/portinstall.py +++ b/lib/ansible/modules/extras/packaging/os/portinstall.py @@ -58,7 +58,6 @@ EXAMPLES = ''' ''' -import json import shlex import os import sys diff --git a/lib/ansible/modules/extras/packaging/os/urpmi.py b/lib/ansible/modules/extras/packaging/os/urpmi.py index d344f2e7c5..0b9ec92931 100644 --- a/lib/ansible/modules/extras/packaging/os/urpmi.py +++ b/lib/ansible/modules/extras/packaging/os/urpmi.py @@ -73,7 +73,6 @@ EXAMPLES = ''' ''' -import json import shlex import os import sys diff --git a/lib/ansible/modules/extras/source_control/github_hooks.py b/lib/ansible/modules/extras/source_control/github_hooks.py index d75fcb1573..9f66487558 100644 --- a/lib/ansible/modules/extras/source_control/github_hooks.py +++ b/lib/ansible/modules/extras/source_control/github_hooks.py @@ -18,7 +18,15 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . -import json +try: + import json +except ImportError: + try: + import simplejson as json + except ImportError: + # Let snippet from module_utils/basic.py return a proper error in this case + pass + import base64 DOCUMENTATION = ''' diff --git a/lib/ansible/modules/extras/system/puppet.py b/lib/ansible/modules/extras/system/puppet.py index 2a70da3cea..d4f69b1d51 100644 --- a/lib/ansible/modules/extras/system/puppet.py +++ b/lib/ansible/modules/extras/system/puppet.py @@ -22,7 +22,12 @@ import stat try: import json except ImportError: - import simplejson as json + try: + import simplejson as json + except ImportError: + # Let snippet from module_utils/basic.py return a proper error in this case + pass + DOCUMENTATION = ''' --- diff --git a/lib/ansible/modules/extras/web_infrastructure/jira.py b/lib/ansible/modules/extras/web_infrastructure/jira.py index 79cfb72d4a..dded069f74 100644 --- a/lib/ansible/modules/extras/web_infrastructure/jira.py +++ b/lib/ansible/modules/extras/web_infrastructure/jira.py @@ -160,7 +160,15 @@ EXAMPLES = """ issue={{issue.meta.key}} operation=transition status="Done" """ -import json +try: + import json +except ImportError: + try: + import simplejson as json + except ImportError: + # Let snippet from module_utils/basic.py return a proper error in this case + pass + import base64 def request(url, user, passwd, data=None, method=None):