unable to run the playbook python module is unable to call from playbook task | error is The offending line appears to be: - name: minify_json minify_json.py that is module i am calling in playbook

16 views
Skip to first unread message

Atul Sharma

unread,
Sep 11, 2017, 3:37:27 AM9/11/17
to Ansible Project
Please suggest what is the best practice to include the any python module in ansible playbook and run the task . Without any error right now i am getting following error while running playbook

Following workaround already have been done.


i have directory structure where all library is exist in git repo inside same library i have my python module minify_json , 

I am using  "  minify_json  " in my playbook 

ansible.cfg already created in same library folder


Please see the following directory structure where files are placed.


path for minify_json 
#################


roles/template/library/ minify_json 


and i am running playbook 



which calling same module and while running same step it is throwing below error 

The error appears to have been in '/tmp/test/workgroup/roles/cf-templates/tasks/ecs/create_ecs.yml': line 8, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


    - name: minify_json
      ^ here


playbook look like

hosts: localhost
  connection: local
  gather_facts: false
  
  tasks: 
  
    - name: minify_json 
      minify_json: 
          src: roles/cf-templates/cf-templates/ecs/ecs.json
          dest: /tmp/ecs-mini.json
      register: ecs_template



minify_json.py 


#!/usr/bin/env python

DOCUMENTATION='''
---
module: minify_json
short_description: Minify JSON file
author: Harpreet Singh
options:
    src:
        required: true
        description:
            - Location of the JSON file to minifiy
    dest:
        required: false
        description:
            - Where to save minified JSON, defaults to tmp/mini.json
'''

EXAMPLES='''
- name: minify json
  minify_json: src=pb_vpc.json
'''

import json
from ansible.module_utils.basic import *

def main():

    module = AnsibleModule(
        argument_spec = dict(
            src    = dict(required = True),
            dest   = dict(required = False)
        ),
        supports_check_mode = True
    )

    src   = module.params['src']
    dest  = module.params['dest']

    if module.check_mode:
        module.exit_json(
            path = src,
            changed = True
        )

    if dest is None:
        if not os.path.exists("tmp"):
            os.mkdir("tmp")
        dest = "tmp/mini.json"

    src_json_file = open(src, 'r')
    src_json = json.load(src_json_file)

    dest_json_file = open(dest, 'w')
    json.dump(src_json, dest_json_file)

    module.exit_json(
        path = dest,
        changed = True
    )

main()







Reply all
Reply to author
Forward
0 new messages