我创建了一个.YML Playbook。
将一些代码放入其中,工作正常。
然后我遇到了一个问题 - 我正在尝试自动部署某个应用程序。应用程序提示用户交互。不幸的是,“是| 命令”不起作用。它只是被忽略,仍然提示。
所以,我决定使用期望模块。
当前代码如下所示:
- 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"'
我已经尝试使用反斜杠()来为单引号执行字符转义,并双引用单引号。他们都没有工作。 根据我所放置的报价顺序,我要么这个看起来很容易修复。或直接坏狼。
答案 0 :(得分:0)
问题不是引号,而是字符串中的特殊字符。 期望模块执行正则表达式匹配,因此响应字符串必须符合正则表达式
这意味着必须转义括号和逗号的特殊字符
工作示例如下:
/tmp/run.sh:
#!/bin/bash
echo "Press 'y' to confirm ('s' to skip, 'a' to abort):"
read response
echo $response
ansible playbook:
- 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