我想检查我的文本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的转义符。
您能否建议一个可行的语法。
根据用户@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