---
- name: Test for Sudo - large configuration
  hosts: all
  tasks:
    - name: Run tests
      block:
        - name: Test setup
          include_tasks: tasks/setup.yml

        - name: Run the role
          include_role:
            name: fedora.linux_system_roles.sudo
          vars:
            sudo_rewrite_default_sudoers_file: true
            sudo_remove_unauthorized_included_files: true
            sudo_sudoers_files:
              - path: /etc/sudoers
                defaults:
                  - "!visiblepw"
                  - always_set_home
                  - match_group_by_gid
                  - always_query_group_plugin
                  - env_reset
                  - secure_path:
                      - /sbin
                      - /bin
                      - /usr/sbin
                      - /usr/bin
                  - env_keep:
                      - COLORS
                      - DISPLAY
                      - HOSTNAME
                      - HISTSIZE
                      - KDEDIR
                      - LS_COLORS
                      - MAIL
                      - PS1
                      - PS2
                      - QTDIR
                      - USERNAME
                      - LANG
                      - LC_ADDRESS
                      - LC_CTYPE
                      - LC_COLLATE
                      - LC_IDENTIFICATION
                      - LC_MEASUREMENT
                      - LC_MESSAGES
                      - LC_MONETARY
                      - LC_NAME
                      - LC_NUMERIC
                      - LC_PAPER
                      - LC_TELEPHONE
                      - LC_TIME
                      - LC_ALL
                      - LANGUAGE
                      - LINGUAS
                      - _XKB_CHARSET
                      - XAUTHORITY
                user_specifications:
                  - users:
                      - root
                    hosts:
                      - ALL
                    operators:
                      - ALL
                    commands:
                      - ALL
                  - users:
                      - "%wheel"
                    hosts:
                      - ALL
                    operators:
                      - ALL
                    commands:
                      - ALL
                default_overrides:
                  - type: user
                    defaults:
                      - "!requiretty"
                    users:
                      - PINGERS
                  - type: runas
                    defaults:
                      - "!set_logname"
                    operators:
                      - root
                  - type: host
                    defaults:
                      - "!requiretty"
                      - "!set_logname"
                    hosts:
                      - host1
                      - host2
                  - type: command
                    defaults:
                      - "!requiretty"
                    commands:
                      - /usr/bin/cd
                include_directories:
                  - /etc/sudoers.d
                aliases:
                  cmnd_alias:
                    - name: PING
                      commands:
                        - /usr/bin/ping
                  host_alias:
                    - name: HOST
                      hosts:
                        - myhost
                  user_alias:
                    - name: PINGERS
                      users:
                        - username
                  runas_alias:
                    - name: RUNAS
                      users:
                        - username
              - path: /etc/sudoers.d/pingers
                default_overrides:
                  - type: user
                    defaults:
                      - "!requiretty"
                    users:
                      - PINGERS
                  - type: runas
                    defaults:
                      - "!set_logname"
                    operators:
                      - root
                  - type: host
                    defaults:
                      - "!requiretty"
                      - "!requiretty"
                    hosts:
                      - host1
                      - host2
                  - type: command
                    defaults:
                      - "!requiretty"
                    commands:
                      - /usr/bin/ls
              - path: /etc/sudoers.d/root
                defaults:
                  - syslog=auth
                default_overrides:
                  - type: runas
                    defaults:
                      - "!set_logname"
                    operators:
                      - root

        - name: Get stat of {{ ok_path }}
          delegate_to: localhost
          vars:
            ok_path: files/tests_large_configuration_sudoers.ok
          stat:
            path: "{{ ok_path }}"
            checksum_algorithm: sha256
          register: sudoers_ok

        - name: Check sudoers
          include_tasks: tasks/assert_files_identical.yml
          vars:
            __sudo_ok_path: files/tests_large_configuration_sudoers.ok
            __sudo_test_path: /etc/sudoers

        - name: Check pingers
          include_tasks: tasks/assert_files_identical.yml
          vars:
            __sudo_ok_path: files/tests_large_configuration_pingers.ok
            __sudo_test_path: /etc/sudoers.d/pingers

        - name: Check root
          include_tasks: tasks/assert_files_identical.yml
          vars:
            __sudo_ok_path: files/tests_large_configuration_root.ok
            __sudo_test_path: /etc/sudoers.d/root

      always:
        - name: Test cleanup
          include_tasks: tasks/cleanup.yml
