How can I answer answer some question with 'y' in ansible with expect?

时间:2018-11-27 21:41:03

标签: ansible

I'm trying to destroy docker containers and images and I need to confirm destruction by entering 'y'. I've tried a few tweaks based on various questions here but I still haven't managed to get it to work. Can someone please advise as to how I can answer the question 'This will delete all containers and data, are you sure? (y/n)' with 'y'?

Here is my current attempt.

- name: Destroy containers and images
  expect:
    command: kolla host destroy all --includedata --removeimages
    responses:
      This will delete all containers and data, are you sure? (y/n):
        - y

The following version shows ok but doesn't actually do it.

- name: Destroy containers and images
shell: |
  spawn kolla host destroy all --includedata --removeimages
  expect "This will delete all containers and data, are you sure? (y/n)"
  send "y"
args:
  executable: /usr/bin/expect

Obviously all works fine from shell.

3 个答案:

答案 0 :(得分:1)

您尝试过kolla-ansible吗? 这将为您提供帮助:

  

kolla-ansible -i INVENTORY destroy 用于清理容器和   群集中的卷。

答案 1 :(得分:0)

好,所以我可以部分地按如下方式工作。

- expect:
    #command: kolla host destroy all --includedata --removeimages
    command: kolla host destroy all
    responses:
      (?i)This will delete all containers and data, are you sure? (y/n): "y"

我只需要弄清楚如何包括这些选项。

答案 2 :(得分:0)

工作解决方案是如图所示更改了响应。我也必须在命令中删除'--includedata'选项,尽管它应该可以工作。在外壳中工作。它在我的设置中不太适用,因此不会再浪费时间了。

- name: Destroy containers and images
  expect:
    command: kolla host destroy --removeimages all
    responses:
      (?i)This will delete all containers and data, are you sure? (y/n): "y"
相关问题