From 96f18edb9f2778bdad477573d5ea8d8856611b94 Mon Sep 17 00:00:00 2001 From: Raoul Scarazzini Date: Wed, 20 Mar 2024 15:59:59 +0100 Subject: [PATCH] Support opentofu and not just terraform 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. --- .github/workflows/main.yml | 12 +++---- .gitignore | 3 +- Azure.md | 44 +++++++++++------------ Libvirt.md | 38 ++++++++++---------- README.md | 51 +++++++++++++++------------ defaults/main.yml | 37 ++++++++++--------- meta/main.yml | 10 +++--- tasks/azure-vms.yml | 6 ++-- tasks/azure.yml | 42 +++++++++++----------- tasks/install.yml | 23 ++++++------ tasks/libvirt-vms.yml | 4 +-- tasks/libvirt.yml | 26 +++++++------- tasks/main.yml | 12 +++---- templates/azure/provider.tf.j2 | 6 ++-- templates/libvirt/cloud_init.tf.j2 | 4 +-- templates/libvirt/networks.tf.j2 | 4 +-- templates/libvirt/pools.tf.j2 | 2 +- templates/libvirt/provider.tf.j2 | 2 +- templates/libvirt/variables.source.j2 | 2 +- templates/libvirt/vms.tf.j2 | 4 +-- templates/libvirt/volumes.tf.j2 | 2 +- tests/inventory/host_vars/myvm-1.yml | 2 +- tests/inventory/host_vars/myvm-2.yml | 2 +- tests/terraform_tfs_generator.yml | 6 ---- tests/tfs_generator.yml | 6 ++++ 25 files changed, 181 insertions(+), 169 deletions(-) delete mode 100644 tests/terraform_tfs_generator.yml create mode 100644 tests/tfs_generator.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 36394bc..06dc177 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 @@ -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 diff --git a/.gitignore b/.gitignore index 32d9468..77e081e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ venv* *.swo collections/* !collections/requirements.yml -/tests/terraform* +tests/tf* +!tests/tfs_generator.yml diff --git a/Azure.md b/Azure.md index f7a7347..0fffc6e 100644 --- a/Azure.md +++ b/Azure.md @@ -1,4 +1,4 @@ -# Generate Terraform manifests for Azure provider +# Generate tf manifests for Azure provider Azure @@ -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' ``` @@ -212,10 +212,10 @@ 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 @@ -223,7 +223,7 @@ before running terraform commands. with all the dependencies, so just run: ```console - (venv-terraform)$ pip install -r requirements.txt + (venv-tf)$ pip install -r requirements.txt ... ``` @@ -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. ``` @@ -257,7 +257,7 @@ before running terraform commands. administrator. Once you obtained this, execute: ```console - (venv-terraform)$ az login --service-principal -u -p --tenant + (venv-tf)$ az login --service-principal -u -p --tenant ``` NOTE 2: with Terraform versions >=0.13.x and hashicorp/azurerm provider >= @@ -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) @@ -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 ... ``` @@ -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 ... ``` diff --git a/Libvirt.md b/Libvirt.md index 5abfbdb..4164685 100644 --- a/Libvirt.md +++ b/Libvirt.md @@ -1,4 +1,4 @@ -# Generate Terraform manifests for Libvirt provider +# Generate tf manifests for Libvirt provider Libvirt @@ -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: @@ -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' ``` @@ -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: @@ -51,13 +51,13 @@ 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' @@ -65,7 +65,7 @@ terraform_libvirt_volumes: format: 'qcow2' # Libvirt cloud inits -terraform_libvirt_cloud_inits: +tf_libvirt_cloud_inits: - name: 'mylabcloudinit' pool: 'mylabpool' cfg: @@ -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 @@ -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] @@ -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 @@ -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... @@ -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... @@ -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: diff --git a/README.md b/README.md index b168904..720b218 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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] @@ -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 diff --git a/defaults/main.yml b/defaults/main.yml index 3908224..aafefce 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,47 +1,50 @@ --- -# Install terraform -terraform_binary_install: true -terraform_binary_version: '0.14.2' -terraform_binary_platform: 'linux_amd64' -terraform_binary_url: "https://releases.hashicorp.com/terraform/{{ terraform_binary_version }}/\ - terraform_{{ terraform_binary_version }}_{{ terraform_binary_platform }}.zip" +# Install tf cmd +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 # Which environment we're going to deploy -terraform_cloud_provider: 'libvirt' +tf_cloud_provider: 'libvirt' # Libvirt defaults -terraform_libvirt_provider_version: '0.7.1' +tf_libvirt_provider_version: '0.7.1' # Libvirt connection uri -terraform_libvirt_uri: 'qemu:///system' +tf_libvirt_uri: 'qemu:///system' # Which groups of machines will be processed by the role -terraform_libvirt_vms_groups: +tf_libvirt_vms_groups: - 'mylabvmsgroup' # Libvirt vNets configuration -terraform_libvirt_networks: +tf_libvirt_networks: - name: 'mylabnetwork' mode: 'nat' addresses: - '192.168.199.0/24' # 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' @@ -49,7 +52,7 @@ terraform_libvirt_volumes: format: 'qcow2' # Libvirt cloud inits -terraform_libvirt_cloud_inits: +tf_libvirt_cloud_inits: - name: 'mylabcloudinit' pool: 'mylabpool' cfg: diff --git a/meta/main.yml b/meta/main.yml index e677e60..733ef5f 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,13 +1,13 @@ --- galaxy_info: - role_name: terraform_tfs_generator + role_name: tfs_generator namespace: mmul author: Raoul Scarazzini (rasca@mmul.it) description: | - 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 idempotent Infrastructure as Code scenario. + This role can be used to install tf binary tool and deploy tf manifest + files (.tf) starting from an Ansible inventory, to create a fully atomated + and idempotent Infrastructure as Code scenario. license: license (MIT) min_ansible_version: '2' platforms: @@ -24,7 +24,9 @@ galaxy_info: versions: - all galaxy_tags: + - tf - terraform + - opentofu - manifests dependencies: [] diff --git a/tasks/azure-vms.yml b/tasks/azure-vms.yml index bbbd1d0..7aacf75 100644 --- a/tasks/azure-vms.yml +++ b/tasks/azure-vms.yml @@ -6,6 +6,6 @@ dest: "{{ item.dest }}" mode: '0640' with_items: - - {src: "azure/vms.tf.j2", dest: "{{ terraform_config_dir }}/azure/{{ group_name }}.vms.tf"} - - {src: "azure/disks.tf.j2", dest: "{{ terraform_config_dir }}/azure/{{ group_name }}.disks.tf"} - - {src: "azure/nics.tf.j2", dest: "{{ terraform_config_dir }}/azure/{{ group_name }}.nics.tf"} + - {src: "azure/vms.tf.j2", dest: "{{ tf_config_dir }}/azure/{{ group_name }}.vms.tf"} + - {src: "azure/disks.tf.j2", dest: "{{ tf_config_dir }}/azure/{{ group_name }}.disks.tf"} + - {src: "azure/nics.tf.j2", dest: "{{ tf_config_dir }}/azure/{{ group_name }}.nics.tf"} diff --git a/tasks/azure.yml b/tasks/azure.yml index e28e696..50ce2b3 100644 --- a/tasks/azure.yml +++ b/tasks/azure.yml @@ -2,20 +2,20 @@ - name: Create Azure private output directory ansible.builtin.file: - path: "{{ terraform_config_dir }}/azure-private" + path: "{{ tf_config_dir }}/azure-private" state: directory mode: '0700' - name: Deploy Azure private variables source templates ansible.builtin.template: src: "azure/variables.source.j2" - dest: "{{ terraform_config_dir }}/azure-private/variables.source" + dest: "{{ tf_config_dir }}/azure-private/variables.source" mode: '0600' - name: Generate ssh RSA public/private key ansible.builtin.command: cmd: ssh-keygen -t rsa -N '' -f azure_id_rsa - chdir: "{{ terraform_config_dir }}/azure-private" + chdir: "{{ tf_config_dir }}/azure-private" creates: azure_id_rsa.pub when: - not azure_vm_admin_ssh_public_key_file is defined @@ -23,7 +23,7 @@ - name: Set the ssh RSA public key fact ansible.builtin.set_fact: - azure_vm_admin_ssh_public_key_file: "{{ terraform_config_dir }}/azure-private/azure_id_rsa.pub" + azure_vm_admin_ssh_public_key_file: "{{ tf_config_dir }}/azure-private/azure_id_rsa.pub" when: - not azure_vm_admin_ssh_public_key_file is defined @@ -31,49 +31,49 @@ block: - name: Purge azure-init output directory ansible.builtin.file: - path: "{{ terraform_config_dir }}/azure-init" + path: "{{ tf_config_dir }}/azure-init" state: absent when: - - terraform_purge | bool + - tf_purge | bool - name: Create azure-init output directory ansible.builtin.file: - path: "{{ terraform_config_dir }}/azure-init" + path: "{{ tf_config_dir }}/azure-init" state: directory mode: '0750' - name: Deploy resources templates ansible.builtin.template: src: "azure/{{ item }}.tf.j2" - dest: "{{ terraform_config_dir }}/azure-init/{{ item }}.tf" + dest: "{{ tf_config_dir }}/azure-init/{{ item }}.tf" mode: '0640' with_items: - "{{ azure_tfstate_resources }}" vars: skip_azure_backend: true azure_resource_group_name: "{{ azure_tfstate_resource_group_name }}" - azure_storage_account_name: "{{ terraform_tfstate_on_azure_storage_container }}" - azure_storage_container_name: "{{ terraform_tfstate_on_azure_storage_container }}" + azure_storage_account_name: "{{ tf_tfstate_on_azure_storage_container }}" + azure_storage_container_name: "{{ tf_tfstate_on_azure_storage_container }}" when: - - terraform_tfstate_on_azure_storage_container is defined + - tf_tfstate_on_azure_storage_container is defined - name: Purge azure output directory ansible.builtin.file: - path: "{{ terraform_config_dir }}/azure" + path: "{{ tf_config_dir }}/azure" state: absent when: - - terraform_purge | bool + - tf_purge | bool - name: Create Azure output directory ansible.builtin.file: - path: "{{ terraform_config_dir }}/azure" + path: "{{ tf_config_dir }}/azure" state: directory mode: '0750' - name: Deploy Azure general resources templates ansible.builtin.template: src: "azure/{{ item }}.tf.j2" - dest: "{{ terraform_config_dir }}/azure/{{ item }}.tf" + dest: "{{ tf_config_dir }}/azure/{{ item }}.tf" mode: '0640' with_items: - "{{ azure_resources }}" @@ -81,7 +81,7 @@ - name: Deploy Azure VPN resources templates ansible.builtin.template: src: "azure/{{ item }}.tf.j2" - dest: "{{ terraform_config_dir }}/azure/{{ item }}.tf" + dest: "{{ tf_config_dir }}/azure/{{ item }}.tf" mode: '0640' with_items: - "{{ azure_vpn_resources }}" @@ -89,7 +89,7 @@ - name: Deploy Azure key vault template ansible.builtin.template: src: "azure/key_vault.tf.j2" - dest: "{{ terraform_config_dir }}/azure/key_vault.tf" + dest: "{{ tf_config_dir }}/azure/key_vault.tf" mode: '0640' when: - azure_key_vault_enable | bool @@ -97,7 +97,7 @@ - name: Deploy Azure database template ansible.builtin.template: src: "azure/database.tf.j2" - dest: "{{ terraform_config_dir }}/azure/database.tf" + dest: "{{ tf_config_dir }}/azure/database.tf" mode: '0640' when: - azure_database_servers is defined @@ -112,6 +112,6 @@ - name: Display a reminder for sourcing variables ansible.builtin.debug: msg: - - "Remember to import the variable file in order to tell Terraform which" - - "subscription use during the run. Before run Terraform, please execute:" - - " source {{ terraform_config_dir }}/azure-private/variables.source" + - "Remember to import the variable file in order to tell the tf cmd which" + - "subscription use during the run. Before run the tf cmd, please execute:" + - " source {{ tf_config_dir }}/azure-private/variables.source" diff --git a/tasks/install.yml b/tasks/install.yml index 840ca89..0cbf8f1 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -1,12 +1,12 @@ --- -- name: Check if terraform binary is available - ansible.builtin.command: "{{ terraform_config_dir }}/bin/terraform -version" - register: terraform_binary_check +- name: Check if tf binary is available + ansible.builtin.command: "{{ tf_config_dir }}/bin/{{ tf_cmd }} -version" + register: tf_binary_check changed_when: false failed_when: false -- name: Get terraform binary +- name: Get tf binary block: - name: Install unzip ansible.builtin.package: @@ -15,17 +15,18 @@ become: true when: ansible_os_family != "Darwin" - - name: "Create directory for terraform binary under {{ terraform_config_dir }}" + - name: "Create directory for tf binary under {{ tf_config_dir }}" ansible.builtin.file: - path: "{{ terraform_config_dir }}/bin" + path: "{{ tf_config_dir }}/bin" state: directory mode: '0750' - - name: Download and extract terraform binary + - name: Download and extract tf binary ansible.builtin.unarchive: - src: "{{ terraform_binary_url }}" - dest: "{{ terraform_config_dir }}/bin" + src: "{{ tf_binary_url }}" + dest: "{{ tf_config_dir }}/bin" remote_src: true when: - - terraform_binary_check.rc != 0 or - terraform_binary_check.stdout.splitlines()[0] != 'Terraform v' + terraform_binary_version + - tf_binary_check.rc != 0 or + not ( tf_binary_check.stdout | regex_search('Terraform v' + tf_binary_version) or + tf_binary_check.stdout | regex_search('OpenTofu v' + tf_binary_version) ) diff --git a/tasks/libvirt-vms.yml b/tasks/libvirt-vms.yml index 87db50e..c1780de 100644 --- a/tasks/libvirt-vms.yml +++ b/tasks/libvirt-vms.yml @@ -1,9 +1,9 @@ --- -- name: "Deploy VMS templates for group {{ terraform_group_name }}" +- name: "Deploy VMS templates for group {{ tf_group_name }}" ansible.builtin.template: src: "{{ item.src }}" dest: "{{ item.dest }}" mode: '0640' with_items: - - {src: "libvirt/vms.tf.j2", dest: "{{ terraform_config_dir }}/{{ terraform_group_name }}.vms.tf"} + - {src: "libvirt/vms.tf.j2", dest: "{{ tf_config_dir }}/{{ tf_group_name }}.vms.tf"} diff --git a/tasks/libvirt.yml b/tasks/libvirt.yml index 6a9342d..2518f46 100644 --- a/tasks/libvirt.yml +++ b/tasks/libvirt.yml @@ -2,33 +2,33 @@ - name: Purge Libvirt output directory ansible.builtin.file: - path: "{{ terraform_config_dir }}" + path: "{{ tf_config_dir }}" state: absent when: - - terraform_purge | bool + - tf_purge | bool - name: Create Libvirt output directory ansible.builtin.file: - path: "{{ terraform_config_dir }}" + path: "{{ tf_config_dir }}" state: directory mode: '0750' - name: Create Libvirt private output directory ansible.builtin.file: - path: "{{ terraform_config_dir }}/private" + path: "{{ tf_config_dir }}/private" state: directory mode: '0700' - name: Deploy Libvirt private variables source templates ansible.builtin.template: src: "libvirt/variables.source.j2" - dest: "{{ terraform_config_dir }}/private/variables.source" + dest: "{{ tf_config_dir }}/private/variables.source" mode: '0600' - name: Deploy Libvirt general resources templates ansible.builtin.template: src: "libvirt/{{ item }}.tf.j2" - dest: "{{ terraform_config_dir }}/{{ item }}.tf" + dest: "{{ tf_config_dir }}/{{ item }}.tf" mode: '0640' with_items: - 'provider' @@ -44,21 +44,21 @@ content: | #cloud-config {{ item.cfg | to_nice_yaml(indent=2, width=9999) }} - dest: "{{ terraform_config_dir }}/cloud_init_{{ item.name }}.cfg" + dest: "{{ tf_config_dir }}/cloud_init_{{ item.name }}.cfg" mode: '0640' with_items: - - "{{ terraform_libvirt_cloud_inits }}" + - "{{ tf_libvirt_cloud_inits }}" - name: Deploy Libvirt virtual machines specific templates ansible.builtin.include_tasks: libvirt-vms.yml with_items: - - "{{ terraform_libvirt_vms_groups }}" + - "{{ tf_libvirt_vms_groups }}" loop_control: - loop_var: terraform_group_name + loop_var: tf_group_name - name: Display a reminder for sourcing variables ansible.builtin.debug: msg: - - "Remember to import the variable file in order to tell Terraform which" - - "subscription use during the run. Before run Terraform, please execute:" - - " source {{ terraform_config_dir }}/private/variables.source" + - "Remember to import the variable file in order to tell the tf cmd which" + - "subscription use during the run. Before run the tf cmd, please execute:" + - " source {{ tf_config_dir }}/private/variables.source" diff --git a/tasks/main.yml b/tasks/main.yml index 41d370e..4c0fcfa 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,22 +1,22 @@ --- -- name: Install terraform locally +- name: Install tf cmd locally ansible.builtin.include_tasks: install.yml when: - - terraform_binary_install | bool + - tf_binary_install | bool -- name: "Create local configurations directory {{ terraform_config_dir }}" +- name: "Create local configurations directory {{ tf_config_dir }}" ansible.builtin.file: - path: "{{ terraform_config_dir }}" + path: "{{ tf_config_dir }}" state: directory mode: '0750' - name: Deploy Azure environment ansible.builtin.include_tasks: azure.yml when: - - terraform_cloud_provider == 'azure' + - tf_cloud_provider == 'azure' - name: Deploy Libvirt environment ansible.builtin.include_tasks: libvirt.yml when: - - terraform_cloud_provider == 'libvirt' + - tf_cloud_provider == 'libvirt' diff --git a/templates/azure/provider.tf.j2 b/templates/azure/provider.tf.j2 index c365c6e..9cdb91b 100644 --- a/templates/azure/provider.tf.j2 +++ b/templates/azure/provider.tf.j2 @@ -29,13 +29,13 @@ terraform { version = "{{ azure_provider_version }}" } } -{% if terraform_tfstate_on_azure_storage_container is defined and +{% if tf_tfstate_on_azure_storage_container is defined and skip_azure_backend is not defined %} backend "azurerm" { resource_group_name = "{{ azure_tfstate_resource_group_name }}" - storage_account_name = "{{ terraform_tfstate_on_azure_storage_container }}" - container_name = "{{ terraform_tfstate_on_azure_storage_container }}" + storage_account_name = "{{ tf_tfstate_on_azure_storage_container }}" + container_name = "{{ tf_tfstate_on_azure_storage_container }}" key = "terraform.tfstate" } {% endif %} diff --git a/templates/libvirt/cloud_init.tf.j2 b/templates/libvirt/cloud_init.tf.j2 index ef097fb..6ae3964 100644 --- a/templates/libvirt/cloud_init.tf.j2 +++ b/templates/libvirt/cloud_init.tf.j2 @@ -1,9 +1,9 @@ # Cloud init that will be created: -{% for cloud_init in terraform_libvirt_cloud_inits %} +{% for cloud_init in tf_libvirt_cloud_inits %} # {{ cloud_init.name }} {{ cloud_init.pool | default('default') }} {% endfor %} -{% for cloud_init in terraform_libvirt_cloud_inits %} +{% for cloud_init in tf_libvirt_cloud_inits %} resource "libvirt_cloudinit_disk" "{{ cloud_init.name }}" { name = "cloud_init_{{ cloud_init.name }}.iso" diff --git a/templates/libvirt/networks.tf.j2 b/templates/libvirt/networks.tf.j2 index 7e4a14f..36f24a8 100644 --- a/templates/libvirt/networks.tf.j2 +++ b/templates/libvirt/networks.tf.j2 @@ -1,9 +1,9 @@ # Subnet that will be created: -{% for network in terraform_libvirt_networks %} +{% for network in tf_libvirt_networks %} # {{ network.name }} {{ network.mode }} {% for address in network.addresses %}"{{ address }}"{%- if not loop.last -%}, {%- endif -%}{% endfor %} {% endfor %} -{% for network in terraform_libvirt_networks %} +{% for network in tf_libvirt_networks %} resource "libvirt_network" "{{ network.name }}" { name = "{{ network.name }}" mode = "{{ network.mode }}" diff --git a/templates/libvirt/pools.tf.j2 b/templates/libvirt/pools.tf.j2 index 24709ad..f042559 100644 --- a/templates/libvirt/pools.tf.j2 +++ b/templates/libvirt/pools.tf.j2 @@ -1,5 +1,5 @@ # Libvirt Storage Pools -{% for pool in terraform_libvirt_pools %} +{% for pool in tf_libvirt_pools %} resource "libvirt_pool" "{{ pool.name }}" { name = "{{ pool.name }}" diff --git a/templates/libvirt/provider.tf.j2 b/templates/libvirt/provider.tf.j2 index 8d5d1c9..3efd67c 100644 --- a/templates/libvirt/provider.tf.j2 +++ b/templates/libvirt/provider.tf.j2 @@ -8,7 +8,7 @@ terraform { libvirt = { source = "dmacvicar/libvirt" - version = "{{ terraform_libvirt_provider_version }}" + version = "{{ tf_libvirt_provider_version }}" } } } diff --git a/templates/libvirt/variables.source.j2 b/templates/libvirt/variables.source.j2 index 1abc99e..8ef5e6d 100644 --- a/templates/libvirt/variables.source.j2 +++ b/templates/libvirt/variables.source.j2 @@ -1,2 +1,2 @@ # This file should be sourced before any Terraform run -export TF_VAR_libvirt_uri='{{ terraform_libvirt_uri }}' +export TF_VAR_libvirt_uri='{{ tf_libvirt_uri }}' diff --git a/templates/libvirt/vms.tf.j2 b/templates/libvirt/vms.tf.j2 index 85b7ea2..60b54f4 100644 --- a/templates/libvirt/vms.tf.j2 +++ b/templates/libvirt/vms.tf.j2 @@ -1,6 +1,6 @@ # Libvirt VMs and related volumes creation blocks -{% for host in groups[terraform_group_name] %} -{% set vm = hostvars[host].terraform_libvirt %} +{% for host in groups[tf_group_name] %} +{% set vm = hostvars[host].tf_libvirt %} # VM {{ vm.name }} specific storage volumes {% for disk in vm.disks %} diff --git a/templates/libvirt/volumes.tf.j2 b/templates/libvirt/volumes.tf.j2 index 61decc4..ce8bc2f 100644 --- a/templates/libvirt/volumes.tf.j2 +++ b/templates/libvirt/volumes.tf.j2 @@ -1,5 +1,5 @@ # Libvirt general storage volumes -{% for volume in terraform_libvirt_volumes %} +{% for volume in tf_libvirt_volumes %} resource "libvirt_volume" "{{ volume.volume_id }}" { name = "{{ volume.file }}" diff --git a/tests/inventory/host_vars/myvm-1.yml b/tests/inventory/host_vars/myvm-1.yml index bf4b5d8..fcabb84 100644 --- a/tests/inventory/host_vars/myvm-1.yml +++ b/tests/inventory/host_vars/myvm-1.yml @@ -1,6 +1,6 @@ --- -terraform_libvirt: +tf_libvirt: name: myvm-1 memory: 1024 vcpu: 1 diff --git a/tests/inventory/host_vars/myvm-2.yml b/tests/inventory/host_vars/myvm-2.yml index bfeed1c..1801bd0 100644 --- a/tests/inventory/host_vars/myvm-2.yml +++ b/tests/inventory/host_vars/myvm-2.yml @@ -1,6 +1,6 @@ --- -terraform_libvirt: +tf_libvirt: name: myvm-2 memory: 1024 vcpu: 1 diff --git a/tests/terraform_tfs_generator.yml b/tests/terraform_tfs_generator.yml deleted file mode 100644 index 38d6b99..0000000 --- a/tests/terraform_tfs_generator.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- - -- name: Create Terraform manifests using terraform_tfs_generator Ansible role - hosts: localhost - roles: - - role: ../.. diff --git a/tests/tfs_generator.yml b/tests/tfs_generator.yml new file mode 100644 index 0000000..b2a1cad --- /dev/null +++ b/tests/tfs_generator.yml @@ -0,0 +1,6 @@ +--- + +- name: Create tf manifests using tfs_generator Ansible role + hosts: localhost + roles: + - role: ../..