即使字符串不匹配,也能显示该值

时间:2020-06-12 15:39:04

标签: ansible ansible-2.x

我试图查找是否通过ssh连接设置的主机VM是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值。 谁能帮我这个?

1 个答案:

答案 0 :(得分: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"
}

如果您需要引用该行上的值,则必须用"set_fact:"调用替换“ debug:/ msg:”块。

需要使用“ b64decode”,因为"slurp:"模块将文件内容作为Base64编码的文本进行消毒。