Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

How to use escape characters in Ansible shell/grep command

I need to extract a hostname from a URL in a file on my remote host to then use that value in another task.

I have the following task in my playbook:

- name: Extract server name
  shell: "grep ^serverURL=https://(.*)/XMLRPC <filename>"
  register: <variable>

But when I try and run it I get the following error (cut down to relevant bits):

Found unknown escape character. The offending line appears to be:

- name: Extract server name
  shell: "grep ^serverURL=https://(.*)/XMLRPC <filename>"
                                ^ here

It seems there is a value started with a quote and the YAML parser is expecting to see the line ended with the same kind of quote.

Obviously it does start and finish with double quotes but the carat is at the escape character in my grep query, so it seems reasonable to think that this is the cause of the problem; if I remove the backslashes my grep query doesn't return anything.

How can I escape this correctly please?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...