- name: Pretend to install stuff.
debug: var=item
with_items: [git, httpd, vim]
when:
- "'all' in ansible_run_tags or item in ansible_run_tags"
- item not in ansible_skip_tags
tags: always
If you run without any tags it will install everything and if you specify --tags=git it will only install git and if you specify --skip-tags=vim it will do the correct thing.Ref: https://docs.ansible.com/ansible/latest/reference_appendices...
And in the specific case of installing packages you can speed this process up dramatically with something like the following without having to use tags.
- name: Probe the host for installed packages.
package_facts:
- name: Install packages.
package: name={{ item }}
with_items: [git, httpd, vim]
when: item not in ansible_facts.packages
But this itself is really inefficient too because you're still calling the module for every package instead of calling the module once with all the packages. Older versions of Ansible did this automatically but it was super magic so it's probably better that it was removed. We can do it ourselves though. - name: Install packages.
package: name={{ to_install }}
vars:
packages: [git, httpd, vim]
to_install: "{{ packages | difference(ansible_facts.packages) | list }}"