【问题标题】:Can't run openssl command with shell module in Ansible无法在 Ansible 中使用 shell 模块运行 openssl 命令
【发布时间】:2021-05-06 15:09:58
【问题描述】:

我正在尝试使用 Ansible 的 shell 模块运行此命令:

openssl s_client -connect router1.lab.net:50051

我的 Ansible 游戏是这样的:

---
- hosts: lab
  connection: local
  tasks:
    - name: Check cert with OpenSSL
      shell: |
        openssl s_client -connect {{ ansible_host }}:50051
      register: cert
      ignore_errors: yes
    - name: Print cert data
      debug:
        msg: "{{ cert }}"
      when: cert is succeeded

当我使用 -vvv 运行 playbook 时,出现以下错误:

 "stderr": "139631224861120:error:0200206F:system library:connect:Connection refused:../crypto/bio/b_sock2.c:110:\n139631224861120:error:2008A067:BIO routines:BIO_connect:connect error:../crypto/bio/b_sock2.c:111:\nconnect:errno=111",
    "stderr_lines": [
        "139631224861120:error:0200206F:system library:connect:Connection refused:../crypto/bio/b_sock2.c:110:",
        "139631224861120:error:2008A067:BIO routines:BIO_connect:connect error:../crypto/bio/b_sock2.c:111:",
        "connect:errno=111"
    ]

当我从 Linux 命令行运行相同的 openssl 命令并给出正确的输出时,它会起作用。 我尝试安装 ansible-galaxy collection install community.crypto (https://ansible.fontein.de/collections/community/crypto/x509_certificate_pipe_module.html) 但没有帮助。

【问题讨论】:

    标签: ansible


    【解决方案1】:

    您的问题是{{ ansible_host }}。它是一个 ansible 变量,您可以设置它来告诉 ansible 要连接的主机的 IP 或主机名。
    在您的情况下,可能是local,因为您使用的是本地连接。
    这可能就是您收到“连接被拒绝”的原因。

    使用localhost 连接到本地计算机,或使用 IP 或可解析的主机名连接到远程主机。
    如果您连接到本地主机并且主机名是可解析的,您也可以尝试使用{{ ansible_hostname }}

    请注意,这些变量的值取决于当前运行任务的主机。

    查看special variables的列表。

    编辑 1

    我才意识到,你正在尝试做什么。
    使用get_certificate 模块从主机获取证书。正如 Zeitounator 所指出的,在 shell 中使用 openssl 来做到这一点是非常糟糕的做法。您需要使用上述正确的主机。

    【讨论】:

    • @stminion001 {{ inventory_hostname }} 在您的特定情况下将具有正确的主机名,但我怀疑您是否想使用它(例如,您可能还想使用 localhost 来检查已安装的证书,即使任务在远程目标上运行...)。确定您是否发送正确 url 的一个好方法是调试其内容。现在您(希望)有正确的模块,您应该重试该模块,因为在 shell 中运行 openssl 绝对不是 ansible 的好习惯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多