我正在运行一个 ansible 任务,并想查看运行的任务返回的列表中是否存在子字符串。
该任务使用 win_shell
模块并执行以下命令:netstat -aon | sls 8080"
如果进程正在运行,结果将如下所示:
stdout_lines: [
"",
"",
" TCP 127.0.0.1:8080 0.0.0.0:0 LISTENING 9196",
""
]
在 python 中,我会执行类似的操作来检查列表中的元素中是否存在子字符串。
匹配 = [s for s in some_list if "8080"in s]
有没有办法在ansible中实现这一点?
最佳答案
我看到两个选项。
首先,你可以这样做:
- name: Capture output for analysis
shell: "netstat -aon | sls 8080"
register: netstat_output
- name: Check if the substring is in the output
command: <do something else>
when: "'some word' in {{netstat_output.stdout}}"
另一种选择是突破 Python 脚本并利用该脚本(如果上述方法仍然不能满足您的需求)。
- name: Run verification in Python
script: some-script.py
希望这有帮助!
关于python - 如何检查 ansible 任务返回的数组中是否存在字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53103901/