Ansible Recipes

The ansible Command Line Tool

The Default Module

ansible's default module is ansible.builtin.command, which is why ansible GROUP -a “arg1 arg2 ...” can be used to execute commands despite only having arguments.


Displaying Ansible Facts

The ansible.builtin.setup module is automatically called by playbooks to gather useful variables about remote hosts that can be used in playbooks.

To see which facts can be used in a playbook, either run the command ansible <hostname> -m ansible.builtin.setup from a shell, or add the following task to a play:

- name: Print all available facts
  ansible.builtin.debug:
    var: ansible_facts

To retrieve information about which services are installed on a host (together with their state), add the following task to a play:

# use the service_facts module to populate the service facts
- name: Populate service facts
  ansible.builtin.service_facts:
  
# example usage
- name: Print debug message if the cups service is installed
  ansible.builtin.debug: "cups.service is installed"
  when: "'cups.service' in services"


Ansible Collections

Installing and Upgrading Collections

While collections may be installed manually, the recommended way is via the ansible-galaxy command, which ensures that they will be upgraded automatically along with the ansible or ansible-core packages.

The following commands can be used to install and upgrade collections:

$ ansible-galaxy collection install my_namespace.my_collection
$ ansible-galaxy collection install my_namespace.my_collection --upgrade

To list the set of installed collections, run ansible-galaxy collection list.

For more information, see docs.ansible.com/ansible/latest/user_guide/collections_using.html.