【问题标题】:ansible: how to use a interactive python script (custom module)ansible:如何使用交互式 python 脚本(自定义模块)
【发布时间】:2020-05-31 13:19:35
【问题描述】:

请告知如何使用 python 交互脚本作为自定义模块。

我的 python 脚本有时会出现如下提示。

[ansible@localhost ~]$ python test.py
No errors found.
do you want to proceed [y/n]
[ansible@localhost ~]$

sometimes:

[ansible@localhost ~]$ python test.py
3 Errors found.
Cant proceed with script.
[ansible@localhost ~]$

输出结果和交互式输出可能会因条件而异,写在我的脚本中。

我想了解如何在ansible的自定义模块下使用这个脚本,并处理交互式提示响应..

我读到expect 模块可以帮助 ansible 提供响应,但在我的脚本中,响应不能始终相同。

例如,我尝试过期望模块,但由于我的 python 脚本结果不同,它失败了,它根据条件有 4-6 种响应。

"invocation": {
    "module_args": {
        "chdir": null,
        "command": "/usr/bin/python3 test3.py",
        "creates": null,
        "echo": true,
        "removes": null,
        "responses": {
            "do you want to proceed [y/n]": "y"
        },
        "timeout": 30

上面失败的输出是ansible期望上面提到的响应,但我的实际python脚本结果不同。

请指教。

谢谢。

【问题讨论】:

    标签: python ansible


    【解决方案1】:

    如果我理解了这个问题,那么您的脚本会为您提供多种可能性。这就是我将使用 yml 文件中的 pexpect 模块和 ansible-playbook 处理它的方式:

    ---
    
    - hosts      : localhost
      remote_user: USER_NAME
      tasks      :
        - name  : run the script
          expect:
            echo     : yes
            command  : /usr/bin/python3 test3.py
            timeout  : 30
            responses:
              (?i)do you want to proceed [y/n]                                       : "y"
              (?i)2nd OPTION                                                         : "y/n"
              (?i)3rd OPTION                                                         : "y/n"
    

    【讨论】:

      猜你喜欢
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多