Skip to content

Commit 81e4313

Browse files
committed
Add PvC infra provision role
Signed-off-by: Jim Enright <[email protected]>
1 parent df6646c commit 81e4313

File tree

14 files changed

+1009
-0
lines changed

14 files changed

+1009
-0
lines changed

roles/provision/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Provision
2+
3+
A role that provisions Cloudera-specific inventory.
4+
5+
The role requires the following two files that are locatable by the enclosing play:
6+
7+
* *hostvars.j2* - a Jinja macro that outputs a host's variables in a static inventory file
8+
* *instance_vars.j2* - a Jinja macro that outputs an instance's metadata, i.e. tags, in the provider
9+
10+
These two Jinja macros _expect variables on the host_ which are assigned via the `add_host` call
11+
within the role. To set these variables, use the `module_defaults` assignment within the enclosing
12+
play of the role.
13+
14+
## Examples
15+
16+
### module_defaults
17+
18+
The `node` variable is in scope of the `add_host` module and contains the output of the Terraform
19+
node provisioning configuration.
20+
21+
```yaml
22+
- name: Provision resources
23+
hosts: localhost
24+
connection: local
25+
gather_facts: no
26+
module_defaults:
27+
ansible.builtin.add_host:
28+
groups: "{{ node.groups | default(omit) }}"
29+
host_template: "{{ node.metadata.host_template | default(omit) }}"
30+
storage_volumes: "{{ node.storage_volumes | default([]) }}"
31+
tls: "{{ node.metadata.tls | default(omit) }}"
32+
tasks: ...
33+
```
34+
35+
### hostvars.j2
36+
37+
```jinja
38+
{# Collect and output individual host variables #}
39+
{% macro host_variables(host) %}
40+
{% set fields = [] %}
41+
{% set _ = fields.append("ansible_user=" + host['ansible_user']) if 'ansible_user' in host %}
42+
{% set _ = fields.append("host_template=" + host['host_template']) if 'host_template' in host %}
43+
{% set _ = fields.append("label=" + host['label']) if 'label' in host %}
44+
{% set _ = fields.append("tls=" + host['tls'] | string) if 'tls' in host %}
45+
{{ host['inventory_hostname'] }} {{ fields | join(' ') }}
46+
{%- endmacro %}
47+
```
48+
49+
### instance_vars.j2
50+
51+
```jinja
52+
{# Define the metadata tags for the individual Openstack instances #}
53+
{# Output should be TF map _entries_, not a map itself #}
54+
55+
{% macro instance_tags(host) %}
56+
{% set tags = {} %}
57+
{% set _ = tags.update({ 'ansible_user': host.ansible_user }) if host.ansible_user is defined %}
58+
{% set _ = tags.update({ 'host_template': host.host_template }) if host.host_template is defined %}
59+
{% set _ = tags.update({ 'groups': host.groups | join(', ') }) if host.groups is defined %}
60+
{% set _ = tags.update({ 'tls': host.tls | string }) if host.tls is defined %}
61+
{% for k, v in tags.items() %}
62+
{{ k }} = "{{ v }}"{{ "," if not loop.last else "" }}
63+
{% endfor %}
64+
{%- endmacro %}
65+
```

roles/provision/defaults/main.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
provision_state: present # absent
3+
provision_provider: aws # aws, etc.
4+
provision_directory: tf_deployment
5+
6+
provision_inventory_file: "{{ undef(hint='Static inventory file') }}" # inventory_static.ini
7+
8+
# provision_terraform_parallelism:
9+
provision_state_storage: local # remote_s3
10+
# provision_remote_storage_s3_region:
11+
# provision_remote_storage_s3_bucket:
12+
provision_create_remote_storage: False
13+
14+
provision_name_prefix: "{{ undef(hint='Deployment name prefix') }}"
15+
provision_domain_suffix: "{{ undef(hint='DNS domain suffix') }}"
16+
provision_ssh_keypair_label: "{{ undef(hint='SSH keypair label') }}"
17+
provision_ssh_keypair_public_key: "{{ undef(hint='SSH keypair public key text') }}"
18+
provision_owner_email: "{{ undef(hint='Resource owner email') }}"
19+
provision_tags: {}
20+
21+
provision_aws_ec2_region: "{{ undef(hint='AWS EC2 region') }}"
22+
#provision_aws_ec2_default_ami_filters: "{{ undef(hint='AWS EC2 filters for default AMI') }}"
23+
#provision_aws_ec2_default_ami_owners: "{{ undef(hint='AWS EC2 AMI owner filter') }}"
24+
#provision_aws_ec2_vpc_name:
25+
provision_aws_ec2_vpc_enable_dns_support: true
26+
provision_aws_ec2_vpc_enable_dns_hostnames: true
27+
#provision_aws_ec2_public_subnets:
28+
#provision_aws_ec2_private_subnets:
29+
30+
#provision_default_instance_user:
31+
provision_instances: "{{ undef(hint='Instance definitions') }}"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
variable "ssh_keypair_name" {
2+
type = string
3+
description = "AWS SSH key pair name"
4+
validation {
5+
condition = length(var.ssh_keypair_name) > 4
6+
error_message = "The SSH key pair name must be greater than 4 characters."
7+
}
8+
}
9+
10+
variable "ssh_keypair_public_key_text" {
11+
type = string
12+
description = "AWS SSH key pair public key text"
13+
validation {
14+
condition = length(var.ssh_keypair_public_key_text) > 0
15+
error_message = "The SSH key pair public key text must not be empty."
16+
}
17+
}
18+
19+
resource "aws_key_pair" "deployment_keypair" {
20+
key_name = var.ssh_keypair_name
21+
public_key = var.ssh_keypair_public_key_text
22+
}
23+
24+
output "ssh_keypair" {
25+
value = {
26+
name = aws_key_pair.deployment_keypair.key_name
27+
public_key = var.ssh_keypair_public_key_text
28+
fingerprint = aws_key_pair.deployment_keypair.fingerprint
29+
}
30+
description = "Deployment SSH keypair"
31+
}

0 commit comments

Comments
 (0)