This repository was archived by the owner on Oct 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnsible.pm
More file actions
191 lines (124 loc) · 4.64 KB
/
Copy pathAnsible.pm
File metadata and controls
191 lines (124 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package TorrustDeploy::Ansible;
use v5.38;
use Path::Tiny qw(path);
=head1 NAME
TorrustDeploy::Ansible - Ansible command wrapper for Torrust deployment
=head1 DESCRIPTION
This package provides methods to interact with Ansible for post-provision
verification and configuration. It handles copying templates, inventory
generation, and running playbooks.
=head1 METHODS
=cut
=head2 new
Create a new Ansible instance.
=cut
sub new {
my ($class) = @_;
return bless {}, $class;
}
=head2 copy_templates_and_generate_inventory
Copy Ansible templates to working directory and generate inventory with VM IP.
$ansible->copy_templates_and_generate_inventory($vm_ip, $ansible_dir);
=cut
sub copy_templates_and_generate_inventory {
my ($self, $vm_ip, $ansible_dir) = @_;
say "Setting up Ansible configuration...";
# Ensure ansible directory exists
$ansible_dir->mkpath unless $ansible_dir->exists;
my $templates_dir = path('templates/ansible');
# Check if templates directory exists
unless ($templates_dir->exists) {
die "Ansible templates directory not found: $templates_dir";
}
# Copy ansible.cfg
my $ansible_cfg_template = $templates_dir->child('ansible.cfg');
my $ansible_cfg_dest = $ansible_dir->child('ansible.cfg');
unless ($ansible_cfg_template->exists) {
die "Ansible config template not found: $ansible_cfg_template";
}
$ansible_cfg_template->copy($ansible_cfg_dest);
say "Copied: $ansible_cfg_template -> $ansible_cfg_dest";
# Copy all playbooks
my @playbooks = (
'wait-for-cloud-init.yml',
'post-provision-verification.yml',
'restart-vm.yml'
);
for my $playbook (@playbooks) {
my $template = $templates_dir->child($playbook);
my $dest = $ansible_dir->child($playbook);
unless ($template->exists) {
die "Playbook template not found: $template";
}
$template->copy($dest);
say "Copied: $template -> $dest";
}
# Generate inventory with VM IP
my $inventory_template = $templates_dir->child('inventory.ini.template');
my $inventory_dest = $ansible_dir->child('inventory.ini');
unless ($inventory_template->exists) {
die "Inventory template not found: $inventory_template";
}
# Read template and replace VM_IP placeholder
my $inventory_content = $inventory_template->slurp_utf8;
$inventory_content =~ s/\{\{VM_IP\}\}/$vm_ip/g;
# Write the generated inventory
$inventory_dest->spew_utf8($inventory_content);
say "Generated inventory: $inventory_dest (VM IP: $vm_ip)";
say "Ansible setup completed successfully.";
}
=head2 wait_for_cloud_init
Wait for cloud-init completion using Ansible playbook.
$ansible->wait_for_cloud_init($ansible_dir);
=cut
sub wait_for_cloud_init {
my ($self, $ansible_dir) = @_;
say "⏳ Waiting for cloud-init completion using Ansible...";
STDOUT->flush();
# Change to ansible directory and run cloud-init wait playbook
my $result = system("cd '$ansible_dir' && ansible-playbook -i inventory.ini wait-for-cloud-init.yml");
if ($result != 0) {
die "Ansible cloud-init wait failed with exit code: $result";
}
say "✅ Cloud-init completion verified via Ansible!";
STDOUT->flush();
}
=head2 run_verification
Run the post-provision verification playbook.
$ansible->run_verification($ansible_dir);
=cut
sub run_verification {
my ($self, $ansible_dir) = @_;
say "🔍 Running Ansible post-provision verification...";
STDOUT->flush();
# Change to ansible directory and run playbook
my $result = system("cd '$ansible_dir' && ansible-playbook -i inventory.ini post-provision-verification.yml");
if ($result != 0) {
die "Ansible verification failed with exit code: $result";
}
say "✅ Ansible verification completed successfully!";
STDOUT->flush();
}
=head2 restart_vm
Restart the VM after post-provision verification.
$ansible->restart_vm($ansible_dir);
=cut
sub restart_vm {
my ($self, $ansible_dir) = @_;
say "🔄 Restarting VM using Ansible...";
STDOUT->flush();
# Change to ansible directory and run restart playbook
my $result = system("cd '$ansible_dir' && ansible-playbook -i inventory.ini restart-vm.yml");
if ($result != 0) {
die "Ansible VM restart failed with exit code: $result";
}
say "✅ VM restart completed successfully!";
STDOUT->flush();
}
1;
__END__
=head1 AUTHOR
Torrust Team
=head1 LICENSE
This software is licensed under the MIT License.
=cut