Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
subprocess
#1
I want to execute below ansible playbook using python, can some one help me with subprocess for a variable? Basically am trying to achieve executing the ansible command with python like below with a variable.
Command: ""/bin/ansible-playbook PlayBook.yml --extra-vars appName={var1}""

I have value of the variable as myFirstApp, trying to put that on the command.
var=myFirstApp

below doesn't works, "var" is not getting resolved.

Quote:subprocess.check_call(["/bin/ansible-playbook", "tomcatPlayBook.yml", "--extra-vars", "appName={var}"])

Any help is appreciated

Thanks
Reply
#2
To have {var} resolved, you must make an f-string of it.
f"appName={var}"
Or else use the format() method.
Reply
#3
thanks for checking! am getting invalid syntax, in fact i have tried this before as well i think. Below is what i tried.

Quote:subprocess.check_call(["/bin/ansible-playbook", "PlayBook.yml", "--extra-vars", f"appName={var}"])

Thanks
Reply
#4
Hi All,

I think its because of version, f string was introduced on 3.x. I was using 2.7 was able to achieve this through format method.

Thanks
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020