puppetlabs / puppetlabs-pecdm

Puppet Bolt driven fusion of puppetlabs/peadm and Terraform.
Apache License 2.0
14 stars 19 forks source link

Add feature for fullpath terraform inventory.yaml references #108

Open gavindidrichsen opened 1 year ago

gavindidrichsen commented 1 year ago

Use Case

As a module developer, I want to be able

---
config:
  transport: ssh
  ssh:
    host-key-check: false
    run-as: root
    user: user
    private-key: ~/.ssh/id_rsa-acceptance
  winrm:
    user: user
    password: Puppetlabs123@
    ssl: false
groups:
  - name: peadm_nodes
    groups:
      - name: pe_server
        targets:
          - _plugin: terraform
            dir: .terraform/google_pe_arch
            resource_type: google_compute_instance.server
            target_mapping:
              name: metadata.internalDNS
              uri: network_interface.0.network_ip
...
...

I believe that the above is generated via the inventory_yaml.epp

Describe the Solution You Would Like

Can we have some option to produce fullpaths as well?

Describe Alternatives You've Considered

I've created a ruby script to amend all my terraform inventory.yaml files so that I can achieve the above. For example, I added the following script to my PATH and then I run this from any GCP terraform inventory.yaml directories

#!/opt/puppetlabs/puppet/bin/ruby
require 'yaml'
require 'fileutils'

# ABORT IF:
# (1) current directory does not contain the inventory.yaml file
# (2) inventory.yaml is not using terraform, i.e. no "_plugin: terraform" entries
abort("ERROR: current directory does not contain the inventory.yaml file") unless File.exist?('inventory.yaml')
abort("INFO: inventory.yaml is not using the terraform plugin so no need to proceed") unless File.read('inventory.yaml').include?('_plugin: terraform')

# ensure all "dir" entries contain fullpath to the directory containing the inventory.yaml file
directory = File.expand_path(File.dirname('inventory.yaml'))
File.open('inventory.yaml', "r+") do |file|
  contents = file.read
  contents.gsub!("dir: .terraform", "dir: #{directory}/.terraform")
  contents.gsub!("dir: terraform", "dir: #{directory}/terraform")
  file.rewind
  file.write(contents)
end