| 1 |
- hosts: localhost |
| 2 |
gather_facts: false |
| 3 |
tasks: |
| 4 |
- name: Detect syslog |
| 5 |
stat: |
| 6 |
path: /var/log/syslog |
| 7 |
register: syslog |
| 8 |
|
| 9 |
- name: Detect journalctl |
| 10 |
shell: command -V journalctl |
| 11 |
ignore_errors: yes |
| 12 |
changed_when: no |
| 13 |
register: journalctl |
| 14 |
|
| 15 |
- block: |
| 16 |
- name: Skip tests if logs were not found. |
| 17 |
debug: |
| 18 |
msg: Did not find /var/log/syslog or journalctl. Tests will be skipped. |
| 19 |
- meta: end_play |
| 20 |
when: journalctl is failed and not syslog.stat.exists |
| 21 |
|
| 22 |
- name: Generate random numbers for unique log entries |
| 23 |
set_fact: |
| 24 |
good_number: "{{ 999999999999 | random }}" |
| 25 |
bad_number: "{{ 999999999999 | random }}" |
| 26 |
|
| 27 |
- name: Generate expected log entry messages |
| 28 |
set_fact: |
| 29 |
good_message: 'My number is: ({{ good_number }})' |
| 30 |
bad_message: 'My number is: ({{ bad_number }})' |
| 31 |
|
| 32 |
- name: Generate log message search patterns |
| 33 |
set_fact: |
| 34 |
# these search patterns are designed to avoid matching themselves |
| 35 |
good_search: '{{ good_message.replace(":", "[:]") }}' |
| 36 |
bad_search: '{{ bad_message.replace(":", "[:]") }}' |
| 37 |
|
| 38 |
- name: Generate grep command |
| 39 |
set_fact: |
| 40 |
grep_command: "grep -e '{{ good_search }}' -e '{{ bad_search }}'" |
| 41 |
|
| 42 |
- name: Run a module that logs without no_log |
| 43 |
module_that_logs: |
| 44 |
number: "{{ good_number }}" |
| 45 |
|
| 46 |
- name: Run a module that logs with no_log |
| 47 |
module_that_logs: |
| 48 |
number: "{{ bad_number }}" |
| 49 |
no_log: yes |
| 50 |
|
| 51 |
- name: Search for expected log messages |
| 52 |
# if this fails the tests are probably running on a system which stores logs elsewhere |
| 53 |
shell: "({{ grep_command }} /var/log/syslog) || (journalctl | {{ grep_command }})" |
| 54 |
changed_when: no |
| 55 |
register: grep |
| 56 |
|
| 57 |
- name: Verify the correct log messages were found |
| 58 |
assert: |
| 59 |
that: |
| 60 |
# if the good message is not found then the cause is likely one of: |
| 61 |
# 1) the remote system does not write user.info messages to the logs |
| 62 |
# 2) the AnsibleModule.log method is not working |
| 63 |
- good_message in grep.stdout |
| 64 |
- bad_message not in grep.stdout |
| 65 |
|
| 66 |
- name: Generate a unique marker for the sub option log entry |
| 67 |
set_fact: |
| 68 |
suboption_marker: "marker{{ 999999999999 | random }}" |
| 69 |
|
| 70 |
- name: Run a module with no_log sub options too short to be registered as secrets |
| 71 |
module_with_no_log_suboptions: |
| 72 |
marker: "{{ suboption_marker }}" |
| 73 |
secret: r7t |
| 74 |
opts: |
| 75 |
secret: q7z |
| 76 |
plain: visible |
| 77 |
entries: |
| 78 |
- secret: z7q |
| 79 |
plain: alsovisible |
| 80 |
|
| 81 |
- name: Search for the sub option log entry |
| 82 |
shell: "(grep -e '{{ suboption_marker }}' /var/log/syslog) || (journalctl | grep -e '{{ suboption_marker }}')" |
| 83 |
changed_when: no |
| 84 |
register: suboption_grep |
| 85 |
|
| 86 |
- name: Verify no_log sub options were redacted from the log entry |
| 87 |
assert: |
| 88 |
that: |
| 89 |
- "'Invoked with' in suboption_grep.stdout" |
| 90 |
- "'r7t' not in suboption_grep.stdout" |
| 91 |
- "'q7z' not in suboption_grep.stdout" |
| 92 |
- "'z7q' not in suboption_grep.stdout" |
| 93 |
- "\"'plain': 'visible'\" in suboption_grep.stdout" |
| 94 |
- "\"'plain': 'alsovisible'\" in suboption_grep.stdout" |
| 95 |
- "' secret=$REDACTED$' in suboption_grep.stdout" |
| 96 |
- "\"'secret': '$REDACTED$'\" in suboption_grep.stdout" |