具有用户输入的游民互动Ansible置备

时间:2019-01-18 13:44:24

标签: ansible vagrant gcloud

我想知道是否可以使用Ansible剧本进行Vagrant设置,以执行一项需要用户输入的命令并暂停以等待用户交互的任务。

用例是使用gcloud auth login对gcloud terminal命令进行身份验证。该命令要求用户转到执行Google身份验证的网址,然后输入在授予访问权限后提供的验证码:

Go to the following link in your browser:

https://accounts.google.com/o/oauth2/....


Enter verification code:

我见过Ansible的promptwait_for模块,但看起来不能在这种情况下使用吗?

2 个答案:

答案 0 :(得分:1)

如果在这种情况下您要认证的帐户是服务帐户,请尝试gcloud auth activate-service-account。这在这里特别有用,因为它不提示任何内容,它所做的只是使用密钥文件登录服务帐户。您可以找到有关here的更多信息。

答案 1 :(得分:0)

也许expect模块适用于您的情况。您了解过吗?

来自Ansible文档的示例:

- name: Case insensitive password string match
  expect:
    command: passwd username
    responses:
      (?i)password: "MySekretPa$$word"
  # you don't want to show passwords in your logs
  no_log: true

- name: Generic question with multiple different responses
  expect:
    command: /path/to/custom/command
    responses:
      Question:
        - response1
        - response2
        - response3

您可以将其与uri模块结合使用,以在Google云上进行身份验证,注册输出,使用正则表达式或其他内容对其进行解析,然后将其用于Expect模块...

来源:Ansible Expect Module