linux下轻量级工具:yq(json,yaml)格式转换工具安装和使用

2023-11-13 08:42:41 浏览数 (2)

1、概述

有时需要将json和yaml格式的配置文件进行相互转换,那么在linux的环境下,yq就是一个很好的命令行的工具。

本文介绍yq工具的安装和基本的使用。

2、yq命令安装

通过以下的命令安装yq命令

代码语言:javascript复制
wget https://github.com/mikefarah/yq/releases/download/v4.16.2/yq_linux_amd64 
  && chmod  x yq_linux_amd64 
  && mv yq_linux_amd64 /usr/local/bin/yq

安装之后,输入以下的命令,确认yq已经正确的完整安装:

代码语言:javascript复制
[root@nctest-snap-test-02 ~]# yq --version
yq (https://github.com/mikefarah/yq/) version 4.16.2
[root@nctest-snap-test-02 ~]# 

3、yq命令的使用

3.1、yaml转换为json

代码语言:javascript复制
yq eval -o json initnginx.yaml |tee initnginx.json

最后一个是yaml文件的名字。

输出结果如下:

代码语言:javascript复制
[root@nctest-snap-test-02 practises]# yq eval -o json initnginx.yaml |tee initnginx.json
{
  "apiVersion": "v1",
  "kind": "Pod",
  "metadata": {
    "annotations": {
      "cni.projectcalico.org/podIP": "172.23.29.31/32",
      "cni.projectcalico.org/podIPs": "172.23.29.31/32",
      "kubectl.kubernetes.io/last-applied-configuration": "{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"initnginx","namespace":"default"},"spec":{"containers":[{"image":"172.20.45.174:81/base/nginx:1.15-alpine","name":"nginx","ports":[{"containerPort":80}],"volumeMounts":[{"mountPath":"/usr/share/nginx/html","name":"workdir"}]}],"dnsPolicy":"Default","initContainers":[{"command":["wget","-O","/work-dir/index.html","https://www.baidu.com"],"image":"busybox","name":"install","volumeMounts":[{"mountPath":"/work-dir","name":"workdir"}]}],"volumes":[{"emptyDir":{},"name":"workdir"}]}}n"
    },
    "creationTimestamp": "2022-01-11T05:49:56Z",
    "managedFields": [
      {
        "apiVersion": "v1",
        "fieldsType": "FieldsV1",
        "fieldsV1": {
          "f:metadata": {
            "f:annotations": {
              ".": {},
              "f:kubectl.kubernetes.io/last-applied-configuration": {}
            }
          },
          "f:spec": {
            "f:containers": {
              "k:{"name":"nginx"}": {
                ".": {},
                "f:imagePullPolicy": {},
                "f:name": {},
                "f:ports": {
                  ".": {},
                  "k:{"containerPort":80,"protocol":"TCP"}": {
                    ".": {},
                    "f:containerPort": {},
                    "f:protocol": {}
                  }
                },
                "f:resources": {},
                "f:terminationMessagePath": {},
                "f:terminationMessagePolicy": {},
                "f:volumeMounts": {
                  ".": {},
                  "k:{"mountPath":"/usr/share/nginx/html"}": {
                    ".": {},
                    "f:mountPath": {},
                    "f:name": {}
                  }
                }
              }
            },
            "f:dnsPolicy": {},
            "f:enableServiceLinks": {},
            "f:initContainers": {
              ".": {},
              "k:{"name":"install"}": {
                ".": {},
                "f:command": {},
                "f:imagePullPolicy": {},
                "f:name": {},
                "f:resources": {},
                "f:terminationMessagePath": {},
                "f:terminationMessagePolicy": {},
                "f:volumeMounts": {
                  ".": {},
                  "k:{"mountPath":"/work-dir"}": {
                    ".": {},
                    "f:mountPath": {},
                    "f:name": {}
                  }
                }
              }
            },
            "f:restartPolicy": {},
            "f:schedulerName": {},
            "f:securityContext": {},
            "f:terminationGracePeriodSeconds": {},
            "f:volumes": {
              ".": {},
              "k:{"name":"workdir"}": {
                ".": {},
                "f:emptyDir": {},
                "f:name": {}
              }
            }
          }
        },
        "manager": "kubectl-client-side-apply",
        "operation": "Update",
        "time": "2022-01-11T05:49:56Z"
      },
      {
        "apiVersion": "v1",
        "fieldsType": "FieldsV1",
        "fieldsV1": {
          "f:metadata": {
            "f:annotations": {
              "f:cni.projectcalico.org/podIP": {},
              "f:cni.projectcalico.org/podIPs": {}
            }
          }
        },
        "manager": "calico",
        "operation": "Update",
        "time": "2022-01-11T05:49:58Z"
      },
      {
        "apiVersion": "v1",
        "fieldsType": "FieldsV1",
        "fieldsV1": {
          "f:spec": {
            "f:containers": {
              "k:{"name":"nginx"}": {
                "f:image": {}
              }
            },
            "f:initContainers": {
              "k:{"name":"install"}": {
                "f:image": {}
              }
            }
          }
        },
        "manager": "kubectl-edit",
        "operation": "Update",
        "time": "2022-01-11T06:16:59Z"
      },
      {
        "apiVersion": "v1",
        "fieldsType": "FieldsV1",
        "fieldsV1": {
          "f:status": {
            "f:conditions": {
              "k:{"type":"ContainersReady"}": {
                ".": {},
                "f:lastProbeTime": {},
                "f:lastTransitionTime": {},
                "f:status": {},
                "f:type": {}
              },
              "k:{"type":"Initialized"}": {
                ".": {},
                "f:lastProbeTime": {},
                "f:lastTransitionTime": {},
                "f:status": {},
                "f:type": {}
              },
              "k:{"type":"Ready"}": {
                ".": {},
                "f:lastProbeTime": {},
                "f:lastTransitionTime": {},
                "f:status": {},
                "f:type": {}
              }
            },
            "f:containerStatuses": {},
            "f:hostIP": {},
            "f:initContainerStatuses": {},
            "f:phase": {},
            "f:podIP": {},
            "f:podIPs": {
              ".": {},
              "k:{"ip":"172.23.29.31"}": {
                ".": {},
                "f:ip": {}
              }
            },
            "f:startTime": {}
          }
        },
        "manager": "kubelet",
        "operation": "Update",
        "time": "2022-01-11T06:18:21Z"
      }
    ],
    "name": "initnginx",
    "namespace": "default",
    "resourceVersion": "11304453",
    "selfLink": "/api/v1/namespaces/default/pods/initnginx",
    "uid": "8f379d58-8bba-4d2b-8b1e-c9fb9f36305b"
  },
  "spec": {
    "containers": [
      {
        "image": "172.20.45.174:81/base/nginx:1.15-alpine",
        "imagePullPolicy": "IfNotPresent",
        "name": "nginx",
        "ports": [
          {
            "containerPort": 80,
            "protocol": "TCP"
          }
        ],
        "resources": {},
        "terminationMessagePath": "/dev/termination-log",
        "terminationMessagePolicy": "File",
        "volumeMounts": [
          {
            "mountPath": "/usr/share/nginx/html",
            "name": "workdir"
          },
          {
            "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
            "name": "default-token-8ss8s",
            "readOnly": true
          }
        ]
      }
    ],
    "dnsPolicy": "Default",
    "enableServiceLinks": true,
    "initContainers": [
      {
        "command": [
          "wget",
          "-O",
          "/work-dir/index.html",
          "https://www.baidu.com"
        ],
        "image": "busybox:1.0",
        "imagePullPolicy": "Always",
        "name": "install",
        "resources": {},
        "terminationMessagePath": "/dev/termination-log",
        "terminationMessagePolicy": "File",
        "volumeMounts": [
          {
            "mountPath": "/work-dir",
            "name": "workdir"
          },
          {
            "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
            "name": "default-token-8ss8s",
            "readOnly": true
          }
        ]
      }
    ],
    "nodeName": "172.20.59.57",
    "preemptionPolicy": "PreemptLowerPriority",
    "priority": 0,
    "restartPolicy": "Always",
    "schedulerName": "default-scheduler",
    "securityContext": {},
    "serviceAccount": "default",
    "serviceAccountName": "default",
    "terminationGracePeriodSeconds": 30,
    "tolerations": [
      {
        "effect": "NoExecute",
        "key": "node.kubernetes.io/not-ready",
        "operator": "Exists",
        "tolerationSeconds": 300
      },
      {
        "effect": "NoExecute",
        "key": "node.kubernetes.io/unreachable",
        "operator": "Exists",
        "tolerationSeconds": 300
      }
    ],
    "volumes": [
      {
        "emptyDir": {},
        "name": "workdir"
      },
      {
        "name": "default-token-8ss8s",
        "secret": {
          "defaultMode": 420,
          "secretName": "default-token-8ss8s"
        }
      }
    ]
  },
  "status": {
    "conditions": [
      {
        "lastProbeTime": null,
        "lastTransitionTime": "2022-01-11T05:50:08Z",
        "status": "True",
        "type": "Initialized"
      },
      {
        "lastProbeTime": null,
        "lastTransitionTime": "2022-01-11T06:18:21Z",
        "status": "True",
        "type": "Ready"
      },
      {
        "lastProbeTime": null,
        "lastTransitionTime": "2022-01-11T06:18:21Z",
        "status": "True",
        "type": "ContainersReady"
      },
      {
        "lastProbeTime": null,
        "lastTransitionTime": "2022-01-11T05:49:57Z",
        "status": "True",
        "type": "PodScheduled"
      }
    ],
    "containerStatuses": [
      {
        "containerID": "docker://d3d78a9b51867e9fe5ea1844c1e2f80776efdbc25a6fba97a5e0485daf92ff7d",
        "image": "172.20.45.174:81/base/nginx:1.15-alpine",
        "imageID": "docker-pullable://172.20.45.174:81/base/nginx@sha256:478a73bcec93acc3e814ddd6fb2f95c6f6b4b0d0f168a4feaa039513d260a5d9",
        "lastState": {
          "terminated": {
            "containerID": "docker://54e79b997c6cae2d39af27e3ede4f9e7e184c34f2b7b94110cd94ea02cd82356",
            "exitCode": 143,
            "finishedAt": "2022-01-11T06:16:59Z",
            "reason": "Error",
            "startedAt": "2022-01-11T06:09:25Z"
          }
        },
        "name": "nginx",
        "ready": true,
        "restartCount": 2,
        "started": true,
        "state": {
          "running": {
            "startedAt": "2022-01-11T06:18:21Z"
          }
        }
      }
    ],
    "hostIP": "172.20.59.57",
    "initContainerStatuses": [
      {
        "containerID": "docker://9d2aec37255a02a2b13abced64717057eb100dd329bc6b6529211e30595f1c93",
        "image": "busybox:latest",
        "imageID": "docker-pullable://busybox@sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678",
        "lastState": {},
        "name": "install",
        "ready": true,
        "restartCount": 0,
        "state": {
          "terminated": {
            "containerID": "docker://9d2aec37255a02a2b13abced64717057eb100dd329bc6b6529211e30595f1c93",
            "exitCode": 0,
            "finishedAt": "2022-01-11T05:50:08Z",
            "reason": "Completed",
            "startedAt": "2022-01-11T05:50:05Z"
          }
        }
      }
    ],
    "phase": "Running",
    "podIP": "172.23.29.31",
    "podIPs": [
      {
        "ip": "172.23.29.31"
      }
    ],
    "qosClass": "BestEffort",
    "startTime": "2022-01-11T05:49:57Z"
  }
}

这样就将yaml格式的文件转为json格式的了。输出到控制台同时保存到了文件。

3.2、json转换为yaml

代码语言:javascript复制
yq eval -P initnginx.json | tee initnginx.yml

执行过程如下:

代码语言:javascript复制
[root@nctest-snap-test-02 practises]# yq eval -P initnginx.json | tee initnginx.yml
apiVersion: v1
kind: Pod
metadata:
  annotations:
    cni.projectcalico.org/podIP: 172.23.29.31/32
    cni.projectcalico.org/podIPs: 172.23.29.31/32
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"initnginx","namespace":"default"},"spec":{"containers":[{"image":"172.20.45.174:81/base/nginx:1.15-alpine","name":"nginx","ports":[{"containerPort":80}],"volumeMounts":[{"mountPath":"/usr/share/nginx/html","name":"workdir"}]}],"dnsPolicy":"Default","initContainers":[{"command":["wget","-O","/work-dir/index.html","https://www.baidu.com"],"image":"busybox","name":"install","volumeMounts":[{"mountPath":"/work-dir","name":"workdir"}]}],"volumes":[{"emptyDir":{},"name":"workdir"}]}}
  creationTimestamp: "2022-01-11T05:49:56Z"
  managedFields:
    - apiVersion: v1
      fieldsType: FieldsV1
      fieldsV1:
        f:metadata:
          f:annotations:
            .: {}
            f:kubectl.kubernetes.io/last-applied-configuration: {}
        f:spec:
          f:containers:
            k:{"name":"nginx"}:
              .: {}
              f:imagePullPolicy: {}
              f:name: {}
              f:ports:
                .: {}
                k:{"containerPort":80,"protocol":"TCP"}:
                  .: {}
                  f:containerPort: {}
                  f:protocol: {}
              f:resources: {}
              f:terminationMessagePath: {}
              f:terminationMessagePolicy: {}
              f:volumeMounts:
                .: {}
                k:{"mountPath":"/usr/share/nginx/html"}:
                  .: {}
                  f:mountPath: {}
                  f:name: {}
          f:dnsPolicy: {}
          f:enableServiceLinks: {}
          f:initContainers:
            .: {}
            k:{"name":"install"}:
              .: {}
              f:command: {}
              f:imagePullPolicy: {}
              f:name: {}
              f:resources: {}
              f:terminationMessagePath: {}
              f:terminationMessagePolicy: {}
              f:volumeMounts:
                .: {}
                k:{"mountPath":"/work-dir"}:
                  .: {}
                  f:mountPath: {}
                  f:name: {}
          f:restartPolicy: {}
          f:schedulerName: {}
          f:securityContext: {}
          f:terminationGracePeriodSeconds: {}
          f:volumes:
            .: {}
            k:{"name":"workdir"}:
              .: {}
              f:emptyDir: {}
              f:name: {}
      manager: kubectl-client-side-apply
      operation: Update
      time: "2022-01-11T05:49:56Z"
    - apiVersion: v1
      fieldsType: FieldsV1
      fieldsV1:
        f:metadata:
          f:annotations:
            f:cni.projectcalico.org/podIP: {}
            f:cni.projectcalico.org/podIPs: {}
      manager: calico
      operation: Update
      time: "2022-01-11T05:49:58Z"
    - apiVersion: v1
      fieldsType: FieldsV1
      fieldsV1:
        f:spec:
          f:containers:
            k:{"name":"nginx"}:
              f:image: {}
          f:initContainers:
            k:{"name":"install"}:
              f:image: {}
      manager: kubectl-edit
      operation: Update
      time: "2022-01-11T06:16:59Z"
    - apiVersion: v1
      fieldsType: FieldsV1
      fieldsV1:
        f:status:
          f:conditions:
            k:{"type":"ContainersReady"}:
              .: {}
              f:lastProbeTime: {}
              f:lastTransitionTime: {}
              f:status: {}
              f:type: {}
            k:{"type":"Initialized"}:
              .: {}
              f:lastProbeTime: {}
              f:lastTransitionTime: {}
              f:status: {}
              f:type: {}
            k:{"type":"Ready"}:
              .: {}
              f:lastProbeTime: {}
              f:lastTransitionTime: {}
              f:status: {}
              f:type: {}
          f:containerStatuses: {}
          f:hostIP: {}
          f:initContainerStatuses: {}
          f:phase: {}
          f:podIP: {}
          f:podIPs:
            .: {}
            k:{"ip":"172.23.29.31"}:
              .: {}
              f:ip: {}
          f:startTime: {}
      manager: kubelet
      operation: Update
      time: "2022-01-11T06:18:21Z"
  name: initnginx
  namespace: default
  resourceVersion: "11304453"
  selfLink: /api/v1/namespaces/default/pods/initnginx
  uid: 8f379d58-8bba-4d2b-8b1e-c9fb9f36305b
