使用expect模块将ansible_python_interpreter添加到任务中无法使用become_user获取命令

使用expect模块将ansible_python_interpreter添加到任务中无法使用become_user获取命令

我正在编写一个CentOS7库存任务,其中必须进行提示对话,我认为这是 andibleexpect模块的最佳候选者。

- name: setup some command
  become: yes
  become_user: user1
  expect:
    command: some_command
    responses:
      'Do you want to continue? [yes/no]': 'y'

上述任务需要pexpect库存上的包,可用版本2.3 如下所示错误,而预期版本:> = 3.3

fatal: [target1]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "msg": "Insufficient version of pexpect installed (2.3), this module requires pexpect>=3.3. Error was 'module' object has no attribute '_run'"}

作为解决方法,我安装了python3它的pexpect库存模块,并ansible_python_interpreter在任务中指定来解决它。这次它无法找到该命令become_user

- name: setup some command
  vars:
    ansible_python_interpreter: /usr/bin/python3
  become: yes
  become_user: user1
  expect:
    command: some_command
    responses:
      'Do you want to continue? [yes/no]': 'y'

以下是任务的错误

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: pexpect.exceptions.ExceptionPexpect: The command was not found or was not executable: some_command.
fatal: [target1]: FAILED! => {"changed": false, "msg": "The command was not found or was not executable: some_command."}

请提出缺少的内容

答案1

我在 Debian 系统上遇到了同样的问题,但可以通过使用命令的完整路径使其正常工作。which例如which some_command,您可以在远程服务器上使用 来获取命令的完整路径。

使用完整路径的 Ansible playbook 任务示例

- name: setup some command
  become: yes
  become_user: user1
  expect:
    command: /usr/bin/some_command
    responses:
      'Do you want to continue? [yes/no]': 'y'

经过更多研究,我了解到这是由于 Ansible 进行了批量登录

无论如何,这是由于 Ansible 执行批量登录,它不会获取与实时登录相同的文件,这取决于您的系统配置,并且可以通过为两种类型的登录设置相同的 PATH 来更改。 来源

ansible <host> -m shell -a "echo $PATH"您可以使用Ansible 剧本或在 Ansible 剧本中亲自尝试一下

- name: check modinfo
  shell: |
    echo $PATH

# "stdout": "/usr/local/bin:/usr/bin:/bin:/usr/games",

相关内容