简体   繁体   English

预期模块中字符串中的字符转义单引号

[英]Character escape single quote in a string in Expect Module

i have created a .YML Playbook. 我创建了一个.YML剧本。

Put some code in it, works fine. 在其中添加一些代码,效果很好。

Then i am hit with an issue - i am trying to automate the deployment of a certain application. 然后我遇到了一个问题-我正在尝试自动化某个应用程序的部署。 The application prompts for user interaction. 该应用程序提示用户交互。 Unfortunatelly, "yes | command " does not work. 不幸的是,“是| 命令 ”无效。 It simply gets ignored, and still prompts. 它只是被忽略,仍然提示。

So, i decided to use the Expect Module. 因此,我决定使用Expect模块。

Current code looks like this: 当前代码如下:

- hosts: all
  remote_user: someuser
  gather_facts: yes

  tasks:
    - name: "Copy files"
      copy: src=../somefiles/ dest=/tmp

    - name: "Execute some script"
      shell: sudo /tmp/script.sh

    - name: "Execute app"
      expect:
        command: /bin/bash -c 'sudo /tmp/app arguments'
        responses:
        "'Press 'y' to confirm ('s' to skip, 'a' to abort):'": "y"
      echo: y

I have encassed the Expected line, in double quotes. 我用双引号将“期望”行括起来。 But, since the Expected Line, has Single quotes ('), it seems to be breaking the syntax. 但是,由于Expected Line具有单引号('),因此似乎在破坏语法。

The error output is as follow: 错误输出如下:

ERROR! Syntax Error while loading YAML.


The error appears to have been in 'deploy.yml': line 16, column 1, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

        responses:
        "'Press 'y' to confirm ('s' to skip, 'a' to abort):'": "y"
^ here
This one looks easy to fix.  It seems that 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.  For instance:

    when: "ok" in result.stdout

Could be written as:

   when: '"ok" in result.stdout'

Or equivalently:

   when: "'ok' in result.stdout"
We could be wrong, but this one looks like it might be an issue with
unbalanced quotes.  If starting a value with a quote, make sure the
line ends with the same set of quotes.  For instance this arbitrary
example:

    foo: "bad" "wolf"

Could be written as:

    foo: '"bad" "wolf"'

I have tried both with backslash () to do character escaping for the single quotes, and double quoting the single quotes. 我已经尝试过使用反斜杠()对单引号进行字符转义,并且对双引号进行双引号。 None of them worked. 他们都没有工作。 Depending on the order of the quotes i place, i either get This one looks easy to fix. 根据我放置的报价顺序,我要么得到,要么看起来很容易解决。 or straight to bad wolf . 或直接变成坏狼

Problem are not quotes but special characters in the string. 问题不是引号,而是字符串中的特殊字符。 Expect module performs a regex matching, so the response string must be regex-compliant Expect模块执行正则表达式匹配,因此响应字符串必须符合正则表达式

That means special characters as parenthesis and commas must be escaped 这意味着必须转义特殊字符(如括号和逗号)

The working example is like that: 工作示例是这样的:

/tmp/run.sh: /tmp/run.sh:

#!/bin/bash
echo "Press 'y' to confirm ('s' to skip, 'a' to abort):"
read response
echo $response

ansible playbook: ansible剧本:

- connection: local
  hosts: localhost
  tasks:
    - name: "Execute app"
      expect:
        command: /tmp/run.sh
        responses:
          Press 'y' to confirm \('s' to skip\, 'a' to abort\):: "y"
        echo: yes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM