如何在 Ansible 中使用正则表达式? [英] How do I use regex with Ansible?

查看:52
本文介绍了如何在 Ansible 中使用正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Ansible 2 中无法匹配正则表达式模式.有人可以帮助我理解我做错了什么吗?谢谢.

I am having trouble getting a regex pattern to match in Ansible 2. Could someone help me understand what I am doing wrong? Thank you.

--- # Disable auto update for Ubuntu
- hosts: nonedgeLinux
  become: yes
  tasks:
    - name: disable auto updates
      replace:
        dest: /etc/apt/apt.conf.d/50unattended-upgrades
        regexp: '(?:[ \t]*\"\${distro_id}:\${distro_codename}-security\";)'
        replace: '//  "\${distro_id}:\${distro_codename}-security\";'

我使用 https://regex101.com/ 根据文件的副本/粘贴来验证正则表达式正在搜索的内容.此测试报告正则表达式模式是正确的.每次在 Ubuntu 16.04.2 机器上运行时,我都会得到以下结果:

I used https://regex101.com/ to validate the regex against a copy/paste of the file contents being searched. This test reports the regexp pattern is correct. Each time it is run on a Ubuntu 16.04.2 box I get the following results:

root@sbx54:/data/scripts/ansible# ansible-playbook disableAutoUpdate.yml -vvvv
Using /data/scripts/ansible/ansible.cfg as config file
Loaded callback default of type stdout, v2.0
1 plays in disableAutoUpdate.yml

PLAY ****************************************************************************

TASK [setup] *******************************************************************
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1492480514.73-9504514182168 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1492480514.73-9504514182168 )" )
localhost PUT /tmp/tmpkYPc6g TO /root/.ansible/tmp/ansible-tmp-1492480514.73-9504514182168/setup
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1492480514.73-9504514182168/setup; rm -rf "/root/.ansible/tmp/ansible-tmp-1492480514.73-9504514182168/" > /dev/null 2>&1
ok: [localhost]

TASK [disable auto updates] ****************************************************
task path: /data/scripts/ansible/disableAutoUpdate.yml:15
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1492480515.71-278594852314124 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1492480515.71-278594852314124 )" )
localhost PUT /tmp/tmpG3gchf TO /root/.ansible/tmp/ansible-tmp-1492480515.71-278594852314124/replace
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1492480515.71-278594852314124/replace; rm -rf "/root/.ansible/tmp/ansible-tmp-1492480515.71-278594852314124/" > /dev/null 2>&1
ok: [localhost] => {"changed": false, "invocation": {"module_args": {"backup": false, "content": null, "delimiter": null, "dest": "/etc/apt/apt.conf.d/50unattended-upgrades", "directory_mode": null, "follow": false, "force": null, "group": null, "mode": null, "owner": null, "regexp": "(?:(?!\\/\\/).*[ \\t]*\\\"\\${distro_id}:\\${distro_codename}-security\\\";)/g", "remote_src": null, "replace": "replaced", "selevel": null, "serole": null, "setype": null, "seuser": null, "src": null, "validate": null}, "module_name": "replace"}, "msg": ""}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0

这是正在搜索的文件的片段:

Here is a snip of the file being searched:

Unattended-Upgrade::Allowed-Origins {
    "${distro_id}:${distro_codename}";
    "${distro_id}:${distro_codename}-system";
//  "${distro_id}:${distro_codename}-updates";
//  "${distro_id}:${distro_codename}-proposed";
//  "${distro_id}:${distro_codename}-backports";
};

# ansible --version
ansible 2.0.0.2
config file = /data/scripts/ansible/ansible.cfg
configured module search path = Default w/o overrides

最后一点:当我只查找字符串时,我确实在此文件中成功搜索和替换.寻找整行时就是失败的时候.此外,我也尝试过转义特殊字符,例如[/][/] 代替 \/\/\ 等,没有运气.

One last note: I did have success in search and replace within this file when only looking for strings. When looking for the entire line is when this fails. Additionally I have tried escaping the special characters as well, e.g. [/][/] in place of \/\/\, etc. with no luck.

推荐答案

除了尝试将 securitysystem 匹配的错误之外,您不需要对任一参数中的双引号或 replace 参数中的美元符号进行转义(否则反斜杠将被插入到文件中):

On top of the mistake in trying to match security with system, you don't need to escape the double quotes in either of the arguments or the dollar sign in the replace argument (otherwise the backslash will be inserted into the file):

- name: disable auto updates
  replace:
    dest: /etc/apt/apt.conf.d/50unattended-upgrades
    regexp: '(?:[ \t]*"\${distro_id}:\${distro_codename}-system";)'
    replace: '//  "${distro_id}:${distro_codename}-system";'

并且您可以使用 lineinfile 模块获得相同的结果(这使代码更具可读性,恕我直言):

And you can achieve the same result using lineinfile module (which makes code a bit more readable, imho):

- name: disable auto updates
  lineinfile:
    dest: /etc/apt/apt.conf.d/50unattended-upgrades
    regexp: '"\${distro_id}:\${distro_codename}-system"'
    line: '//  "${distro_id}:${distro_codename}-system";'

这篇关于如何在 Ansible 中使用正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