自定义变量使用
subprocess.call 是不能作为赋值的,需要用到 subprocess.check_output 函数,而且如果要引用赋值就必须使用subprocess.call(['echo',line])这种形式。 In [42]: import subprocess In [101]: subprocess.call(['kubectl','get','nodes']) NAME STATUS ROLES AGE VERSION test2 Ready node 2d v1.11.0 In [42]: output=subprocess.check_output(["kubectl get nodes | grep test2 | awk '{print $1}'"], shell=True) In [43]: name=output.decode('utf8').strip() In [44]: print(line) test2 In [83]: test2 In [102]: subprocess.call(['kubectl','label','nodes',name,'node-role.kubernetes.io/node='])