我正在使用python3和ansible 2.9.2, 我使用python脚本运行剧本:
subprocess.Popen(["ansible-playbook", "create.yml"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
我以字符串的形式获取输出:
"(b'\nPLAY [localhost] **\n\nTASK [Creating abc] ***\nok: [localhost]\n\nTASK [debug] ***\nok: [localhost] => {\n "msg": "10.0.0.1"\n}\n\nPLAY RECAP ******\nlocalhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 \n\n', b"[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'\n[WARNING]: Found variable using reserved name: name\n")"
如何在python中只过滤“msg”输出
我想将ip地址10.0.0.1
作为变量,但找不到合适的正则表达式来捕获它
谢谢
由于ansible输出(playbook的结果)与2.9.2中的语法没有变化,因此我将使用类似的方法来捕获作为变量的ip:
输出:
然后做这个小把戏,将所有IP地址捕获为列表:(如果您要在多个目的地上运行playbook,可能供将来使用)
输出:
希望你会觉得这很有用
因为您已经从python脚本启动了一个剧本,所以我推荐Ansible Runner
相关问题 更多 >
编程相关推荐