错误! Ansible中有冲突的动作语句(expect,command)

时间:2016-04-19 08:14:44

标签: ansible expect ansible-playbook ansible-2.x

我正在尝试使用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)

谢谢!

1 个答案:

答案 0 :(得分:3)

注意 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命令,您可以添加-y参数:

apt-get install -y openjdk-7-jdk

甚至更好,请使用apt Ansible module