python系列之:本地执行bash命令,执行ansible命令
- os.popen(command):command是要执行的bash命令
- os.popen(command).read().strip():通过read输出执行命令的内容
import os
use = "f"
sparkAgent = "spakr-prod"
command = f"""
ansible-playbook
--inventory=inventories/{sparkAgent}
--user=f
--private-key=/Users/{user}/{user}.private_key
--ssh-common-args='-o ProxyCommand="/usr/local/bin/ncat --proxy optics-bj.nioint.com:8080 --proxy-type http %h %p"' update_spark_conf.yml
"""
commandOutput = os.popen(command).read().strip()
successList = []
failedList = []
if commandOutput.find("failed=0") !=-1 and commandOutput.find("unreachable=0") !=-1:
successList.append(flumeAgent)
else:
failedList.append(flumeAgent)