Module development bug - Should this also be an Ansible bug?

Hello,
I came across a bug in a published collection, and have submitted an issue and PR to that repository, but I was also wondering if this would also warrant an Ansible bug.

In the module in question, it seems that someone had originally called an argument user, and then later renamed it to username, with an alias of user. But they did not make this change everywhere. So the argument spec handed to Ansible Module was changed, but a call retrieving the argument using module.params[‘user’] was left in the code. And in fact there was no call using module.params[‘username’] or anything else using the new argument name. These common arguments of host/username/password also have environment variable fallbacks. So I discovered this when I went to use the module with the environment variables, it throws a KeyError on the module.params[‘user’] line in the module. The same thing happens if you use the correct/preferred argument name of username in the task. But if you use the alias of user in the task, which is also still prevalent in the examples, the module works.

This seems like it could warrant either some tighter or looser checking in the core AnsibleModule code. I can see a looser approach, where the name and all aliases for an argument can be used to retrieve the value might be one approach. Or a stricter approach where only the main name of the argument is allowed, and anything else fails with a KeyError regardless of the way the argument was populated. The current behavior at least on 2.17.4 seems be the worst parts of both, where the alias name gets a value when the alias was used, but not when the updated name gets used, or when using the environment variables.

I hope that made some sense, and if there is a feeling that it might warrant more investigation, I would be happy to participate, but was just looking to get a feel before I dove in to far beyond just fixing the offending collection code. Thanks in advance for your feedback!

1 Like

@netgirard hello, thanks for the post!
If you think that this is a bug of ansible-core, just go and open an issue there:)

Thanks

1 Like

IMO, always including aliases in module.params seems cluttered, so I’d personally be more in favor of making it strict and popping aliases out of the module.params. That seems like a pretty trivial change - this line could be followed by something like

        for alias in self.aliases:
            self.params.pop(alias, None)

It probably needs some discussion before it’s changed (if you open an issue in the ansible/ansible repo it will be), since I guess it wouldn’t be backward compatible for a module trying to detect alias usage.