如何使用 ansible 'expect' 模块来处理多个不同的响应?

2024-01-04

在这里,我尝试测试我的 bash 脚本,它提示了四次。

#!/bin/bash
date >/opt/prompt.txt
read -p "enter one: " one
echo $one
echo $one >>/opt/prompt.txt
read -p "enter two: " two
echo $two
echo $two >>/opt/prompt.txt
read -p "enter three: " three
echo $three
echo $three >>/opt/prompt.txt
read -p "enter password: " password
echo $password
echo $password >>/opt/prompt.txt

对于这个脚本,我编写了下面的代码,并且运行良好

- hosts: "{{ hosts }}"
  tasks:
  - name: Test Script
    expect:
      command: sc.sh
      responses:
        enter one: 'one'
        enter two: 'two'
        enter three: 'three'
        enter password: 'pass'
      echo: yes

但如果我做同样的事情mysql_secure_installation命令它不工作

- hosts: "{{ hosts }}"
  tasks:
  - name: mysql_secure_installation Command Test
    expect:
      command: mysql_secure_installation
      responses:
        'Enter current password for root (enter for none):': "\n"
        'Set root password? [Y/n]:': 'y'
        'New password:': '123456'
        'Re-enter new password:': '123456'
        'Remove anonymous users? [Y/n]:': 'y'
        'Disallow root login remotely? [Y/n]:': 'y'
        'Remove test database and access to it? [Y/n]:': 'y'
        'Reload privilege tables now? [Y/n]:': 'y'

      echo: yes

它的引用在这里:

PLAY [S1] **********************************************************************

TASK [setup] *******************************************************************
ok: [S1]

TASK [mysql_secure_installation Command Test] **********************************
fatal: [S1]: FAILED! => {"changed": true, "cmd": "mysql_secure_installation", "delta": "0:00:30.139266", "end": "2016-07-15 15:36:32.549415", "failed": true, "rc": 1, "start": "2016-07-15 15:36:02.410149", "stdout": "\r\n\r\n\r\n\r\nNOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL\r\n      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!\r\n\r\n\r\nIn order to log into MySQL to secure it, we'll need the current\r\npassword for the root user.  If you've just installed MySQL, and\r\nyou haven't set the root password yet, the password will be blank,\r\nso you should just press enter here.\r\n\r\nEnter current password for root (enter for none): ", "stdout_lines": ["", "", "", "", "NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL", "      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!", "", "", "In order to log into MySQL to secure it, we'll need the current", "password for the root user.  If you've just installed MySQL, and", "you haven't set the root password yet, the password will be blank,", "so you should just press enter here.", "", "Enter current password for root (enter for none): "]}

NO MORE HOSTS LEFT *************************************************************
    to retry, use: --limit @/home/jackson/AnsibleWorkSpace/AnsibleTest/example1.retry

PLAY RECAP *********************************************************************
S1                         : ok=1    changed=0    unreachable=0    failed=1  

我也尝试过空白''代替"\n"对于第一个答案,但它也不起作用。我也访问过 Ansibleexpect http://docs.ansible.com/ansible/expect_module.htmldoc,但它们仅显示非常简单的示例和解释。我还尝试对多个不同的响应进行正则表达式匹配,但它也不起作用。
请不要推荐我使用Ansible的mysql模块,因为我在这里的目的是学习这个模块以供将来使用。


原因是问题被解释为正则表达式。因此,必须对正则表达式中具有特殊含义的字符进行转义,例如 -()[]\?*。等等。

Hence:

'Enter current password for root (enter for none):'

应该是:

'Enter current password for root \(enter for none\):'

