【问题标题】:Search for a string in a file using Ansible regex使用 Ansible 正则表达式在文件中搜索字符串
【发布时间】:2020-01-14 08:38:29
【问题描述】:

我有一个文件,其中包含多个条目之一,如下面的 grep 命令所示:

grep SSLFile /tmp/httpd.conf
Output:
        SSLFile  /web/certs/mycert7.crt

我希望使用 Ansible 正则表达式从输出中获取文件名第二列,即“/web/certs/mycert7.crt”,所有以 SSLFile 开头的条目都使用 ansible。

以下是任何 ansible 剧本:

- name: Find file
  lineinfile:
    path: "/tmp/httpd.conf"
    regexp: '^SSLFile .*'
    state: absent
  check_mode: yes
  changed_when: false
  register: getfiles

- debug:
    msg: "{{ item.split()[1] }}"
  with_items:
    - "{{ getfiles.stdout_lines }}"

不幸的是,我没有得到 greped 字符串,并且出现如下运行时错误:

TASK [Find file] ***************************************
task path: /app/test.yml:895
ok: [10.9.9.34] => {"backup": "", "changed": false, "found": 0, "msg": ""}

TASK [debug] *******************************************************************
task path: 
/app/test.yml:905
fatal: [10.9.9.34]: FAILED! => {"msg": "template error while templating string: unexpected char u'_' at 7. String: {{ getfiles.stdout_lines }}"}

这里是httpd.conf

<VirtualHost *:443>
        <LimitExcept GET POST>
        order deny,allow
        deny from all
        </LimitExcept>
        </Location>
        SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:RSA+AESGCM:RSA+AES:!SHA:!MD5:!RC4:!3DES
        SSLHonorCipherOrder On
        SSLFile  /tmp/certs/mycert.crt
        SSLKeyFile  /tmp/certs/mycert.key

我尝试了正则表达式 ^SSLFile,它甚至不匹配 https://regex101.com/ 上的字符串

更新: 尝试 regexp: '\\sSSLFile.*' 得到匹配但由于以下错误而无法打印。

task path: /tmp/test.yml:914
ok: [10.9.9.34] => {"backup": "", "changed": false, "found": 1, "msg": "1 line(s) removed"}

TASK [debug] *******************************************************************
task path: /tmp/test.yml:924
fatal: [10.9.9.34]: FAILED! => {"msg": "template error while templating string: unexpected char u'_' at 7. String: {{ getfiles.stdout_lines }}"}

您能否建议我的剧本有什么问题以及如何让它发挥作用?

【问题讨论】:

  • 似乎下划线在stdout_lines中抛出了错误我不知道ansible但可能你必须删除它。
  • @Toto 这不是问题/答案。
  • 你确定它是一个“真正的”下划线 (ASCII 5F) 并且不是一个相似的字符但在 ASCII 范围之外?
  • @Toto 标准输出也给出了类似的错误(没有下划线)。在剧本中的其他地方也使用了 stdout_lines 并且工作正常。请考虑下划线不是问题。
  • OK,但是消息指向下划线unexpected char u'_' at 7

标签: regex grep ansible


【解决方案1】:

你能试试下面的方法吗?

- hosts: localhost
  vars:
    input : "{{ lookup('template', '/tmp/httpd.conf') }}"
    target: "{{ input | regex_replace('\\sSSLFile\\s*(.*)', '\\1')}}"

  tasks:
  - debug:
     msg: "{{target }}"

或者你可以用下面的shell来做

  - name: test
    shell: cat /tmp/httpd.conf | grep -v '^#'| grep SSLFile | awk '{print $2}'
    register: op
  - debug:
     msg: "{{op.stdout_lines}}"


【讨论】:

  • 我尝试了 set_fact 而不是 vars,但是我在运行时无法在预期的路径中找到 /tmp/httpd.conf 错误。请注意,该文件位于远程主机上。
  • 模块执行期间出现意外故障。标准输出
  • 我已经更新了原始帖子并添加了信息,请检查。正则表达式甚至不匹配 regex101.com
  • 但是它也会匹配不以 SSLFile 开头的行,例如它将匹配我不想要的### SSLFile。我希望 SSLFile 成为一行的起始文本
  • 试试 \sSSLFile \s* (.*)
猜你喜欢
  • 1970-01-01
  • 2019-10-17
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 1970-01-01
  • 2017-06-15
  • 2023-01-13
  • 1970-01-01
相关资源
最近更新 更多