ansible也会显示该值,即使该字符串不匹配。

问题描述 投票:0回答:1

我试图找到主机虚拟机的ssh连接是否设置为36000.下面是我的代码。

  tasks:
    - name: To check SSH connection is set to 36000
      lineinfile:
       dest: /etc/ssh/sshd_config
       line: "ClientAliveInterval 36000"
      check_mode: yes
      register: presence
      failed_when: presence.changed

它实际上工作得很好,但我想在输出中打印ClientAliveInterval值。谁能帮我解决这个问题?

ansible ansible-2.x
1个回答
0
投票

它实际上工作得很好,但我想在输出中打印出ClientAliveInterval的值。

你所要求的不是Ansible中的一个模块。 你首先要读取文件,然后在输出中打印出包含你想要的值的行。

    - name: 'Read in a line'
      slurp:
        src: /etc/ssh/sshd_config
      register: found_lines

    - name: 'Show the line read'
      debug:
        msg: "{{ found_lines['content'] | b64decode | regex_search('^ClientAliveInterval.*') }}"

当我在我的测试系统上运行时,我得到的最终输出是这样的。

TASK [Show the line read] ****************
ok: [localhost] => {
    "msg": "ClientAliveInterval 36000"
}

如果你需要引用行上的值,你必须用一个 "debug:msg: "块代替 "debug: msg: "块。"set_fact:" 的调用来代替。

需要使用 "b64decode "是因为 "slurp:" 模块将文件内容消毒为Base64编码文本。

© www.soinside.com 2019 - 2024. All rights reserved.