0

I am trying pass the arguments to python code which will be called from terraform command . I am looking for the way to solve in terraform code , by passing args which have space

    resource "null_resource" "CWS" {
        provisioner "local-exec" {
        command =  "python ..\\ruc.py  ${var.com_port} '${var.cmd1}"
      }
        triggers = {
       always_run = "${timestamp()}"
     }
    }

In my tfvars

com_port="COM12"
cmd1= "n.set_d_cfg w0 p2.5 1678"

My python code have main function as below

def main():
    # Parse arguments from command line
    parser = argparse.ArgumentParser()

    # Set up required arguments this script
    parser.add_argument('com_port', type=str, help='comport')
    parser.add_argument('uc', type=str, help='cmd')
    
    # Parse the given arguments
    args = parser.parse_args()

    print(args.com_port)
    print(args.uc)


if __name__ == '__main__':
    main()

Getting below error

null_resource.CWS (local-exec): Executing: ["cmd" "/C" "python ..\\ruc.py COM12 n.set_d_cfg w0 p2.5 1678"]
null_resource.CWS (local-exec): usage: ruc.py [-h] com_port uc
null_resource.CWS (local-exec): ruc.py: error: unrecognized arguments: w0 p2.5 1678
╷
│ Error: local-exec provisioner error
│
│   with null_resource.CWS,
│   on SPRW.tf line 107, in resource "null_resource" "CWS":
│  107:   provisioner "local-exec" {
│
│ Error running command 'python ..\\ruc.py COM12 n.set_d_cfg w0 p2.5 1678': exit status 2. Output: usage: ruc.py [-h] com_port uc
│ ruc.py: error: unrecognized arguments: w0 p2.5 1678
Sreevathsabr
  • 649
  • 7
  • 23
  • I don't think this is a terraform issue, rather that Python parses only the first argument and does not look at the other arguments. You can see this by looking at the unrecognized arguments part: `w0 p2.5 1678`. This means that the first one was read, i.e., `n.set_d_cfg`. So you would need to add three more arguments to Python. At least those are my two cents. – Marko E Jan 25 '22 at 08:05
  • I Understang , I Can use Kargs** to solve in python side. But I am looking for the way to solve in terraform – Sreevathsabr Jan 25 '22 at 08:31

1 Answers1

0

You are using named arguments. So it should be:

command =  "python ..\\ruc.py  ${var.com_port} uc='${var.cmd1}'"
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • sorry this have tried . But it didn't work , almost same error . Only change is `'n.set_d_cfg w0 p2.5 1678'` in quotes – Sreevathsabr Jan 25 '22 at 08:29
  • 1
    @Sreevathsabr Lets go back to the beginning. Please provide your real `null_resource`. The code you provided is not even valid TF code. – Marcin Jan 25 '22 at 09:23
  • My bad , I missed an line . I have added it now . It's actually `local-exec` – Sreevathsabr Jan 25 '22 at 09:26