ansible-ruby icon indicating copy to clipboard operation
ansible-ruby copied to clipboard

Improve Parsing with Ansible argument_spec

Open wied03 opened this issue 8 years ago • 0 comments

In addition to looking at examples (or maybe replace examples since that parsing is quite brittle), use intercepted argument_spec values from AnsibleModule to form better metadata. Here is an example script that could be used for this:

from ansible.module_utils.basic import AnsibleModule
from mock import patch
import json
import importlib
import sys

def dump(args):
  as_json = json.dumps(args['argument_spec'])
  print as_json
  # Will cause the argument spec to be printed as JSON and then we exit without running the module
  sys.exit()

with patch.object(AnsibleModule, '__init__') as mock_method:
  mock_method.side_effect = lambda **args: dump(args)
  # Import module from command line
  importlib.import_module(sys.argv[1])

Then run PYTHONPATH=/usr/local/lib/python2.7/site-packages/ansible/modules/core/cloud/amazon python test.py ec2

Output:

{
   "kernel":{

   },
   "ramdisk":{

   },
   "image":{

   },
   "monitoring":{
      "default":false,
      "type":"bool"
   },
   "user_data":{

   },
   "termination_protection":{
      "default":null,
      "type":"bool"
   },
   "private_ip":{

   },
   "spot_type":{
      "default":"one-time",
      "choices":[
         "one-time",
         "persistent"
      ]
   },
   "ec2_url":{

   },
   "id":{

   },
   "source_dest_check":{
      "default":true,
      "type":"bool"
   },
   "aws_secret_key":{
      "no_log":true,
      "aliases":[
         "ec2_secret_key",
         "secret_key"
      ]
   },
   "spot_wait_timeout":{
      "default":600
   },
   "group":{
      "type":"list",
      "aliases":[
         "groups"
      ]
   },
   "zone":{
      "aliases":[
         "aws_zone",
         "ec2_zone"
      ]
   },
   "exact_count":{
      "default":null,
      "type":"int"
   },
   "ebs_optimized":{
      "default":false,
      "type":"bool"
   },
   "state":{
      "default":"present",
      "choices":[
         "present",
         "absent",
         "running",
         "restarted",
         "stopped"
      ]
   },
   "placement_group":{

   },
   "spot_launch_group":{

   },
   "key_name":{
      "aliases":[
         "keypair"
      ]
   },
   "profile":{

   },
   "count_tag":{

   },
   "vpc_subnet_id":{

   },
   "instance_ids":{
      "type":"list",
      "aliases":[
         "instance_id"
      ]
   },
   "tenancy":{
      "default":"default"
   },
   "assign_public_ip":{
      "default":false,
      "type":"bool"
   },
   "spot_price":{

   },
   "wait":{
      "default":false,
      "type":"bool"
   },
   "count":{
      "default":"1",
      "type":"int"
   },
   "aws_access_key":{
      "aliases":[
         "ec2_access_key",
         "access_key"
      ]
   },
   "instance_profile_name":{

   },
   "security_token":{
      "no_log":true,
      "aliases":[
         "access_token"
      ]
   },
   "region":{
      "aliases":[
         "aws_region",
         "ec2_region"
      ]
   },
   "network_interfaces":{
      "type":"list",
      "aliases":[
         "network_interface"
      ]
   },
   "instance_initiated_shutdown_behavior":{
      "default":null,
      "choices":[
         "stop",
         "terminate"
      ]
   },
   "instance_type":{
      "aliases":[
         "type"
      ]
   },
   "wait_timeout":{
      "default":300
   },
   "volumes":{
      "type":"list"
   },
   "instance_tags":{
      "type":"dict"
   },
   "group_id":{
      "type":"list"
   },
   "validate_certs":{
      "default":true,
      "type":"bool"
   }
}

wied03 avatar Mar 16 '17 20:03 wied03