sed语法不适用于Ansible shell模块

问题描述 投票:-1回答:1

我想检查我的文本Options在打开和关闭LocationMatch标签之间是否存在。下面的sed命令给了我想要的结果。

$ sed -n '/^<LocationMatch "^\/+$">/,/^<\/LocationMatch/p' httpd.conf |  grep -i 'Options '
 Options -Indexes

但是,当从ansible执行同一命令时,出现语法错误。

- name: Check if Options exists between Location Match tags
  shell: "sed -n '/^<LocationMatch \"^\/+$\">/,/^<\/LocationMatch/p' {{ httpd_home }}/conf/httpd.conf |  grep -i 'Options '"
  register: Optionsexist

输出:

      shell: "sed -n '/^<LocationMatch \"^\/+$\">/,/^<\/LocationMatch/p' {{ httpd_home }}/conf/httpd.conf |  grep -i 'Options '"
                                          ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

我确实在sed命令中的双引号之前保留了ansible的转义符。

您能否建议一个可行的语法。

sed ansible syntax-error full-text-search ansible-2.x
1个回答
0
投票

根据用户@Zeitounators的建议,以下带转义符的格式化代码有助于克服语法错误。

- name: Check if Options exists between Location Match tags
  shell: "sed -n '/^<LocationMatch \"^\\/+$\\">/,/^<\/LocationMatch/p' {{ httpd_home }}/conf/httpd.conf |  grep -i 'Options '"
  register: Optionsexist
© www.soinside.com 2019 - 2024. All rights reserved.