spec:
  containers:
    - image: 172.20.45.174:81/base/nginx:1.15-alpine
      imagePullPolicy: IfNotPresent
      name: nginx
      ports:
        - containerPort: 80
          protocol: TCP
      resources: {}
      terminationMessagePath: /dev/termination-log
      terminationMessagePolicy: File
      volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: workdir
        - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
          name: default-token-8ss8s
          readOnly: true
  dnsPolicy: Default
  enableServiceLinks: true
  initContainers:
    - command:
        - wget
        - -O
        - /work-dir/index.html
        - https://www.baidu.com
      image: busybox:1.0
      imagePullPolicy: Always
      name: install
      resources: {}
      terminationMessagePath: /dev/termination-log
      terminationMessagePolicy: File
      volumeMounts:
        - mountPath: /work-dir
          name: workdir
        - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
          name: default-token-8ss8s
          readOnly: true
  nodeName: 172.20.59.57
  preemptionPolicy: PreemptLowerPriority
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
    - effect: NoExecute
      key: node.kubernetes.io/not-ready
      operator: Exists
      tolerationSeconds: 300
    - effect: NoExecute
      key: node.kubernetes.io/unreachable
      operator: Exists
      tolerationSeconds: 300
  volumes:
    - emptyDir: {}
      name: workdir
    - name: default-token-8ss8s
      secret:
        defaultMode: 420
        secretName: default-token-8ss8s
