<p>A complex <code>.yaml</code> file from <a href="https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml" rel="noreferrer">this link</a> needs to be fed into a bash script that runs as part of an automation program running on an EC2 instance of Amazon Linux 2.  Note that the <code>.yaml</code> file in the link above contains many objects, and that I need to extract one of the environment variables defined inside one of the many objects that are defined in the file.  </p>

<blockquote>
  <p>Specifically, how can I extract the <code>192.168.0.0/16</code> value of the <code>CALICO_IPV4POOL_CIDR</code> variable into a bash variable?  </p>
</blockquote>

<pre><code>        - name: CALICO_IPV4POOL_CIDR
          value: "192.168.0.0/16"
</code></pre>

<p>I have read a lot of other postings and blog entries about parsing flatter, simpler <code>.yaml</code> files, but none of those other examples show how to extract a nested value like the <code>value</code> of <code>CALICO_IPV4POOL_CIDR</code> in this question.</p>

CodeMed
<p>As others are commenting, it is recommended to make use of <code>yq</code> (along with <code>jq</code>) if available.<br>
Then please try the following:</p>

<pre><code>value=$(yq -r 'recurse | select(.name? == "CALICO_IPV4POOL_CIDR") | .value' "calico.yaml")
echo "$value"
</code></pre>

<p>Output:</p>

<pre><code>192.168.0.0/16
</code></pre>
