Definition
args:
is a directive that allows you to “unpack” arguments with values into a module, most often we pass only the value of previously predefined arguments (something like **kwargs
in Python)
What does this mean? I’ll show you in practice:
- name: Create file
ansible.builtin.command: touch some_file
args:
chdir: /tmp/
creates: /tmp/some_file
Completely identical to the task:
- name: Create file
ansible.builtin.command:
cmd: touch some_file
chdir: /tmp/
creates: /tmp/some_file
But in the first option we pass the command as a string and there is simply no place to add chdir
and creates
I will give a few more useful examples:
args:
и loop:
Using args:
and loop:
(or with_X
) you can make tasks more compact, for example:
# Without args:
- name: Remove Qemu-NAT line and add DNS server to resolv.conf
ansible.builtin.lineinfile:
path: /etc/resolv.conf
state: "{{ item.state }}"
line: "{{ item.line }}"
loop:
- { state: absent, line: nameserver 10.0.2.3 }
- { state: present, line: nameserver 192.0.2.1 }
# With args:
- name: Remove Qemu-NAT line and add DNS server to resolv.conf
ansible.builtin.lineinfile:
path: /etc/resolv.conf
args: "{{ item }}"
loop:
- { state: absent, line: nameserver 10.0.2.3 }
- { state: present, line: nameserver 192.0.2.1 }
args:
и default(omit)
You can make different sets of arguments for each dictionary element similar to the behavior of default(omit):
:
# Without args:
- name: Install deb pkgs
ansible.builtin.apt:
deb: http://192.0.2.1/repo/somepackage-x86_64.deb
state: present
- name: Install pkgs on arm host
ansible.builtin.apt:
name:
- firefox
- chromium
- libreoffice
update_cache: true
state: present
install_recommends: false
# With args:
- name: Install pkgs
ansible.builtin.apt:
state: present
args: "{{ item }}"
loop:
- deb: http://192.0.2.1/repo/somepackage-x86_64.deb
- name:
- firefox
- chromium
- libreoffice
update_cache: true
install_recommends: false
Security
However, it is worth considering that using variables in this case (if you are writing a role or collection) may be unsafe, because it will be easy to override arguments and values (https://docs.ansible.com/ansible/devel/reference_appendices/faq.html#when-is-it-unsafe-to-bulk-set-task-arguments-from-a-variable)