1

My Need is to execute a python script in my azure pipeline with additional command line arguments.

I'm able to run the script inline successfully with this code & the output I get is as expected:

1- for print ('Hello ', args.svc) as ('Hello, 'Service_De')

2- for print ('Hello ', args.env) as ('Hello, 'Dev')

- task: PythonScript@0
        inputs:
          scriptSource: inline
          script: |
            import argparse
            parser = argparse.ArgumentParser()
            parser.add_argument("--svc")
            parser.add_argument("--env")
            args = parser.parse_args()
            print ('Hello ', args.svc)
            print ('Hello ', args.env)
          arguments: --svc ${{parameters.service}}_${{parameters.environment}}
                     --env ${{parameters.environment}}

I have 2 issues that I need help with:

Issue #1: When I separate my service & environment arguments and update my python task as a combination of both arguments, the script gives me an error print ('Hello ', args.svc_args.env)

I am not able to format the python code properly. I ideally want the output as ('Helo','Service_Dev')

- task: PythonScript@0
        inputs:
          scriptSource: inline
          script: |
            import argparse
            parser = argparse.ArgumentParser()
            parser.add_argument("--svc")
            parser.add_argument("--env")
            args = parser.parse_args()
            print ('Hello ', args.svc_args.env)
          arguments: --svc ${{parameters.service}}
                     --env ${{parameters.environment}}

Issue #2:: When I execute the same working code using the filepath option, it fails to execute and give me an error:

IOError: [Errno 2] No such file or directory: './group_vars/args.svc.yaml'

- task: PythonScript@0
        inputs:
          scriptSource: 'filePath'
          scriptPath: "$(System.DefaultWorkingDirectory)/modify-config.py"
          arguments: --svc ${{parameters.service}}_${{parameters.environment}}
          #pythonInterpreter: # Optional
          #workingDirectory: # Optional
          #failOnStderr: false # Optional
        displayName: 'Update Config files'

The modify-config.py has the following code:

#!/usr/bin/python
import os
import yaml
from jinja2 import Template
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--svc")
#parser.add_argument("--env")
args = parser.parse_args()

with open(r'./group_vars/args.svc.yaml') as file:
  #value_list = yaml.load(file, Loader=yaml.FullLoader)
  value_list = yaml.full_load(file)
Arvind
  • 79
  • 1
  • 7

1 Answers1

0

You need to do something like this to get real directory path See: Get parent of current directory from Python script

import os
import yaml
from jinja2 import Template
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--svc")
#parser.add_argument("--env")
args = parser.parse_args()

base_dir = Path(__file__).resolve().parent
file_path = os.path.join(base_dir, "group_vars/args.svc.yaml")
with open(file_path) as file:
  #value_list = yaml.load(file, Loader=yaml.FullLoader)
  value_list = yaml.full_load(file)
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 11 '22 at 18:23