无法在 Ansible 中使用 shell 模块运行 openssl 命令

时间:2021-05-06 03:23:45

标签: ansible

我正在尝试使用 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 运行剧本时,出现以下错误:

 "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) 但没有帮助。

1 个答案:

答案 0 :(得分:1)

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

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

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

查看special variables的列表。

编辑 1

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