首页 > 解决方案 > 我可以浏览 Ansible 的 CICS 资源之间的关系吗?

问题描述

我已经开始使用Ansible Galaxy 上的CICS 集合。我一直在用它来做一些事情,比如请求单个资源,但是我看不到像在 CICS 的 CMCI GraphQL API 中那样遍历关系的方法。

合集有这个能力吗?

标签: ansiblegraphqlcics

解决方案


不,CICS 集合无法使用 CMCI GraphQL API。它纯粹使用 CMCI REST API,它只处理一种资源类型。

但是,这并不意味着您不能将 CMCI GraphQL API 用于 Ansible 的 CICS!该 API 通常更易于理解,您可以使用 GraphiQL 构建查询,而无需特殊的集合。然后,您可以使用 Ansible 的内置uri模块发送 GraphQL 请求并从响应中获取信息。

简单查询

例如,这里有一个简单的剧本和随附的 GraphQL 查询,用于获取 CICSplex 及其 CICS 区域的结构,并打印出结果。重要的部分是query添加到body请求中的键。

playbook1.yml

- hosts: localhost

  tasks:

  - name: Get data from CICS
    register: result
    uri:
      url: https://my.cicsplex:12345/graphql/
      method: POST
      body_format: json
      body:
        query: '{{ lookup("file", "./queries/topology_query.graphql") }}' #  GraphQL query is passed here

  - name: Print out response
    debug:
      msg: '{{ result.json }}'

queries/topology_query.graphql

{
  cicsplexes {
    name
    regions {
      name
    }
  }
}

向查询中添加变量

当然,您可能希望参数化查询。您可以使用 Ansible 中的 Jinja 模板来做到这一点。这是一个剧本和随附的 GraphQL 查询,用于在所有连接的 CICSplex中查找特定区域名称(名为MYREGION,在 中)。vars

playbook2.yml

- hosts: localhost

  vars:
    regionName: MYREGION

  tasks:
  - name: Get data from CICS
    register: result
    uri:
      url: https://my.cicsplex:12345/graphql/
      method: POST
      body_format: json
      body:
        query: '{{ lookup("template", "./templates/single_region_query.graphql.j2") }}' # template instead of plain file

  - name: Print out response
    debug:
      msg: '{{ result.json }}'

templates/single_region_query.graphql.j2

{
  cicsplexes {
    name
    region(name: "{{ regionName }}") { # this variable will be expanded by Ansible
      name
    }
  }
}

向查询中添加变量 - 更好的方法

但是,我对模板有点怀疑。当 vars 从其他地方进来时,似乎很容易出现注入问题,无论是恶意的还是偶然的!即使写了上面的例子,我也错过了正确的引号。所以我更喜欢使用 GraphQL 的内置变量支持来更好地清理变量。

在 CICS 的 CMCI GraphQL API 中,您可以使用variables您发送给 CICS 的请求正文中的密钥以及现有query密钥来提供变量。

在这里,您会看到在正文中提供的变量,然后在 GraphQL 查询中regionName看到相同的变量(称为)。$regionName

playbook3.yml

- hosts: localhost

  vars:
    regionName: MYREGION

  tasks:

  - name: Get data from plex
    register: result
    uri:
      url: https://my.cicsplex:12345/graphql/
      method: POST
      body_format: json
      body:
        query: '{{ lookup("file", "./queries/single_region_query.graphql") }}' # plain file, not template
        variables:
          regionName: "{{ regionName }}" # variables will get passed to CICS

  - name: Print out response
    debug:
      msg: '{{ result.json }}'

queries/single_region_query.graphql

query searchForRegion ($regionName: String!) { # query declares used variables
  cicsplexes {
    name
    region(name: $regionName) { # GraphQL expands the variable here at query execution time
      name
    }
  }
}

推荐阅读