Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

terraform - converting list variable to ansible inventory file

I have some terraform configuration files that I use in order to provision virtual machines, it does exactly what I want it to do, details for the virtual machines I want to provision are specified in a list which looks like this:

variable "virtual_machines" {
  default = {
    "master1" = {
       name         = "z-ca-bdc-master1"
       worker_node  = false
       ipv4_address = "192.168.113.79"
       ipv4_netmask = "22"
       ipv4_gateway = "192.168.112.1"
       dns_server   = "192.168.112.2"
       ram          = 8192
       logical_cpu  = 4
       disk0_size   = 40
       disk1_size   = 0
    },
    "master2" =  {
       name         = "z-ca-bdc-master2"
       worker_node  = false
       ipv4_address = "192.168.113.80"
       ipv4_netmask = "22"
       ipv4_gateway = "192.168.112.1"
       dns_server   = "192.168.112.2"
       ram          = 8192
       logical_cpu  = 4
       disk0_size   = 40
       disk1_size   = 0
    },

After I have created these I then want to use local-exec to invoke ansible in order to create a Kubernetes cluster, the challenge I have is that the format of an inventory.ini file looks somewhat different to my list variable, this is a sample template provided:

[all]
# node1 ansible_host=95.54.0.12  # ip=10.3.0.1 etcd_member_name=etcd1
# node2 ansible_host=95.54.0.13  # ip=10.3.0.2 etcd_member_name=etcd2
# node3 ansible_host=95.54.0.14  # ip=10.3.0.3 etcd_member_name=etcd3
# node4 ansible_host=95.54.0.15  # ip=10.3.0.4 etcd_member_name=etcd4
# node5 ansible_host=95.54.0.16  # ip=10.3.0.5 etcd_member_name=etcd5
# node6 ansible_host=95.54.0.17  # ip=10.3.0.6 etcd_member_name=etcd6

# ## configure a bastion host if your nodes are not directly reachable
# bastion ansible_host=x.x.x.x ansible_user=some_user

[kube-master]
# node1
# node2
# node3

[etcd]
# node1
# node2
# node3

[kube-node]
# node2
# node3
# node4
# node5
# node6

[calico-rr]

[k8s-cluster:children]
kube-master
kube-node
calico-rr

I could use something such as awk to create this, however - I was wondering if there is a more simpler and elegant solution to this.

question from:https://stackoverflow.com/questions/65920381/converting-list-variable-to-ansible-inventory-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...