status:
  conditions:
    - lastProbeTime: null
      lastTransitionTime: "2022-01-11T05:50:08Z"
      status: "True"
      type: Initialized
    - lastProbeTime: null
      lastTransitionTime: "2022-01-11T06:18:21Z"
      status: "True"
      type: Ready
    - lastProbeTime: null
      lastTransitionTime: "2022-01-11T06:18:21Z"
      status: "True"
      type: ContainersReady
    - lastProbeTime: null
      lastTransitionTime: "2022-01-11T05:49:57Z"
      status: "True"
      type: PodScheduled
  containerStatuses:
    - containerID: docker://d3d78a9b51867e9fe5ea1844c1e2f80776efdbc25a6fba97a5e0485daf92ff7d
      image: 172.20.45.174:81/base/nginx:1.15-alpine
      imageID: docker-pullable://172.20.45.174:81/base/nginx@sha256:478a73bcec93acc3e814ddd6fb2f95c6f6b4b0d0f168a4feaa039513d260a5d9
      lastState:
        terminated:
          containerID: docker://54e79b997c6cae2d39af27e3ede4f9e7e184c34f2b7b94110cd94ea02cd82356
          exitCode: 143
          finishedAt: "2022-01-11T06:16:59Z"
          reason: Error
          startedAt: "2022-01-11T06:09:25Z"
      name: nginx
      ready: true
      restartCount: 2
      started: true
      state:
        running:
          startedAt: "2022-01-11T06:18:21Z"
  hostIP: 172.20.59.57
  initContainerStatuses:
    - containerID: docker://9d2aec37255a02a2b13abced64717057eb100dd329bc6b6529211e30595f1c93
      image: busybox:latest
      imageID: docker-pullable://busybox@sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
      lastState: {}
      name: install
      ready: true
      restartCount: 0
      state:
        terminated:
          containerID: docker://9d2aec37255a02a2b13abced64717057eb100dd329bc6b6529211e30595f1c93
          exitCode: 0
          finishedAt: "2022-01-11T05:50:08Z"
          reason: Completed
          startedAt: "2022-01-11T05:50:05Z"
  phase: Running
  podIP: 172.23.29.31
  podIPs:
    - ip: 172.23.29.31
  qosClass: BestEffort
  startTime: "2022-01-11T05:49:57Z"

这样就将json格式的文件转换为yaml格式。

ok以上就是yq工具的常见用法。

0 人点赞