-1

I am trying to send the output of a linux command as an input to variable. Here is the command I tried

cat init.txt | grep glal20213lvdlb04
glal13lvd            

The output of this should go into "vm_name" of the yml file. The yml file will look like this.

vim instant.yml
resources:

  instance01:
    type: ../../../templates/ipnf.yaml
    properties:
      vm_name: 'THE OUTPUT SHOULD COME HERE'
      vm_flavour: 'dsolate'
      image_name: 'pdns_14121207'
      vm_az: 'az-1'
      vm_disk_root_size: '50'
      vm_disk_data_size: '50'
      network_mgmt_refs: 'int:dxs_pcg'
      network_mgmt_ip: '10.194.112.75'

Any help would be appreciated

Siddharth
  • 192
  • 3
  • 20

2 Answers2

1

You can use to replace the YAML.

Use yq's strenv() to get a bash env variable.

We can export the desired value using

export newValue="$(grep glal20213lvdlb04 init.txt)"

And the generate the YAML:

 yq e '.resources.instance01.properties.vm_name = strenv(newValue)' instant.yml

Ouput:

resources:
  instance01:
    type: ../../../templates/ipnf.yaml
    properties:
      vm_name: 'glal20213lvdlb04'
      vm_flavour: 'dsolate'
      image_name: 'pdns_14121207'
      vm_az: 'az-1'
      vm_disk_root_size: '50'
      vm_disk_data_size: '50'
      network_mgmt_refs: 'int:dxs_pcg'
      network_mgmt_ip: '10.194.112.75'
0stone0
  • 34,288
  • 4
  • 39
  • 64
1

While I would recommend a proper YAML processor like yq, for your particular situation, you may be able to utilize sed:

var=$(grep 'glal20213lvdlb04' init.txt) ; sed -i'' -e "s/THE OUTPUT SHOULD COME HERE/${var}/g" instant.yaml

Output:

resources:

  instance01:
    type: ../../../templates/ipnf.yaml
    properties:
      vm_name: 'glal20213lvdlb04'
      vm_flavour: 'dsolate'
      image_name: 'glal20213lvdlb04'
      vm_az: 'az-1'
      vm_disk_root_size: '50'
      vm_disk_data_size: '50'
      network_mgmt_refs: 'int:dxs_pcg'
      network_mgmt_ip: '10.194.112.75'
j_b
  • 1,975
  • 3
  • 8
  • 14
  • This worked. But can "THE OUTPUT SHOULD COME HERE" be auto detected? Like search for vm_name= and replace whatever it is with $var? @j_b – Siddharth Nov 01 '22 at 14:40