0

my script in python 3.8 must run an external command and use some variables, and is:

print('\nProvisiong VM with ansible')
print(url)
print(vm_id)
output = subprocess.run(["ansible-playbook", "-i", "%s:22%s,", "-e", "ansible_user=ubuntu", "playbook.yml" % (url, vm_id)]) 
print(output)

I've insert the print(url) and print(vm_id) to see if they exist, the result is:

Provisiong VM with ansible
11.22.33.44
113
Traceback (most recent call last):
  File "./launch_vm.py", line 136, in <module>
    output = subprocess.run(["ansible-playbook", "-i", "%s:22%s,", "-e", "ansible_user=ubuntu", "playbook.yml" % (url, vm_id)]) 
TypeError: not all arguments converted during string formatting

The syntax seems correct to me ... maybe the problem is with the subprocess call?

Thanks

1 Answer 1

1

You should apply the formatting to the correct string, i.e. "%s:22%s,":

output = subprocess.run(["ansible-playbook", "-i", "%s:22%s," % (url, vm_id), "-e", "ansible_user=ubuntu", "playbook.yml"])

If you really want to do a common formatting of all the strings, you need to write them as one. Thus this does the same thing:

output = subprocess.run(("ansible-playbook -i %s:22%s, -e ansible_user=ubuntu playbook.yml" % (url, vm_id)).split())

Also note: I'm not sure what you are trying to do, but the additional comma after %s:22%s looks a bit suspicious.

Sign up to request clarification or add additional context in comments.

2 Comments

Maybe add an example of good use of f-strings? It seems a nice addendum and does the job gracefully
The comma after %s:22%s is required, thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.