错误! Ansible 中冲突的动作语句(期望,命令)

ERROR! conflicting action statements (expect, command) in Ansible

我正在尝试使用 Ansible 在多台主机上安装 java。 我寻找了 expect 模块的一些示例来提供提示的答案。 我认为这个语法很好:

- hosts: datanode
  sudo: yes
  sudo_user: root
  tasks:
  - expect:
    name: install java jdk 7
    command: apt-get install openjdk-7-jdk
    responses:
    Question:
      'Do you want to continue? [Y/n]': 'Y'

但是当我尝试执行 ansible-playbook file.yml 时,我收到错误消息:

ERROR! conflicting action statements (expect, command)

The error appears to have been in '/root/scp.yml': line 5, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
  - expect:
    ^ here

问题出在哪里? (我已经安装了ansible 2.0.1.0, pexpect, python)

谢谢!

注意 Ansible 使用 yaml 文件,这种文件是 缩进的。这意味着您在每个语句之前放置的空格对于让 Ansible 理解它们是如何嵌套的很重要。 More info about yaml.

更正的任务:

- hosts: datanode
  sudo: yes
  sudo_user: root
  tasks:
  - name: install java jdk 7
    expect:
      command: apt-get install openjdk-7-jdk
      responses:
        Question:
          - 'Y'
          - 'n'

这将避免您的语法错误。

来源:http://docs.ansible.com/ansible/expect_module.html

或者,如果您总是想对您的 apt-get install 命令说 "yes",您可以添加 -y 参数:

apt-get install -y openjdk-7-jdk

或者更好,使用 apt Ansible module