robertdebock / ansible-role-rsyslog

Install and configure rsyslog on your system.
https://robertdebock.nl/
Apache License 2.0
37 stars 40 forks source link

Asserted type for rsyslog_dircreatemode and rsyslog_filecreatemode is incorrect #13

Closed benformosa closed 4 years ago

benformosa commented 4 years ago

Describe the bug

The assert.yml task list asserts that the rsyslog_dircreatemode and rsyslog_filecreatemode variables should be numbers. Before fa480e94, this assert was | int, which did pass, but didn't actually test that the variable was set correctly.

Rsyslog's documentation specifies:

The value given must always be a 4-digit octal number, with the initial digit being zero

https://www.rsyslog.com/doc/v8-stable/configuration/action/rsconf1_filecreatemode.html

It might be better to specify the variable as an octal number, then ensure that it is formatted correctly in the template.

Output

    TASK [ansible-role-rsyslog : test if rsyslog_dircreatemode is set correctly] ***
fatal: [rsyslog-alpine-latestpy38-ansible-current]: FAILED! => changed=false 
  assertion: rsyslog_dircreatemode is number
  evaluated_to: false
  msg: Assertion failed

https://github.com/robertdebock/ansible-role-rsyslog/runs/1383861975

benformosa commented 4 years ago

Here's an example of how the mode might be represented as an octal literal. This might be a bit confusing, as this is basically the opposite of how modes are used with the file module etc.

Another, probably simpler approach would be to assume that rsyslog_dircreatemode will be a string with 4 characters, starting with 0, and never attempt to convert it to an int.

Playbook

---
- hosts: localhost
  gather_facts: false
  vars:
    # my_mode: 420
    my_mode: 0644
  tasks:

    - name: Display variables
      debug:
        var: my_mode

    - name: Display variable type
      debug:
        var: my_mode | type_debug

    - name: test if my_mode is set correctly
      assert:
        that:
          - my_mode | int is number
          - my_format is match('0')
          - my_length | int is eq(4)
      vars:
        my_format: "{{ '{0:04o}'.format(my_mode | int) }}"
        my_length: "{{ my_format | length }}"

    - name: format as 4-digit number with leading 0
      debug:
        msg: "mode is: {{ '{0:04o}'.format(my_mode | int) }}"

Output

PLAY [localhost] ***************************************************************

TASK [Display variables] *******************************************************
ok: [localhost] => {
    "my_mode": 420
}

TASK [Display variable type] ***************************************************
ok: [localhost] => {
    "my_mode | type_debug": "int"
}

TASK [test if my_mode is set correctly] ****************************************
ok: [localhost] => {
    "changed": false,
    "msg": "All assertions passed"
}

TASK [format as 4-digit number with leading 0] *********************************
ok: [localhost] => {
    "msg": "mode is: 0644"
}

PLAY RECAP *********************************************************************
localhost  : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
robertdebock commented 4 years ago

You are correct, I've just pushed a commit to fix this. Give CI a bit of time to test everything and I'll release a version when done.

robertdebock commented 4 years ago

This has been fixed in release 3.0.1, Thanks once again @benformosa for notifying me!