祝你好运!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 ansible 'expect' 模块来处理多个不同的响应? 的相关文章

  • Jupyter Notebooks 不显示进度条

    我正在尝试在 Jupyter 笔记本中显示进度条 这是一台新电脑 我通常做的事情似乎不起作用 from tqdm import tqdm notebook example iter 1 2 3 4 5 for rec in tqdm not
  • 为什么我的代码不能根据字典解码加密字符串?

    我有一本字典 其中包含代表字母的键和值 例如一个简单的 DICT CODE b g n a p o x d t y 我收到了一个加密代码 并将该字符串转换为一个列表 其中每个项目都是一个单词 我需要根据字典中的项目来解决它 代码示例是 wo
  • 查找模块中显式定义的函数 (python)

    好的 我知道您可以使用 dir 方法列出模块中的所有内容 但是有什么方法可以仅查看该模块中定义的函数吗 例如 假设我的模块如下所示 from datetime import date datetime def test return Thi
  • 根据开始列和结束列扩展数据框(速度)

    我有一个pandas DataFrame含有start and end列 加上几个附加列 我想将此数据框扩展为一个时间序列 从start值并结束于end值 但复制我的其他专栏 到目前为止 我想出了以下内容 import pandas as
  • numpy 使用 datetime64 进行数字化

    我似乎无法让 numpy digitize 与 datetime64 一起使用 date bins np array np datetime64 datetime datetime 2014 n 1 s for n in range 1 1
  • 登录网站并使用 python 请求下载文件

    我有一个带有 HTML 表单的网站 登录后 它会将我带到 start php 站点 然后将我重定向到overview php 我想从该服务器下载文件 当我单击 ZIP 文件的下载链接时 链接后面的地址是 getimage php path
  • 可以用 Django 制作移动应用程序吗?

    我想知道我是否可以在我的网站上使用 Django 代码 并以某种方式在移动应用程序 Flutter 等框架中使用它 那么是否可以使用我现在拥有的 Django 后端并在移动应用程序中使用它 所以就像models views etc 是的 有
  • 如何将 self 传递给装饰器?

    我该如何通过self key下面进入装饰器 class CacheMix object def init self args kwargs super CacheMix self init args kwargs key func Cons
  • WindowsError:[错误 126] 使用 ctypes 加载操作系统时

    python代码无法在Windows 7平台上运行 def libSO lib ctypes cdll LoadLibrary ConsoleApplication2 so lib cfoo2 1 3 当我尝试运行它时 得到来自python
  • 使用 scipy curve_fit 拟合噪声指数的建议?

    我正在尝试拟合通常按以下方式建模的数据 def fit eq x a b c d e return a 1 np exp x b c np exp x d e x np arange 0 100 0 001 y fit eq x 1 1 1
  • 动态 __init_subclass__ 方法的参数绑定

    我正在尝试让类装饰器工作 装饰器会添加一个 init subclass 方法到它所应用的类 但是 当该方法动态添加到类中时 第一个参数不会绑定到子类对象 为什么会发生这种情况 举个例子 这是可行的 下面的静态代码是我试图最终得到的示例 cl
  • `list()` 被认为是一个函数吗?

    list显然是内置类型 https docs python org 3 library stdtypes html list在Python中 我看到底下有一条评论this https stackoverflow com a 53645813
  • 具有屏蔽无效值的 pcolormesh

    我试图将一维数组绘制为 pcolormesh 因此颜色沿 x 轴变化 但每个 x 的 y 轴保持不变 但我的数据有一些错误值 因此我使用屏蔽数组和自定义颜色图 其中屏蔽值设置为蓝色 import numpy as np import mat
  • PyTorch DataLoader 对并行运行的批次使用相同的随机种子

    有一个bug https tanelp github io posts a bug that plagues thousands of open source ml projects 在 PyTorch Numpy 中 当并行加载批次时Da
  • Pandas style.bar 颜色基于条件?

    如何渲染其中一列的 Pandas dfstyle bar color属性是根据某些条件计算的 Example df style bar subset before after color ff781c vmin 0 0 vmax 1 0 而
  • 将文本注释到轴并对齐为圆

    我正在尝试在轴上绘制文本并将该文本与圆对齐 更准确地说 有一些具有不同坐标 x y 的点位于该圆内 并使用以下命令创建 ax scatter x y s 100 我想用圆圈连接并标记每个点 Cnameb 文本的坐标由 xp yp 定义 因此
  • Python 类型安全吗?

    根据维基百科 https en wikipedia org wiki Type system Type safety and memory safety 如果一种语言不允许违反类型系统规则的操作或转换 计算机科学家就认为该语言是 类型安全的
  • 由于 json 字符串化 dict 键导致数据丢失

    考虑下面的例子 gt gt gt import json gt gt gt d 0 potato 0 spud gt gt gt json dumps d 0 potato 0 spud gt gt gt json loads json d
  • 如何循环遍历字典列表并打印特定键的值?

    我是 Python 新手 有一个问题 我知道这是一个非常简单的问题 运行Python 3 4 我有一个需要迭代并提取特定信息的列表 以下是列表 称为部分 的示例 已截断 数千个项目 state DEAD id phwl type name
  • 在 Django shell 会话期间获取 SQL 查询计数

    有没有办法打印 Django ORM 在 Django shell 会话期间执行的原始 SQL 查询的数量 Django 调试工具栏已经提供了此类信息 例如 5 QUERIES in 5 83MS但如何从 shell 中获取它并不明显 您可

随机推荐