Ansible-Lint warning for custom module argument exception

376 Views Asked by At

I have a custom module to manage Keycloak realms. That module takes a long list of arguments including a sub-dictionary for smtp_server:


def keycloak_argument_spec():
  return dict(
    auth_keycloak_url=dict(type='str', aliases=['url'], required=True),
    auth_client_id=dict(type='str', default='admin-cli'),
    auth_realm=dict(type='str', required=True),
    auth_client_secret=dict(type='str', default=None),
    auth_username=dict(type='str', aliases=['username'], required=True),
    auth_password=dict(type='str', aliases=['password'], required=True, no_log=True),
    validate_certs=dict(type='bool', default=True)
  )

def main():
  argument_spec = keycloak_argument_spec()

  meta_args = dict(
    state=dict(default='present', choices=['present', 'absent']),
    realm=dict(type='str', required=True),
    smtp_server=dict(type='dict', aliases=['smtpServer'], options={
      'host': dict(default='localhost'),
      'port': dict(type='int', default=25),
      'auth': dict(type='bool', default=False),
      'ssl': dict(type='bool', default=False),
      'starttls': dict(type='bool', default=False),
      'user': dict(no_log=True),
      'password': dict(no_log=True),
      'envelopeFrom': dict(),
      'from': dict(),
      'fromDisplayName': dict(),
      'replyTo': dict(),
      'replyToDisplayName': dict(),
    }),
  )
  
  argument_spec.update(meta_args)

  module = AnsibleModule(
    argument_spec = argument_spec,
    supports_check_mode=True
  )

Here is a task with that module.

- name: "Create realm"
  keycloak_realm:
    realm: "{{ keycloak_realm.key }}"
    smtp_server:
      host: "{{ keycloak_realm.value.mail.host | default('localhost') }}"
      port: "{{ keycloak_realm.value.mail.port | default(25) | int }}"
      starttls: "{{ keycloak_realm.value.mail.starttls | default(false) }}"
      ssl: "{{ keycloak_realm.value.mail.ssl | default(false) }}"
      auth: "{{ keycloak_realm.value.mail.auth | default(false) }}"
      user: "{{ keycloak_realm.value.mail.user | default(omit) }}"
      password: "{{ keycloak_realm.value.mail.password | default(omit) }}"
      replyTo: "{{ keycloak_realm.value.mail.replyto | default(omit) }}"
      from: "{{ keycloak_realm.value.mail.from | default(omit) }}"
      fromDisplayName: "{{ keycloak_realm.value.mail.from_name | default(omit) }}"
    state: "present"

But when I run ansible-lint against a task using that module, I'm getting a strange message.

WARNING  Ignored exception from ArgsRule.<bound method AnsibleLintRule.matchtasks of args: Validating module arguments.> while processing roles/keycloak/tasks/install/configure_realm.yml (tasks): 'port'

I'm using

$ ansible-lint --version
ansible-lint 6.14.2 using ansible 2.14.1

I've tried using ansible-lint -vv to get that exception. But I don't get the stack trace, so I cannot investigate the problem. When I run the task, everything is fine. But ansible-lint has a problem with that dict.

It doesn't have something to do with "port"-attribute, when I rearrange the order of the args in the module, another attribute is a problem (but only int or bool types have a problem). When I redefine "port" to type=str another not str attribute will raise the warning.

There is also another way of defining the smtp_server dictionary, like

    smtp_server=dict(type='dict', aliases=['smtpServer'], options=dict(
      host=dict(default='localhost'),
      port=dict(type='int', default=25),

which I like more, but "from" is a keyword and the warning comes anyway.

I also tried with "default(omit)" in the task, because the value is optional and ansible-lint has no idea, what is in keycloak_realm. But it's the same.

Any ideas to avoid that warning?

0

There are 0 best solutions below