0

I wish to check if my text Options exists between opening and closing LocationMatch tags. Below sed command gives me the desired result.

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

However, I'm getting syntax error when executing the same command from 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

Output:

      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 }}"

I did keep ansible's escape charecter before the double quotes in the sed command.

Can you please suggest a working syntax.

Ashar
  • 2,942
  • 10
  • 58
  • 122
  • If you need a backslash in your final string, then you need to escape it in your yaml double quote surrounded scalar (i.e. double backslash) – Zeitounator Feb 11 '20 at 07:31

1 Answers1

0

With User @Zeitounators suggestion the below formatted code with escape charecters helped overcome the syntax error.

- 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
Ashar
  • 2,942
  • 10
  • 58
  • 122