What does the `add_host` module do and when is it useful?

AdvancedConceptInventory
Answer

add_host adds a host (and optionally groups and variables) to the in-memory inventory during a run. It is lost when the playbook ends.

- hosts: localhost
  tasks:
    - amazon.aws.ec2_instance:
        name: temp-build
        image_id: ami-0abc1234567890def
        instance_type: t3.micro
        wait: true
      register: ec2
    - ansible.builtin.add_host:
        name: "{{ ec2.instances[0].private_ip_address }}"
        groups: new_nodes
- hosts: new_nodes
  tasks:
    - ansible.builtin.ping:

It bridges provisioning and configuration in one playbook. Note it runs once per play, not per host, so loop over results explicitly.

All Ansible questions Roadmap: Ansible

More on Inventory