Separazione del codice di Ansible in modulo a parte.
This commit is contained in:
parent
a56801c04f
commit
d1af10892d
@ -6,7 +6,7 @@ import pulumi_command as command
|
||||
import pulumiverse_time as time
|
||||
import additional_configs as add
|
||||
from jinja2 import Template
|
||||
from passlib.hash import sha512_crypt
|
||||
import ansible
|
||||
|
||||
config = pulumi.Config()
|
||||
|
||||
@ -66,27 +66,8 @@ wait_time = time.Sleep("wait_time", create_duration="30s",
|
||||
opts = pulumi.ResourceOptions(depends_on = [ vm ])
|
||||
)
|
||||
|
||||
# Creating the inventory file
|
||||
inventory = command.local.Command("a-inventory",
|
||||
create = vm.network_interfaces[0]['addresses'][0].apply(
|
||||
lambda ipaddr: f"echo '{env['name']} ansible_host={ipaddr} ansible_user={env['username']}' >./inventory"
|
||||
),
|
||||
delete = "rm -f ./inventory",
|
||||
opts = pulumi.ResourceOptions(depends_on = [ wait_time ])
|
||||
)
|
||||
|
||||
deploy = command.local.Command('ap deploy',
|
||||
create = 'ansible-playbook deploy.yml',
|
||||
delete = 'rm -f ./ansible.log',
|
||||
opts = pulumi.ResourceOptions(depends_on = [ inventory ])
|
||||
)
|
||||
|
||||
# Running additional playbooks
|
||||
for playbook in sorted(config.get('playbooks').split(',')):
|
||||
execute_ansible = command.local.Command(f"ap {playbook.split('.')[0]}",
|
||||
create = f"ansible-playbook {playbook}",
|
||||
delete = "rm -f ./ansible.log",
|
||||
opts = pulumi.ResourceOptions(depends_on = [ deploy ])
|
||||
)
|
||||
adeploy = ansible.AnsibleDeployment(vm, env, config.get('playbooks'))
|
||||
adeploy.create_inventory([ wait_time ])
|
||||
adeploy.run_playbooks()
|
||||
|
||||
pulumi.export("ip", vm.network_interfaces[0]['addresses'][0])
|
||||
|
59
vm-ansible/ansible.py
Normal file
59
vm-ansible/ansible.py
Normal file
@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env python3
|
||||
# encoding: utf-8
|
||||
|
||||
import pulumi
|
||||
from pulumi_command.local import Command
|
||||
|
||||
class AnsibleDeployment():
|
||||
|
||||
def __init__(self, virtual_machine, environment, additionals = None, deploy = None):
|
||||
"""Creates an ansible deployment and attaches resources."""
|
||||
|
||||
self.inventory = 'inventory'
|
||||
self.virtual_machine = virtual_machine
|
||||
self.environment = environment
|
||||
self.deploy = str(deploy) or 'deploy.yml'
|
||||
self.additionals = str(additionals) or ''
|
||||
|
||||
def create_inventory(self, dependencies = []):
|
||||
"""Returns a basic inventory from a virtual_machine object."""
|
||||
deps = []
|
||||
if len(dependencies) == 0:
|
||||
deps.append(self.virtual_machine)
|
||||
else:
|
||||
deps.extend(dependencies)
|
||||
|
||||
self.inventory = Command("a inventory",
|
||||
create = self.virtual_machine.network_interfaces[0]['addresses'][0].apply(
|
||||
lambda ipaddr: f"echo '{self.environment['name']} ansible_host={ipaddr} ansible_user={self.environment['username']}' >./inventory"
|
||||
),
|
||||
delete = "rm -f ./inventory",
|
||||
opts = pulumi.ResourceOptions(depends_on = dependencies)
|
||||
)
|
||||
|
||||
def run_playbooks(self):
|
||||
"""Execute the rest of the playbooks."""
|
||||
|
||||
self.deploy_resource = Command("ap deploy",
|
||||
create = "ansible-playbook deploy.yml",
|
||||
delete = "rm -f ansible.log",
|
||||
opts = pulumi.ResourceOptions(depends_on = [ self.inventory ])
|
||||
)
|
||||
|
||||
# Running additionals if present
|
||||
if len(self.additionals) != 0:
|
||||
|
||||
for playbook in sorted(self.additionals.split(',')):
|
||||
name = playbook.split('.')[0]
|
||||
Command(f"ap {name}",
|
||||
create = f"ansible-playbook {playbook}",
|
||||
delete = "rm -f ansible.log",
|
||||
opts = pulumi.ResourceOptions(depends_on = [ self.inventory, self.deploy_resource ])
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user