ansible – store stdout to custome variables with set_fact

You’re reading a template file

shell> cat conf.j2
kubeadm_version: "{{ kube_version }}"
crun_version: 1.4.4
runc_version: v1.1.1
kata_containers_version: 2.2.3
youki_version: 0.0.1
gvisor_version: 20210921
containerd_version: 1.6.3
cri_dockerd_version: v0.2.0
docker_containerd_version: 1.4.12
calico_version: "v3.22.2"
calico_ctl_version: "{{ calico_version }}"
calico_cni_version: "{{ calico_version }}"
calico_flexvol_version: "{{ calico_version }}"
calico_policy_version: "{{ calico_version }}"
calico_typha_version: "{{ calico_version }}"
calico_apiserver_version: "{{ calico_version }}"
flannel_version: "v0.17.0"
flannel_cni_version: "v1.0.1"
cni_version: "v1.0.1"
weave_version: 2.8.1
pod_infra_version: "3.3"
cilium_version: "v1.11.1"
kube_ovn_version: "v1.8.1"
kube_router_version: "v1.4.0"
multus_version: "v3.8"
helm_version: "v3.8.2"
nerdctl_version: "0.19.0"
krew_version: "v0.4.2"
kube_major_version: "{{ kube_version | regex_replace('^v([0-9])+\.([0-9]+)\.[0-9]+', 'v\1.\2') }}"
etcd_version: "{{ etcd_supported_versions[kube_major_version] }}"
crictl_version: "{{ crictl_supported_versions[kube_major_version] }}"
netcheck_version: "v1.2.2"
coredns_version: "{{ 'v1.8.6' if (kube_version is version('v1.23.0','>=')) else 'v1.8.0' }}"
nodelocaldns_version: "1.21.1"
dnsautoscaler_version: 1.8.5
metrics_server_version: "v0.5.2"
cert_manager_version: "v1.8.0"
metallb_version: v0.12.1

What you see is the raw content of the file

    - command: cat conf.j2
      register: get_version
    - debug:
        var: get_version.stdout

rendered by the Ansible default callback plugin (ANSIBLE_STDOUT_CALLBACK=default)

ok: [localhost] => {
    "get_version.stdout": "kubeadm_version: "{{ kube_version }}"ncrun_version: 1.4.4nrunc_version: v1.1.1nkata_containers_version: 2.2.3nyouki_version: 0.0.1ngvisor_version: 20210921ncontainerd_version: 1.6.3ncri_dockerd_version: v0.2.0ndocker_containerd_version: 1.4.12ncalico_version: "v3.22.2"ncalico_ctl_version: "{{ calico_version }}"ncalico_cni_version: "{{ calico_version }}"ncalico_flexvol_version: "{{ calico_version }}"ncalico_policy_version: "{{ calico_version }}"ncalico_typha_version: "{{ calico_version }}"ncalico_apiserver_version: "{{ calico_version }}"nflannel_version: "v0.17.0"nflannel_cni_version: "v1.0.1"ncni_version: "v1.0.1"nweave_version: 2.8.1npod_infra_version: "3.3"ncilium_version: "v1.11.1"nkube_ovn_version: "v1.8.1"nkube_router_version: "v1.4.0"nmultus_version: "v3.8"nhelm_version: "v3.8.2"nnerdctl_version: "0.19.0"nkrew_version: "v0.4.2"nkube_major_version: "{{ kube_version | regex_replace('^v([0-9])+\\.([0-9]+)\\.[0-9]+', 'v\\1.\\2') }}"netcd_version: "{{ etcd_supported_versions[kube_major_version] }}"ncrictl_version: "{{ crictl_supported_versions[kube_major_version] }}"nnetcheck_version: "v1.2.2"ncoredns_version: "{{ 'v1.8.6' if (kube_version is version('v1.23.0','>=')) else 'v1.8.0' }}"nnodelocaldns_version: "1.21.1"ndnsautoscaler_version: 1.8.5nmetrics_server_version: "v0.5.2"ncert_manager_version: "v1.8.0"nmetallb_version: v0.12.1"
}

