Debian Codegraph @

ansible_collections/community/general/docs/docsite/rst/guide_iocage_inventory_hooks.rst

raw
1 ..
2 Copyright (c) Ansible Project
3 GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 SPDX-License-Identifier: GPL-3.0-or-later
5
6 .. _ansible_collections.community.general.docsite.guide_iocage.guide_iocage_inventory.guide_iocage_inventory_hooks:
7
8 Hooks
9 -----
10
11 The iocage utility internally opens a console to a jail to get the jail's DHCP address. This
12 requires root. If you run the command ``iocage list -l`` as unprivileged user, you'll see the
13 message ``DHCP (running -- address requires root)``. If you are not granted the root privilege, use
14 ``/etc/dhclient-exit-hooks``. For example, in the jail *srv_1*, create the file
15 ``/zroot/iocage/jails/srv_1/root/etc/dhclient-exit-hooks``
16
17 .. code-block:: shell
18
19 case "$reason" in
20 "BOUND"|"REBIND"|"REBOOT"|"RENEW")
21 echo $new_ip_address > /var/db/dhclient-hook.address.$interface
22 ;;
23 esac
24
25 where ``/zroot/iocage`` is the activated pool.
26
27 .. code-block:: console
28
29 shell> zfs list | grep /zroot/iocage
30 zroot/iocage 4.69G 446G 5.08M /zroot/iocage
31 zroot/iocage/download 927M 446G 384K /zroot/iocage/download
32 zroot/iocage/download/14.1-RELEASE 465M 446G 465M /zroot/iocage/download/14.1-RELEASE
33 zroot/iocage/download/14.2-RELEASE 462M 446G 462M /zroot/iocage/download/14.2-RELEASE
34 zroot/iocage/images 384K 446G 384K /zroot/iocage/images
35 zroot/iocage/jails 189M 446G 480K /zroot/iocage/jails
36 zroot/iocage/jails/srv_1 62.9M 446G 464K /zroot/iocage/jails/srv_1
37 zroot/iocage/jails/srv_1/root 62.4M 446G 3.53G /zroot/iocage/jails/srv_1/root
38 zroot/iocage/jails/srv_2 62.8M 446G 464K /zroot/iocage/jails/srv_2
39 zroot/iocage/jails/srv_2/root 62.3M 446G 3.53G /zroot/iocage/jails/srv_2/root
40 zroot/iocage/jails/srv_3 62.8M 446G 464K /zroot/iocage/jails/srv_3
41 zroot/iocage/jails/srv_3/root 62.3M 446G 3.53G /zroot/iocage/jails/srv_3/root
42 zroot/iocage/log 688K 446G 688K /zroot/iocage/log
43 zroot/iocage/releases 2.93G 446G 384K /zroot/iocage/releases
44 zroot/iocage/releases/14.2-RELEASE 2.93G 446G 384K /zroot/iocage/releases/14.2-RELEASE
45 zroot/iocage/releases/14.2-RELEASE/root 2.93G 446G 2.88G /zroot/iocage/releases/14.2-RELEASE/root
46 zroot/iocage/templates 682M 446G 416K /zroot/iocage/templates
47 zroot/iocage/templates/ansible_client 681M 446G 432K /zroot/iocage/templates/ansible_client
48 zroot/iocage/templates/ansible_client/root 681M 446G 3.53G /zroot/iocage/templates/ansible_client/root
49
50 See: `man dhclient-script <https://man.freebsd.org/cgi/man.cgi?dhclient-script>`_
51
52 Create the inventory configuration. Use the option :ansopt:`community.general.iocage#inventory:hooks_results` instead of :ansopt:`community.general.iocage#inventory:sudo`:
53
54 .. code-block:: console
55
56 shell> cat hosts/02_iocage.yml
57
58 .. code-block:: yaml
59
60 plugin: community.general.iocage
61 host: 10.1.0.73
62 user: admin
63 hooks_results:
64 - /var/db/dhclient-hook.address.epair0b
65
66 .. note::
67
68 The option :ansopt:`community.general.iocage#inventory:hooks_results` expects the poolname to be mounted to ``/poolname``. For example, if you
69 activate the pool iocage, this plugin expects to find the :ansopt:`community.general.iocage#inventory:hooks_results` items in the path
70 /iocage/iocage/jails/<name>/root. If you mount the poolname to a different path, the easiest
71 remedy is to create a symlink.
72
73 As admin at the controller, display the inventory:
74
75 .. code-block:: console
76
77 shell> ansible-inventory -i hosts/02_iocage.yml --list --yaml
78 all:
79 children:
80 ungrouped:
81 hosts:
82 srv_1:
83 iocage_basejail: 'no'
84 iocage_boot: 'off'
85 iocage_hooks:
86 - 10.1.0.183
87 iocage_ip4: '-'
88 iocage_ip4_dict:
89 ip4: []
90 msg: DHCP (running -- address requires root)
91 iocage_ip6: '-'
92 iocage_jid: '204'
93 iocage_release: 14.2-RELEASE-p3
94 iocage_state: up
95 iocage_template: ansible_client
96 iocage_type: jail
97 srv_2:
98 iocage_basejail: 'no'
99 iocage_boot: 'off'
100 iocage_hooks:
101 - 10.1.0.204
102 iocage_ip4: '-'
103 iocage_ip4_dict:
104 ip4: []
105 msg: DHCP (running -- address requires root)
106 iocage_ip6: '-'
107 iocage_jid: '205'
108 iocage_release: 14.2-RELEASE-p3
109 iocage_state: up
110 iocage_template: ansible_client
111 iocage_type: jail
112 srv_3:
113 iocage_basejail: 'no'
114 iocage_boot: 'off'
115 iocage_hooks:
116 - 10.1.0.169
117 iocage_ip4: '-'
118 iocage_ip4_dict:
119 ip4: []
120 msg: DHCP (running -- address requires root)
121 iocage_ip6: '-'
122 iocage_jid: '206'
123 iocage_release: 14.2-RELEASE-p3
124 iocage_state: up
125 iocage_template: ansible_client
126 iocage_type: jail
127
128 Compose the variable ``ansible_host``. For example, ``hosts/02_iocage.yml`` could look like:
129
130 .. code-block:: yaml+jinja
131
132 plugin: community.general.iocage
133 host: 10.1.0.73
134 user: admin
135 hooks_results:
136 - /var/db/dhclient-hook.address.epair0b
137 compose:
138 ansible_host: (iocage_hooks.0 == '-') | ternary(iocage_ip4, iocage_hooks.0)
139
140 Test the jails. Create a playbook ``pb-test-uname.yml``:
141
142 .. code-block:: yaml
143
144 - hosts: all
145 remote_user: admin
146
147 vars:
148
149 ansible_python_interpreter: auto_silent
150
151 tasks:
152
153 - command: uname -a
154 register: out
155
156 - debug:
157 var: out.stdout
158
159 See: :ref:`working_with_bsd`
160
161 Run the playbook:
162
163 .. code-block:: console
164
165 shell> ansible-playbook -i hosts/02_iocage.yml pb-test-uname.yml
166
167 PLAY [all] **********************************************************************************************************
168
169 TASK [command] ******************************************************************************************************
170 changed: [srv_3]
171 changed: [srv_1]
172 changed: [srv_2]
173
174 TASK [debug] ********************************************************************************************************
175 ok: [srv_1] =>
176 out.stdout: FreeBSD srv-1 14.2-RELEASE-p1 FreeBSD 14.2-RELEASE-p1 GENERIC amd64
177 ok: [srv_3] =>
178 out.stdout: FreeBSD srv-3 14.2-RELEASE-p1 FreeBSD 14.2-RELEASE-p1 GENERIC amd64
179 ok: [srv_2] =>
180 out.stdout: FreeBSD srv-2 14.2-RELEASE-p1 FreeBSD 14.2-RELEASE-p1 GENERIC amd64
181
182 PLAY RECAP **********************************************************************************************************
183 srv_1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
184 srv_2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
185 srv_3 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
186
187 Note: This playbook and the inventory configuration works also for the *Shared IP Jails*.