Debian Codegraph @

lib/ansible/modules/add_host.py

raw
1 # -*- mode: python -*-
2
3 # Copyright: (c) 2012, Seth Vidal (@skvidal)
4 # Copyright: Ansible Team
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import annotations
8
9
10 DOCUMENTATION = r"""
11 ---
12 module: add_host
13 short_description: Add a host (and alternatively a group) to the ansible-playbook in-memory inventory
14 description:
15 - Use variables to create new hosts and groups in inventory for use in later plays of the same playbook.
16 - Takes variables so you can define the new hosts more fully.
17 - This module is also supported for Windows targets.
18 version_added: "0.9"
19 options:
20 name:
21 description:
22 - The hostname/ip of the host to add to the inventory, can include a colon and a port number.
23 type: str
24 required: true
25 aliases: [ host, hostname ]
26 groups:
27 description:
28 - The groups to add the hostname to.
29 type: list
30 elements: str
31 aliases: [ group, groupname ]
32 extends_documentation_fragment:
33 - action_common_attributes
34 - action_common_attributes.conn
35 - action_common_attributes.flow
36 - action_core
37 attributes:
38 action:
39 support: full
40 core:
41 details: While parts of this action are implemented in core, other parts are still available as normal plugins and can be partially overridden
42 support: partial
43 become:
44 support: none
45 bypass_host_loop:
46 support: full
47 bypass_task_loop:
48 support: none
49 check_mode:
50 details: While this makes no changes to target systems the 'in memory' inventory will still be altered
51 support: partial
52 connection:
53 support: none
54 delegation:
55 support: none
56 diff_mode:
57 support: none
58 platform:
59 platforms: all
60 notes:
61 - The alias O(host) of the parameter O(name) is only available on Ansible 2.4 and newer.
62 - Since Ansible 2.4, the C(inventory_dir) variable is now set to V(None) instead of the 'global inventory source',
63 because you can now have multiple sources. An example was added that shows how to partially restore the previous behaviour.
64 - Though this module does not change the remote host, we do provide C(changed) status as it can be useful for those trying to track inventory changes.
65 - The hosts added will not bypass the C(--limit) from the command line, so both of those need to be in agreement to make them available as play targets.
66 They are still available from hostvars and for delegation as a normal part of the inventory.
67 seealso:
68 - module: ansible.builtin.group_by
69 author:
70 - Ansible Core Team
71 - Seth Vidal (@skvidal)
72 """
73
74 EXAMPLES = r"""
75 - name: Add host to group 'just_created' with variable foo=42
76 ansible.builtin.add_host:
77 name: '{{ ip_from_ec2 }}'
78 groups: just_created
79 foo: 42
80
81 - name: Add host to multiple groups
82 ansible.builtin.add_host:
83 hostname: '{{ new_ip }}'
84 groups:
85 - group1
86 - group2
87
88 - name: Add a host with a non-standard port local to your machines
89 ansible.builtin.add_host:
90 name: '{{ new_ip }}:{{ new_port }}'
91
92 - name: Add a host alias that we reach through a tunnel (Ansible 1.9 and older)
93 ansible.builtin.add_host:
94 hostname: '{{ new_ip }}'
95 ansible_ssh_host: '{{ inventory_hostname }}'
96 ansible_ssh_port: '{{ new_port }}'
97
98 - name: Add a host alias that we reach through a tunnel (Ansible 2.0 and newer)
99 ansible.builtin.add_host:
100 hostname: '{{ new_ip }}'
101 ansible_host: '{{ inventory_hostname }}'
102 ansible_port: '{{ new_port }}'
103
104 - name: Ensure inventory vars are set to the same value as the inventory_hostname has (close to pre Ansible 2.4 behaviour)
105 ansible.builtin.add_host:
106 hostname: charlie
107 inventory_dir: '{{ inventory_dir }}'
108
109 - name: Add all hosts running this playbook to the done group
110 ansible.builtin.add_host:
111 name: '{{ item }}'
112 groups: done
113 loop: "{{ ansible_play_hosts }}"
114 """