debugging - 如何检查注册变量在ansible中是否具有特定模式?

标签 debugging ansible conditional-statements msg

所以我想检查nginx语法是否正确

- name: check if syntax is ok on nginx
  shell: nginx -t
  register: result

然后我想将其与带有“when:”条件的服务模块一起使用,以运行服务模块来重新加载nginx“when:”“结果”中有“语法正确”。

这样的东西有用吗?

when: result.stdout_lines == "syntax is ok"

最佳答案

你可以使用这样的东西,

when: result.stdout | search("syntax is ok")

它将在 result.stdout 字符串中搜索字符串“syntax is ok”,如果找到,则条件将成功并运行所需的任务(如果找到)如果失败,则任务将被跳过。

我添加了更多方法来达到相同的结果,

when: result.stdout is search("syntax is ok")

使用正则表达式搜索

when: result.stdout | regex_search("syntax is ok")

也可以使用@TinaC建议的。

ansible版本2.8.0上测试

测试 ansible 剧本

---
- hosts: all
  gather_facts: no
  vars:
    - error_msg: "nginx: [alert] could not open error log file: open() \"/var/log/nginx/error.log\" failed (13: Permission denied) 2019/09/12 16:19:28 [warn] 5343#0: the \"user\" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:5 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 2019/09/12 16:19:28 [emerg] 5343#0: open() \"/run/nginx.pid\" failed (13: Permission denied) nginx: configuration file /etc/nginx/nginx.conf test failed"
  tasks:
    - debug:
        msg: "{{ error_msg }}"

    - debug:
        msg: "{{ error_msg }}"
      when: error_msg | search("syntax is ok")

    - debug:
        msg: "{{ error_msg }}"
      when: error_msg is search("syntax is ok")

    - debug:
        msg: "{{ error_msg }}"
      when: error_msg | regex_search("syntax is ok")

输出

PLAY [all] ***********************************************************************************************************************************************

TASK [debug] *********************************************************************************************************************************************
ok: [192.168.100.101] => {
    "msg": "nginx: [alert] could not open error log file: open() \"/var/log/nginx/error.log\" failed (13: Permission denied) 2019/09/12 16:19:28 [warn] 5343#0: the \"user\" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:5 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 2019/09/12 16:19:28 [emerg] 5343#0: open() \"/run/nginx.pid\" failed (13: Permission denied) nginx: configuration file /etc/nginx/nginx.conf test failed"
}

TASK [debug] *********************************************************************************************************************************************
ok: [192.168.100.101] => {
    "msg": "nginx: [alert] could not open error log file: open() \"/var/log/nginx/error.log\" failed (13: Permission denied) 2019/09/12 16:19:28 [warn] 5343#0: the \"user\" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:5 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 2019/09/12 16:19:28 [emerg] 5343#0: open() \"/run/nginx.pid\" failed (13: Permission denied) nginx: configuration file /etc/nginx/nginx.conf test failed"
}

TASK [debug] *********************************************************************************************************************************************
ok: [192.168.100.101] => {
    "msg": "nginx: [alert] could not open error log file: open() \"/var/log/nginx/error.log\" failed (13: Permission denied) 2019/09/12 16:19:28 [warn] 5343#0: the \"user\" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:5 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 2019/09/12 16:19:28 [emerg] 5343#0: open() \"/run/nginx.pid\" failed (13: Permission denied) nginx: configuration file /etc/nginx/nginx.conf test failed"
}

TASK [debug] *********************************************************************************************************************************************
ok: [192.168.100.101] => {
    "msg": "nginx: [alert] could not open error log file: open() \"/var/log/nginx/error.log\" failed (13: Permission denied) 2019/09/12 16:19:28 [warn] 5343#0: the \"user\" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:5 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 2019/09/12 16:19:28 [emerg] 5343#0: open() \"/run/nginx.pid\" failed (13: Permission denied) nginx: configuration file /etc/nginx/nginx.conf test failed"
}

PLAY RECAP ***********************************************************************************************************************************************
192.168.100.101            : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

当将字符串 syntax is ok 更改为 syntax is not ok 时,以下是输出,

PLAY [all] ***********************************************************************************************************************************************

TASK [debug] *********************************************************************************************************************************************
ok: [192.168.100.101] => {
    "msg": "nginx: [alert] could not open error log file: open() \"/var/log/nginx/error.log\" failed (13: Permission denied) 2019/09/12 16:19:28 [warn] 5343#0: the \"user\" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:5 nginx: the configuration file /etc/nginx/nginx.conf syntax is not ok 2019/09/12 16:19:28 [emerg] 5343#0: open() \"/run/nginx.pid\" failed (13: Permission denied) nginx: configuration file /etc/nginx/nginx.conf test failed"
}

TASK [debug] *********************************************************************************************************************************************
skipping: [192.168.100.101]

TASK [debug] *********************************************************************************************************************************************
skipping: [192.168.100.101]

TASK [debug] *********************************************************************************************************************************************
skipping: [192.168.100.101]

PLAY RECAP ***********************************************************************************************************************************************
192.168.100.101            : ok=1    changed=0    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0

关于debugging - 如何检查注册变量在ansible中是否具有特定模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57805684/

相关文章:

debugging - 从 emacs 运行时,ocaml 调试器找不到核心

javascript - 调试 JS 文件 - 客户端代码

debugging - 使用 ARM 半主机

python - 在 Windows 上的本地主机上运行 Ansible-Playbook

javascript - 为什么要在删除之前检查元素/属性?

mysql - 有条件地在where中添加条件

android - 在 Eclipse 中调试时,如何查看 android SDK 系统库的符号?

ansible - 使用 Jinja 过滤器从嵌套对象中获取唯一键

docker - 在 Ansible 模块中编写条件 docker 属性

Bash 条件管道