0

I am trying to find whether the host VM's are set with ssh connection is 36000. Below is my code..

  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

It actually works fine, but i would like to get the ClientAliveInterval value printed in the output. Can anyone help me with this?

user1509613
  • 151
  • 2
  • 3
  • 9

1 Answers1

0

It actually works fine, but i would like to get the ClientAliveInterval value printed in the output.

What you're asking for isn't a single module in Ansible. You'll first have to read in the file, then print out the line(s) that contain the value you want in your output:

    - 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.*') }}"

When I run that on my test system, I get this for the final output:

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

If you need to reference the value on the line, you'll have to replace the "debug: / msg:" block with a "set_fact:" call instead.

The "b64decode" is needed because the "slurp:" module sanitizes the file contents as Base64 encoded text.

dan_linder
  • 881
  • 1
  • 9
  • 30