---
- name: Verify Match and Host keywords are rendered as expected
  hosts: all
  gather_facts: true
  vars:
    __ssh_test_backup_files:
      - /etc/ssh/ssh_config.d/00-ansible.conf
      - /etc/ssh/ssh_config

  tasks:
    - name: Backup configuration files
      include_tasks: tasks/backup.yml

    - name: Run role
      include_role:
        name: linux-system-roles.ssh
      vars:
        # noqa var-naming
        ssh:
          Match:
            - Condition: user testuser
              LogLevel: DEBUG2
            - Condition: host example
              Hostname: example.com
          Host:
            - Condition: local
              User: localuser
            - Condition: remote
              Hostname: remote.example.com

    - name: Verify the configuration file was created in the right place
      block:
        - name: Download the configuration file
          slurp:
            src: "{{ main_ssh_config }}"
          register: config

        - name: Verify the options are in the file
          assert:
            that:
              - "'Match user testuser' in config.content | b64decode"
              - "'LogLevel DEBUG2' in config.content | b64decode"
              - "'Match host example' in config.content | b64decode"
              - "'Hostname example.com' in config.content | b64decode"
              - "'Host local' in config.content | b64decode"
              - "'User localuser' in config.content | b64decode"
              - "'Host remote' in config.content | b64decode"
              - "'Hostname remote.example.com' in config.content | b64decode"

    - name: Verify also runtime configuration
      when:
        - ansible_facts['distribution'] not in ['CentOS', 'RedHat'] or
          ansible_facts['distribution_version'] | int > 6
      block:
        - name: Run first ssh in test mode to evaluate the configuration
          command: ssh -G testuser@example
          register: test1
          changed_when: false

        - name: Run second ssh in test mode to evaluate the configuration
          command: ssh -G local
          register: test2
          changed_when: false

        - name: Run third ssh in test mode to evaluate the configuration
          command: ssh -G remote
          register: test3
          changed_when: false

        - name: Verify the options are correctly evaluated
          assert:
            that:
              - "'loglevel DEBUG2' in test1.stdout"
              - "'loglevel DEBUG2' not in test2.stdout"
              - "'loglevel DEBUG2' not in test3.stdout"
              - "'hostname example.com' in test1.stdout"
              - "'hostname example.com' not in test2.stdout"
              - "'hostname example.com' not in test3.stdout"
              - "'user localuser' in test2.stdout"
              - "'user localuser' not in test3.stdout"
              - "'user localuser' not in test1.stdout"
              - "'hostname remote.example.com' in test3.stdout"
              - "'hostname remote.example.com' not in test1.stdout"
              - "'hostname remote.example.com' not in test2.stdout"

    - name: Restore configuration files
      include_tasks: tasks/restore.yml
