Skip to content

Commit

Permalink
Support opentofu and not just terraform
Browse files Browse the repository at this point in the history
Since the main GitHub project has been renamed into tfs_generator, and today
there are upstream's alternatives to terraform like opentofu, let's make the
role more general.
This commit changes the names of all the involved variables.
  • Loading branch information
rascasoft committed Mar 20, 2024
1 parent c8d7a97 commit 96f18ed
Show file tree
Hide file tree
Showing 25 changed files with 181 additions and 169 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ jobs:
- name: YAML files check
run: yamllint $(find . -name *.yml -or -name *.yaml)
- name: Ansible Linter check
run: ansible-lint tests/terraform_tfs_generator.yml
run: ansible-lint tests/tfs_generator.yml
- name: Ansible syntax check
run: ansible-playbook --syntax-check tests/terraform_tfs_generator.yml
run: ansible-playbook --syntax-check tests/tfs_generator.yml

test:
runs-on: ubuntu-latest
Expand All @@ -35,12 +35,12 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Ansible effective execution (should produce terraform/*.tf files)
run: ansible-playbook -i tests/inventory tests/terraform_tfs_generator.yml
- name: Check terraform/*.tf files existence
- name: Ansible effective execution (should produce tf/*.tf files)
run: ansible-playbook -i tests/inventory tests/tfs_generator.yml
- name: Check tf/*.tf files existence
run: for tf in cloud_init.tf mylabvmsgroup.vms.tf networks.tf pools.tf
provider.tf volumes.tf; do
echo "Checking tests/terraform/${tf}";
echo "Checking tests/tfs/${tf}";
test -f tests/terraform/${tf};
done

Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ venv*
*.swo
collections/*
!collections/requirements.yml
/tests/terraform*
tests/tf*
!tests/tfs_generator.yml
44 changes: 22 additions & 22 deletions Azure.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generate Terraform manifests for Azure provider
# Generate tf manifests for Azure provider

<img src="https://cdn-dynmedia-1.microsoft.com/is/content/microsoftcorp/azure-blue" alt="Azure" width="128px">

Expand All @@ -7,7 +7,7 @@
After defining the Azure provider and its version:

```yaml
terraform_cloud_provider: 'azure'
tf_cloud_provider: 'azure'
azure_provider_version: '2.40.0'
```
Expand Down Expand Up @@ -212,18 +212,18 @@ before running terraform commands.
you'll have already the proper exclusion in your .gitignore file):

```console
$ python3 -m venv --system-site-packages venv-terraform
$ python3 -m venv --system-site-packages venv-tf
...
$ source venv-terraform/bin/activate
(venv-terraform)$
$ source venv-tf/bin/activate
(venv-tf)$
```

- Install dependencies: once in your environment, you need some dependencies
(like ansible and azure-cli). We already provides a 'requirements.txt' file
with all the dependencies, so just run:

```console
(venv-terraform)$ pip install -r requirements.txt
(venv-tf)$ pip install -r requirements.txt
...
```

Expand All @@ -237,7 +237,7 @@ before running terraform commands.
simply execute this command:

```console
(venv-terraform)$ az login
(venv-tf)$ az login
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code XXXXXXXXX to authenticate.
```

Expand All @@ -257,7 +257,7 @@ before running terraform commands.
administrator. Once you obtained this, execute:

```console
(venv-terraform)$ az login --service-principal -u <user URL> -p <certificate pem> --tenant <tenant id>
(venv-tf)$ az login --service-principal -u <user URL> -p <certificate pem> --tenant <tenant id>
```

NOTE 2: with Terraform versions >=0.13.x and hashicorp/azurerm provider >=
Expand All @@ -273,17 +273,17 @@ Once you've compiled the inventory, you are ready to generate your Terraform
resource files by launching:

```console
(venv-terraform)$ ansible-playbook -v -i inventory/myenv/hosts tests/terraform_tfs_generator.yaml
(venv-tf)$ ansible-playbook -v -i inventory/myenv/hosts tests/tfs_generator.yaml
...
```

### Terraform directory structure

Ansible will generate a directory structure based on the *terraform_config_dir*
Ansible will generate a directory structure based on the *tf_config_dir*
variable. Here, you can find this subdirectories:

```console
terraform_config_dir/
tf_config_dir/
|
|- bin/ (contains the Terraform binary)
|- azure-init/ (used to initialize Azure for keeping the tfstate file)
Expand All @@ -292,17 +292,17 @@ terraform_config_dir/

### Prepare Azure for keeping the tfstate file

If it's the first time you use Terraform to provision Azure resources, and you
choosed to keep the Terraform tfstate on Azure, you need to prepare Azure to
store the file. In your ${terraform_config_dir}/azure-init/ directory you've
all the Terraform resources needed to do this.
If it's the first time you use tf cmd to provision Azure resources, and you
choosed to keep the tfstate file on Azure, you need to prepare Azure to
store the file. In your ${tf_config_dir}/azure-init/ directory you've
all the tf resources needed to do this.

```console
(venv-terraform)$ terraform/myenv/bin/terraform init terraform/myenv/azure-init
(venv-tf)$ tf/myenv/bin/terraform init tf/myenv/azure-init
...
(venv-terraform)$ terraform/myenv/bin/terraform plan terraform/myenv/azure-init
(venv-tf)$ tf/myenv/bin/terraform plan tf/myenv/azure-init
...
(venv-terraform)$ terraform/myenv/bin/terraform apply terraform/myenv/azure-init
(venv-tf)$ tf/myenv/bin/terraform apply tf/myenv/azure-init
...
```

Expand All @@ -312,13 +312,13 @@ file will be already on Azure, so you can skip this step.
### Create the Azure environment

Creating the environment will be a matter of just initializing and using the
`terraform/myenv/azure` manifests, as follows:
`tf/myenv/azure` manifests, as follows:

```console
(venv-terraform)$ terraform/myenv/bin/terraform init terraform/myenv/azure
(venv-tf)$ tf/myenv/bin/terraform init tf/myenv/azure
...
(venv-terraform)$ terraform/myenv/bin/terraform plan terraform/myenv/azure
(venv-tf)$ tf/myenv/bin/terraform plan tf/myenv/azure
...
(venv-terraform)$ terraform/myenv/bin/terraform apply terraform/myenv/azure
(venv-tf)$ tf/myenv/bin/terraform apply tf/myenv/azure
...
```
38 changes: 19 additions & 19 deletions Libvirt.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generate Terraform manifests for Libvirt provider
# Generate tf manifests for Libvirt provider

<img src="https://libvirt.org/logos/logo-square-128.png" alt="Libvirt" width="128px">

Expand All @@ -7,8 +7,8 @@
After defining the Libvirt provider and its version:

```yaml
terraform_cloud_provider: 'libvirt'
terraform_libvirt_provider_version: '0.7.1
tf_cloud_provider: 'libvirt'
tf_libvirt_provider_version: '0.7.1
```
There are several variables that can be defined to an environment deployed:
Expand All @@ -24,14 +24,14 @@ You will need a connection url (by default is local qemu system):
```yaml
# Libvirt connection uri
terraform_libvirt_uri: 'qemu:///system'
tf_libvirt_uri: 'qemu:///system'
```
The Ansible group of machines that will be part of this deployment:
```yaml
# Which groups of machines will be processed by the role
terraform_libvirt_vms_groups:
tf_libvirt_vms_groups:
- 'mylabvmsgroup'
```
Expand All @@ -40,7 +40,7 @@ set the `autostart` option to `false`):

```yaml
# Libvirt vNets configuration
terraform_libvirt_networks:
tf_libvirt_networks:
- name: 'mylabnetwork'
mode: 'nat'
addresses:
Expand All @@ -51,21 +51,21 @@ And finally pools, volumes and cloud-init specific configurations:

```yaml
# Libvirt pools
terraform_libvirt_pools:
tf_libvirt_pools:
- name: 'mylabpool'
type: 'dir'
path: '/lab'
# Libvirt volumes
terraform_libvirt_volumes:
tf_libvirt_volumes:
- volume_id: 'almalinux-8'
file: 'almalinux-8.qcow2'
pool: 'mylabpool'
source: 'https://repo.almalinux.org/almalinux/8/cloud/x86_64/images/AlmaLinux-8-GenericCloud-latest.x86_64.qcow2'
format: 'qcow2'
# Libvirt cloud inits
terraform_libvirt_cloud_inits:
tf_libvirt_cloud_inits:
- name: 'mylabcloudinit'
pool: 'mylabpool'
cfg:
Expand All @@ -89,7 +89,7 @@ created like this (check [tests/inventory/host_vars/myvm-1.yml](tests/inventory/
```yaml
---
terraform_libvirt:
tf_libvirt:
name: myvm-1
memory: 1024
vcpu: 1
Expand Down Expand Up @@ -145,9 +145,9 @@ Once you've compiled the inventory, you are ready to generate your Terraform
resource files by launching:

```console
> ansible-playbook -i tests/inventory tests/terraform_tfs_generator.yml
> ansible-playbook -i tests/inventory tests/tfs_generator.yml
PLAY [Create Terraform manifests using terraform_tfs_generator Ansible role] ******************************************************************************
PLAY [Create Terraform manifests using tfs_generator Ansible role] ******************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************************
ok: [localhost]
Expand All @@ -159,16 +159,16 @@ PLAY RECAP *********************************************************************
localhost : ok=15 changed=7 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
```

This will generate a `tests/terraform` directory containing all the generated
This will generate a `tests/tf` directory containing all the generated
manifests.

### Terraform directory structure

Ansible will generate a directory structure based on the `terraform_config_dir`
Ansible will generate a directory structure based on the `tf_config_dir`
variable. Here, you can find this subdirectories:

```console
tests/terraform
tests/tf
├── bin
│   └── terraform
├── cloud_init_mylabcloudinit.cfg
Expand All @@ -184,14 +184,14 @@ tests/terraform
└── volumes.tf
```

## Using Terraform binary to generate the environment
## Using tf cmd binary to generate the environment

if the user who executes the binary have the rights to
use libvirt, then the environment will be created with no pain, first
initializing Terraform:

```console
(venv-ansible) rasca@catastrofe [~/Git/mmul-it/terraform_tfs_generator]> tests/terraform/bin/terraform -chdir=tests/terraform init
(venv-ansible) rasca@catastrofe [~/Git/mmul-it/tfs_generator]> tests/tf/bin/terraform -chdir=tests/tf init
Initializing the backend...
Expand Down Expand Up @@ -226,7 +226,7 @@ commands will detect it and remind you to do so if necessary.
And then applying the manifests:

```console
> tests/terraform/bin/terraform -chdir=tests/terraform apply -auto-approve
> tests/tf/bin/terraform -chdir=tests/tf apply -auto-approve
libvirt_cloudinit_disk.mylabcloudinit: Creating...
libvirt_pool.mylabpool: Creating...
libvirt_network.mylabnetwork: Creating...
Expand Down Expand Up @@ -271,7 +271,7 @@ In the defaults example file cloud-init is configured to set password to
...
# Libvirt cloud inits
terraform_libvirt_cloud_inits:
tf_libvirt_cloud_inits:
- name: 'mylabcloudinit'
pool: 'mylabpool'
cfg:
Expand Down
51 changes: 28 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Terraform manifests generator Ansible role
# Tf manifests generator Ansible role

This role can be used to install Terraform binary and deploy Terraform manifest
files (`.tf`) starting from an Ansible inventory, to create a fully atomated and
This role can be used to install tf tool binary and deploy tf manifest files
(`.tf`) starting from an Ansible inventory, to create a fully atomated and
idempotent Infrastructure as Code scenario.

[![Lint and test the project](https://github.com/mmul-it/terraform_tfs_generator/actions/workflows/main.yml/badge.svg)](https://github.com/mmul-it/terraform_tfs_generator/actions/workflows/main.yml)
[![Ansible Galaxy](https://img.shields.io/badge/ansible--galaxy-terraform_tfs_generator-blue.svg)](https://galaxy.ansible.com/mmul/terraform_tfs_generator)
[![Lint and test the project](https://github.com/mmul-it/tfs_generator/actions/workflows/main.yml/badge.svg)](https://github.com/mmul-it/tfs_generator/actions/workflows/main.yml)
[![Ansible Galaxy](https://img.shields.io/badge/ansible--galaxy-tfs_generator-blue.svg)](https://galaxy.ansible.com/mmul/tfs_generator)

## Role Variables

Expand All @@ -15,25 +15,30 @@ these:
```yaml
---

# Install terraform
terraform_binary_install: true
terraform_binary_version: '0.14.2'
terraform_binary_platform: 'linux_amd64'
# Install tf cmd binary
tf_binary_install: true
tf_binary_version: '0.14.2'
tf_binary_platform: 'linux_amd64'
tf_binary_url: "https://releases.hashicorp.com/terraform/{{ tf_binary_version }}/\
terraform_{{ tf_binary_version }}_{{ tf_binary_platform }}.zip"

# Where to deploy terraform resource files
terraform_config_dir: 'terraform'
# Name of the command used to generate tf files
tf_cmd: 'terraform'

# Where to deploy tf resource files
tf_config_dir: 'tfs'

# Delete existing resources before deploying new ones
terraform_purge: false
tf_purge: false
```
To start working with this role you'll need to choose which provider will be
used to generate Terraform manifests, currently `libvirt` (default) and `azure`
are the two supporterd providers.
used to generate tf manifests, currently `libvirt` (default) and `azure` are
the two supporterd providers.

```yaml
# Which environment we're going to deploy
terraform_cloud_provider: 'libvirt'
tf_cloud_provider: 'libvirt'
```

To get specific configuration options look at the [Libvirt](Libvirt.md) or
Expand All @@ -44,14 +49,14 @@ To get specific configuration options look at the [Libvirt](Libvirt.md) or

## Example playbook

To test this role and generate a set of Terraform manifests, just use the
[tests/terraform_tfs_generator.yml](tests/terraform_tfs_generator.yml)
playbook passing the test inventory with `-i tests/inventory`:
To test this role and generate a set of tf manifests, just use the
[tests/tfs_generator.yml](tests/tfs_generator.yml) playbook passing the test
inventory with `-i tests/inventory`:

```console
> ansible-playbook -i tests/inventory tests/terraform_tfs_generator.yml
> ansible-playbook -i tests/inventory tests/tfs_generator.yml
PLAY [Create Terraform manifests using terraform_tfs_generator Ansible role] ******************************************************************************
PLAY [Create tf manifests using tfs_generator Ansible role] ******************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************************
ok: [localhost]
Expand All @@ -63,11 +68,11 @@ PLAY RECAP *********************************************************************
localhost : ok=15 changed=7 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
```

This will generate a `tests/terraform` directory containing all the generated
This will generate a `tests/tf` directory containing all the generated
manifests.

To learn how to use this with Terraform to automate environment generation check
the [Libvirt](Libvirt.md) page.
To learn how to use this with the tf tool to automate environment generation
check the [Libvirt](Libvirt.md) page.

## License

Expand Down
Loading

0 comments on commit 96f18ed

Please sign in to comment.