我已经创建了一本.YML行动手册。
在里面放一些代码,效果很好。
然后,我遇到了一个问题--我正在尝试自动化某个应用程序的部署。应用程序提示用户进行交互。不幸的是,"yes | command“不起作用。它只是被忽略,并仍然提示。
因此,我决定使用Expect模块。
当前代码如下所示:
- 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
我已经将预期的行括在双引号中。但是,由于预期的行有单引号('),它似乎打破了语法。
错误输出如下:
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"'
我已经尝试过使用反斜杠()对单引号和双引号进行字符转义。它们都不起作用。根据我引用的顺序,我要么让这个看起来很容易修复。或者直接去找坏狼。
发布于 2018-05-28 19:51:40
问题不在于引号,而在于字符串中的特殊字符。Expect模块执行正则表达式匹配,因此响应字符串必须符合正则表达式
这意味着必须转义特殊字符,如圆括号和逗号
工作示例如下所示:
#!/bin/bash
echo "Press 'y' to confirm ('s' to skip, 'a' to abort):"
read response
echo $response
- 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
https://stackoverflow.com/questions/50565339
复制相似问题