Ansible:如何从 openshift 'oc' 命令响应中获取结构化 YAML?

问题描述 投票:0回答:1

Ansible 在 Jenkins 上启动,我想连接到 OpenShift 并获取当前实体(例如,路由)。

目标:获得一个文件,所有实体由

---
.

分隔

我尝试使用

to_yaml
from_yaml
等与
oc --server
shell 命令,但到目前为止没有任何效果。

获取嵌套部分最简单的方法是什么?从

items
中获取
stdout
的最佳方法是什么? 还是完全走其他路更好?例如,不使用
shell
模块?

目前我试过这个:

  - name: Get K8s items
    register: getItems
    shell: oc --server {{ os_project.host }} --namespace {{ os_project.project }} --token {{ os_token }} get -o yaml route.route.openshift.io

  - name: Write united config to file
    copy:
      content: "{{ getItems.stdout }}"
      dest: processed_templates/os_backup/os_dump_{{ version }}.yml

我得到以下输出:

apiVersion: v1
items:
- apiVersion: route.openshift.io/v1
  kind: Route
  metadata:
    annotations:
      haproxy.router.openshift.io/balance: roundrobin
    <...>
      kind: Service
      name: ingressgateway-ift-shared-https-svc
      weight: 100
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""

我怎样才能得到这样的输出:

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  annotations:
    haproxy.router.openshift.io/balance: roundrobin
  <...>
    kind: Service
    name: ingressgateway-ift-shared-https-svc
    weight: 100
jenkins ansible yaml openshift
1个回答
0
投票

根据提供的信息,一个最小的示例剧本

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    getItems:
      stdout:
        apiVersion: v1
        items:
        - apiVersion: route.openshift.io/v1
          kind: Route
          metadata:
            annotations:
              haproxy.router.openshift.io/balance: roundrobin
              kind: Service
              name: ingressgateway-ift-shared-https-svc
              weight: 100
        kind: List
        metadata:
          resourceVersion: ""
          selfLink: ""

  tasks:

  - debug:
      msg: "{{ getItems.stdout['items'] | first }}"

结果为请求的输出

TASK [debug] ******************************************
ok: [localhost] =>
  msg:
    apiVersion: route.openshift.io/v1
    kind: Route
    metadata:
      annotations:
        haproxy.router.openshift.io/balance: roundrobin
        kind: Service
        name: ingressgateway-ift-shared-https-svc
        weight: 100
© www.soinside.com 2019 - 2024. All rights reserved.