0% found this document useful (0 votes)
5 views

ArgPaser_JSON_complex_input

The document discusses how to use Python's argparse to capture user input and insert those values into a JSON file for use with Packer. It outlines various methods to achieve this, including using the json module to dump argument values into a JSON-compatible format without overwriting existing files. Additionally, it provides examples of code snippets and solutions that have been proposed by users on Stack Overflow to facilitate this process.

Uploaded by

Pawan kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

ArgPaser_JSON_complex_input

The document discusses how to use Python's argparse to capture user input and insert those values into a JSON file for use with Packer. It outlines various methods to achieve this, including using the json module to dump argument values into a JSON-compatible format without overwriting existing files. Additionally, it provides examples of code snippets and solutions that have been proposed by users on Stack Overflow to facilitate this process.

Uploaded by

Pawan kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Using python argparse arguments as variable values within a json file - Sta... https://stackoverflow.com/questions/53973531/using-python-argparse-ar...

Using python argparse arguments as variable values


within a json file
Asked 1 year, 8 months ago Active 1 year, 8 months ago Viewed 483 times

1 of 4 8/30/2020, 4:54 PM
Using python argparse arguments as variable values within a json file - Sta... https://stackoverflow.com/questions/53973531/using-python-argparse-ar...

I've googled this quite a bit and am unable to find helpful insight. Basically, I need to take the
user input from my argparse arguments from a python script (as shown below) and plug those
0 values into a json file (packerfile.json) located in the same working directory. I have been
experimenting with subprocess , invoke and plumbum libraries without being able to "find the
shoe that fits".

From the following code, I have removed all except for the arguments as to clean up:

#!/usr/bin/python
import os, sys, subprocess
import argparse
import json
from invoke import run
import packer

parser = argparse.ArgumentParser()
parser._positionals.title = 'Positional arguments'
parser._optionals.title = 'Optional arguments'
parser.add_argument("--access_key",
required=False,
action='store',
default=os.environ['AWS_ACCESS_KEY_ID'],
help="AWS access key id")
parser.add_argument("--secret_key",
required=False,
action='store',
default=os.environ['AWS_SECRET_ACCESS_KEY'],
help="AWS secret access key")
parser.add_argument("--region",
required=False,
action='store',
help="AWS region")
parser.add_argument("--guest_os_type",
required=True,
action='store',
help="Operating system to install on guest machine")
parser.add_argument("--ami_id",
required=False,
help="AMI ID for image base")
parser.add_argument("--instance_type",
required=False,
action='store',
help="Type of instance determines overall performance (e.g.
t2.medium)")
parser.add_argument("--ssh_key_path",
required=False,

json example code:

{
"variables": {
"aws_access_key": "{{ env `AWS_ACCESS_KEY_ID` }}",
"aws_secret_key": "{{ env `AWS_SECRET_ACCESS_KEY` }}",
"magic_reference_date": "{{ isotime \"2006-01-02\" }}",
"aws_region": "{{ env 'AWS_REGION' }}",
"aws_ami_id": "ami-036affea69a1101c9",

2 of 4 8/30/2020, 4:54 PM
Using python argparse arguments as variable values within a json file - Sta... https://stackoverflow.com/questions/53973531/using-python-argparse-ar...

After parse_args() , print(args) to see what the parser has produced. print(vars(args)) will
show the same thing as a dictionary. args,region will be the value parsed as region . – hpaulj Dec
29 '18 at 22:46

so I am aware of how to print the value of args. The full command that I am providing to the script is:
python packager.py --region us-west-2 --guest_os_type rhel7 --ssh_key_name test_key and
the printed results are {'access_key': 'REDACTED', 'secret_key': 'REDACTED', 'region': 'us-
west-2', 'guest_os_type': 'rhel7', 'ami_id': None, 'instance_type': None,
'ssh_key_path': '/Users/REDACTEDt/.ssh', 'ssh_key_name': 'test_key'} .. what i need is to
import thos values into the packerfile.json variables list.. preferably in a way that i can reuse it (so
it musn't overwrite the file) – aphexlog Dec 29 '18 at 23:13

This is really a packer issue, not an argparse one. The parser gives you access, in your Python
script, to a set of variables (or call them attributes). You could collect those values in a JSON
compatible dictionary, and use the standard json module to dump that as a string or file. But whether
that helps with packer I don't know. – hpaulj Dec 30 '18 at 0:53

What do you think about just treating packer as though I’m simply dealing with json... that is what I’m
trying to do.. now I know that packer gives me the ability to have my variables located inside of a
different json file... so I was thinking of just generating a json file with the py script into the current
working directory and using it as the bar.json file. – aphexlog Dec 30 '18 at 0:58

If you are wondering about my use case... I’m trying to make it so that I can have a single script that
will apply any value to a packer variable and ultimately all of our packer images will be powered via a
single packer json file... thus we would only ever need to version a single json file rather than dozens
of them. – aphexlog Dec 30 '18 at 2:24

2 Answers Active Oldest Votes

I was actually able to come up with a pretty simple solution.

0 args = parser.parse_args()
print(json.dumps(vars(args), indent=4))
s.call("echo '%s' > variables.json && packer build -var-file=variables.json
packerfile.json" % json_formatted, shell=True)

arguments are captured under the variable args and dumped to the output with json.dump
while vars is making sure to also dump the arguments with their key values and I currently
have to run my code with >> vars.json but I'll insert logic to have python handle that.

Note: s == subprocess in s.call

edited Jan 2 '19 at 16:28 answered Dec 31 '18 at 17:40


aphexlog
533 7 27

3 of 4 8/30/2020, 4:54 PM
Using python argparse arguments as variable values within a json file - Sta... https://stackoverflow.com/questions/53973531/using-python-argparse-ar...

You might use the __dict__ attribute from the SimpleNamespace that is returned by the
ArgumentParser . Like so:
0
import json
parsed = parser.parse_args()
with open('packerfile.json', 'w') as f:
json.dump(f, parsed.__dict__)

If required, you could use add_argument(dest='attrib_name') to customise attribute names.

answered Dec 29 '18 at 21:51


dr1fter
21 1 2

What is the function of __dict__ ? also, I edited my question to include the important bits of that json
file... hopefully to provide a bit more clarity. – aphexlog Dec 29 '18 at 22:32

vars(foo) (as you found out in your updated solution) is pretty much similar to immediately using
foo.__dict__. So I suppose your approach is cleaner, but mostly equivalent to mine :-) – dr1fter Jan 5
'19 at 11:07

4 of 4 8/30/2020, 4:54 PM

You might also like