stecog
stecog

Reputation: 2344

Python - error during formatting string in subprocess call

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

Upvotes: 0

Views: 204

Answers (1)

jmd_dk
jmd_dk

Reputation: 13090

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.

Upvotes: 1

Related Questions