The same in a SO block for better readability

ok: [localhost] => { “get_version.stdout”: “kubeadm_version: “{{ kube_version }}”ncrun_version: 1.4.4nrunc_version: v1.1.1nkata_containers_version: 2.2.3nyouki_version: 0.0.1ngvisor_version: 20210921ncontainerd_version: 1.6.3ncri_dockerd_version: v0.2.0ndocker_containerd_version: 1.4.12ncalico_version: “v3.22.2″ncalico_ctl_version: “{{ calico_version }}”ncalico_cni_version: “{{ calico_version }}”ncalico_{calico_version_version:”{{ }}”ncalico_policy_version: “{{ calico_version }}”ncalico_typha_version: “{{ calico_version }}”ncalico_apiserver_version: “{{ calico_version }}”nflannel_version: “v0.17.0″nflannel_cni_version: “v1. ncni_version: “v1.0.1″nweave_version: 2.8.1npod_infra_version: “3.3”ncilium_version: “v1.11.1″nkube_ovn_version: “v1.8.1″nkube_router_version: “v1.4.0″nmultus_version: “v3.8 “nhelm_version: “v3.8.2″nnerdctl_version: “0.19.0”nkrew_version: “v0.4.2″nkube_major_version: “{{ kube_version | regex_replace(‘^v([0-9](+\.)[0-9]+)\.[0-9]+’, ‘v\1.\2’) }}”netcd_version: “{{ etcd_supported_versions[kube_major_version] }}”ncrictl_version: “{{ crictl_supported_versions[kube_major_version] }}”nnetcheck_version: “v1.2.2″ncoredns_version: “{{ ‘v1.8.6’ if (kube_version is version(‘v1.23.0′,’>=’)) else ‘v1.8.0’ }}” nnodelocaldns_version: “1.21.1”ndnsautoscaler_version: 1.8.5nmetrics_server_version: “v0.5.2″ncert_manager_version: “v1.8.0″nmetallb_version: v0.12.1” }

As a side note, you might want to render the output by the Ansible yaml callback plugin (ANSIBLE_STDOUT_CALLBACK=yaml)

ok: [localhost] => 
  get_version.stdout: |-
    kubeadm_version: "{{ kube_version }}"
    crun_version: 1.4.4
    runc_version: v1.1.1
    kata_containers_version: 2.2.3
    youki_version: 0.0.1
    gvisor_version: 20210921
    containerd_version: 1.6.3
    cri_dockerd_version: v0.2.0
    docker_containerd_version: 1.4.12
    calico_version: "v3.22.2"
    calico_ctl_version: "{{ calico_version }}"
    calico_cni_version: "{{ calico_version }}"
    calico_flexvol_version: "{{ calico_version }}"
    calico_policy_version: "{{ calico_version }}"
    calico_typha_version: "{{ calico_version }}"
    calico_apiserver_version: "{{ calico_version }}"
    flannel_version: "v0.17.0"
    flannel_cni_version: "v1.0.1"
    cni_version: "v1.0.1"
    weave_version: 2.8.1
    pod_infra_version: "3.3"
    cilium_version: "v1.11.1"
    kube_ovn_version: "v1.8.1"
    kube_router_version: "v1.4.0"
    multus_version: "v3.8"
    helm_version: "v3.8.2"
    nerdctl_version: "0.19.0"
    krew_version: "v0.4.2"
    kube_major_version: "{{ kube_version | regex_replace('^v([0-9])+\.([0-9]+)\.[0-9]+', 'v\1.\2') }}"
    etcd_version: "{{ etcd_supported_versions[kube_major_version] }}"
    crictl_version: "{{ crictl_supported_versions[kube_major_version] }}"
    netcheck_version: "v1.2.2"
    coredns_version: "{{ 'v1.8.6' if (kube_version is version('v1.23.0','>=')) else 'v1.8.0' }}"
    nodelocaldns_version: "1.21.1"
    dnsautoscaler_version: 1.8.5
    metrics_server_version: "v0.5.2"
    cert_manager_version: "v1.8.0"
    metallb_version: v0.12.1

The purpose of the template files is to substitute the variables and create files. For example, use the template plugin and display the result

    - debug:
        msg: "{{ lookup('template', 'conf.j2') }}"
      vars:
        kube_major_version: '1'
        kube_version: 'v1.23'
        calico_version: '3.22'
        etcd_supported_versions:
          '1': ['3.5', '3.4', '3.3']
        crictl_supported_versions:
          '1': ['1.15', '1.14', '1.13']

ok: [localhost] => 
  msg: |-
    kubeadm_version: "v1.23"
    crun_version: 1.4.4
    runc_version: v1.1.1
    kata_containers_version: 2.2.3
    youki_version: 0.0.1
    gvisor_version: 20210921
    containerd_version: 1.6.3
    cri_dockerd_version: v0.2.0
    docker_containerd_version: 1.4.12
    calico_version: "v3.22.2"
    calico_ctl_version: "3.22"
    calico_cni_version: "3.22"
    calico_flexvol_version: "3.22"
    calico_policy_version: "3.22"
    calico_typha_version: "3.22"
    calico_apiserver_version: "3.22"
    flannel_version: "v0.17.0"
    flannel_cni_version: "v1.0.1"
    cni_version: "v1.0.1"
    weave_version: 2.8.1
    pod_infra_version: "3.3"
    cilium_version: "v1.11.1"
    kube_ovn_version: "v1.8.1"
    kube_router_version: "v1.4.0"
    multus_version: "v3.8"
    helm_version: "v3.8.2"
    nerdctl_version: "0.19.0"
    krew_version: "v0.4.2"
    kube_major_version: "v1.23"
    etcd_version: "['3.5', '3.4', '3.3']"
    crictl_version: "['1.15', '1.14', '1.13']"
    netcheck_version: "v1.2.2"
    coredns_version: "v1.8.0"
    nodelocaldns_version: "1.21.1"
    dnsautoscaler_version: 1.8.5
    metrics_server_version: "v0.5.2"
    cert_manager_version: "v1.8.0"
    metallb_version: v0.12.1

Or, use the template module to create a file

    - template:
        src: conf.j2
        dest: conf
      vars:
        kube_major_version: '1'
        kube_version: 'v1.23'
        calico_version: '3.22'
        etcd_supported_versions:
          '1': ['3.5', '3.4', '3.3']
        crictl_supported_versions:
          '1': ['1.15', '1.14', '1.13']

shell> cat conf
kubeadm_version: "v1.23"
crun_version: 1.4.4
runc_version: v1.1.1
kata_containers_version: 2.2.3
youki_version: 0.0.1
gvisor_version: 20210921
containerd_version: 1.6.3
cri_dockerd_version: v0.2.0
docker_containerd_version: 1.4.12
calico_version: "v3.22.2"
calico_ctl_version: "3.22"
calico_cni_version: "3.22"
calico_flexvol_version: "3.22"
calico_policy_version: "3.22"
calico_typha_version: "3.22"
calico_apiserver_version: "3.22"
flannel_version: "v0.17.0"
flannel_cni_version: "v1.0.1"
cni_version: "v1.0.1"
weave_version: 2.8.1
pod_infra_version: "3.3"
cilium_version: "v1.11.1"
kube_ovn_version: "v1.8.1"
kube_router_version: "v1.4.0"
multus_version: "v3.8"
helm_version: "v3.8.2"
nerdctl_version: "0.19.0"
krew_version: "v0.4.2"
kube_major_version: "v1.23"
etcd_version: "['3.5', '3.4', '3.3']"
crictl_version: "['1.15', '1.14', '1.13']"
netcheck_version: "v1.2.2"
coredns_version: "v1.8.0"
nodelocaldns_version: "1.21.1"
dnsautoscaler_version: 1.8.5
metrics_server_version: "v0.5.2"
cert_manager_version: "v1.8.0"
metallb_version: v0.12.1

Leave a Comment