Introduction
In Ansible, the args rule is a critical aspect of ensuring that your playbook's task arguments align with the plugin's documentation. It's vital to maintain a proper structure and adhere to the required parameters for each module. Failure to do so can lead to unexpected behavior and issues in your automation workflow.
This rule primarily serves as a validator, confirming that your task arguments are not only present but also correctly defined. It checks if the option names are valid and if they have the correct values. Additionally, it examines conditions related to these options, such as mutually exclusive, required together, required one of, and more.
Here are some possible messages that this rule might generate:
args[module] - missing required arguments: ...
args[module] - missing parameter(s) required by ...
Let's delve into some examples to understand this rule better.
Problematic Code
``yaml
---
- name: Fixture to validate module options failure scenarios
hosts: all
tasks:
- name: Clone content repository
ansible.builtin.git: # <- Required option repo is missing.
dest: /home/www
accept_hostkey: true
version: master
update: false
- name: Enable service httpd and ensure it is not masked
ansible.builtin.systemd: # <- Missing 'name' parameter required by 'enabled'.
enabled: true
masked: false
- name: Use quiet to avoid verbose output
ansible.builtin.assert:
test:
- my_param <= 100
- my_param >= 0
quiet: invalid # <- Value for option quiet is invalid.
`
Output
`bash
WARNING Listing 2 violation(s) that are fatal
args[module]: missing required arguments: repo (warning)
args.yml:5 Task/Handler: Clone content repository
args[module]: missing parameter(s) required by 'enabled': name (warning)
args.yml:12 Task/Handler: Enable service httpd and ensure it is not masked
Read documentation for instructions on how to ignore specific rule violations.
Rule Violation Summary
count tag profile rule associated tags
2 args[module] syntax, experimental (warning)
Passed: 0 failure(s), 2 warning(s) on 1 files. Last profile that met the validation criteria was 'production'. Rating: 5/5 star
`
Correct Code
`yaml
---
- name: Fixture to validate module options pass scenario
hosts: all
tasks:
- name: Clone content repository
ansible.builtin.git: # <- Contains required option repo`.
repo: https://github.com/ansible/ansible-examples
dest: /home/www
accept_hostkey: true
version: master
update: false
- name: Enable service httpd and ensure it is not masked
ansible.builtin.systemd: # <- Contains 'name' parameter required by 'enabled'.
name: httpd
enabled: false
masked: false
- name: Use quiet to avoid verbose output
ansible.