Work in Progress Json2yaml converter for Ansible (http://docs.ansible.com/ansible/ec2_group_module.html) creation
Link to Project: https://github.com/shdobxr/json2yamlAnsible/blob/master/json2yaml.py
Answered! See above for project.
I have been researching this on stack. And it's just not clicking.
Python replace multiple strings
Here is my attempt, please be kind I have dabbled with python in the past. The rust is showing here:
#!/usr/local/bin/python
import re, sys, json, yaml
with open(sys.argv[1]) as f:
#   print yaml.safe_dump(json.load(f), default_flow_style=False)
   a = yaml.safe_dump(json.load(f), default_flow_style=False)
c = sys.argv[1] + ".yml"
text_file = open(c, "w")
text_file.write(a)
text_file.close
replacements = {
    '    ToPort:': '            to_port:',
    '  - FromPort:': '            from_port:',
    'UserIdGroupPairs:': '',
    'Vpc:': '',
    'VpcPeeringConnectionId:': '',
    'UserIdGroupPairs:': '',
    'PrefixListIds: []': '',
    '- Description:': '        description:',
    '  GroupName:': '    - name:',
    '  -     IpProtocol:': '          - proto:',
    '- Description:': '        description:',
    'SecurityGroups:': '- name:',
    '  IpPermissions:': '        rules:',
    '  IpPermissionsEgress:': '        rules_egress:',
    '  GroupId:': '',
    '    - GroupId:': '            group_id:'
}
replacements = dict((re.escape(k), v) for k, v in replacements.iteritems())
#pattern = re.compile('|'.join(replacements.keys()))
remove_pattern = re.compile('|'.join(k for k, v in replacements.iteritems() if v is None))
replace_pattern = re.compile('|'.join(k for k, v in replacements.iteritems() if v is not None))
def rewrite(text):
    if remove_pattern.search(text):
        return ''
    return replace_pattern.sub(lambda m: replacements[re.escape(m.group())], text)
with open(c, 'rt') as fin:
    with open('out.txt', 'wt') as fout:
        for line in fin:
            fout.write(rewrite(line))
Test file (save this as test.json)
{
    "SecurityGroups": [
        {
            "IpPermissionsEgress": [
                {
                    "PrefixListIds": [],
                    "FromPort": 22,
                    "IpRanges": [
                        {
                            "CidrIp": "10.0.0.0/24"
                        }
                    ],
                    "ToPort": 22,
                    "IpProtocol": "tcp",
                    "UserIdGroupPairs": [
                        {
                            "UserId": "XXXXXXXXXXXX",
                            "GroupId": "sg-xxxxxxxx"
                        }
                    ]
                },
                {
                    "PrefixListIds": [],
                    "FromPort": 3389,
                    "IpRanges": [
                        {
                            "CidrIp": "10.0.0.0/24"
                        }
                    ],
                    "ToPort": 3389,
                    "IpProtocol": "tcp",
                    "UserIdGroupPairs": [
                        {
                            "UserId": "XXXXXXXXXXXX",
                            "GroupId": "sg-xxxxxxxx"
                        }
                    ]
                }
            ],
            "Description": "TEST JSON",
            "Tags": [
                {
                    "Value": "Test JSON",
                    "Key": "Name"
                }
            ],
            "IpPermissions": [
                {
                    "PrefixListIds": [],
                    "FromPort": 22,
                    "IpRanges": [
                        {
                            "CidrIp": "10.0.0.0/24"
                        }
                    ],
                    "ToPort": 22,
                    "IpProtocol": "tcp",
                    "UserIdGroupPairs": [
                        {
                            "UserId": "XXXXXXXXXXXX",
                            "GroupId": "sg-xxxxxxxx"
                        }
                    ]
                },
                {
                    "PrefixListIds": [],
                    "FromPort": 3389,
                    "IpRanges": [
                        {
                            "CidrIp": "10.0.0.0/24"
                        }
                    ],
                    "ToPort": 3389,
                    "IpProtocol": "tcp",
                    "UserIdGroupPairs": [
                        {
                            "UserId": "XXXXXXXXXXXX",
                            "GroupId": "sg-xxxxxxxx"
                        }
                    ]
                }
            ],
            "GroupName": "Test JSON",
            "VpcId": "vpc-XXXXXXXX",
            "OwnerId": "XXXXXXXXXXXX",
            "GroupId": "sg-xxxxxxxx"
        }
    ]
}
 
     
    