即使字符串不匹配,也可以显示值



我正在尝试查找主机虚拟机是否设置为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 值。 谁能帮我解决这个问题?

它实际上工作正常,但我想在输出中打印 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:"块替换为"set_fact:"调用。

需要"b64decode",因为"slurp:"模块将文件内容清理为 Base64 编码文本。

相关内容

最新更新