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.
More on Inventory
What is an inventory and why does Ansible need one?BeginnerWhat are the INI and YAML inventory formats and how do they differ?BeginnerHow do you create groups of groups?BeginnerExplain host patterns: `:`, `:&`, `:!` and wildcards.IntermediateWhich connection variables can you set per host in inventory?BeginnerWhat are `group_vars` and `host_vars` directories?Intermediate