ansible - Ansible - 通过合并事实创建 JSON 输出
问题描述
我正在尝试从 Ansible 剧本创建 JSON 输出,该输出将通过文件或 RabbitMQ 发送到第 3 方系统。我遇到的问题是创建 JSON 文件/正文以作为输出发送。
环境:
- Ansible:2.10.17
- JSON 输入文件:'无法更改输入文件的格式'
逻辑过程:
- 接收 JSON 输入文件 => 工作
- 将 JSON 输入文件转换为事实 (JSON_input) => 工作
- 搜索 a 目录并找到尚未列出的任何文件夹(存储桶)(var_client_buckets_diff_state_current)=> 工作
- 创建由 [输入文件 + var_client_buckets_diff_state_current] 组成的新事实(为新事实创建 json 文件结构)=> 不起作用
- 将新事实发送给第 3 方 => 尚未尝试
尝试了什么:
尝试使用带有 indicies 的“update_fact”=> 这似乎不起作用,旨在更新事实中的现有变量
试图使用 'set_fact' + [item] => 不是我所期望的
输入文件:
{
"siteIdentifier": "site1",
"configurations": [
{
"tenantName": "patcorp",
"keypairConfigurations": [
{
"accessKey": "key1",
"secretKey": "kjjhgghfdgf765ghdf",
"port": "60008",
"mountPath": "/tmp/mnt/scoutfs/patcorp",
"buckets": [
{
"bucketName": "bucket1",
"settings": {
"status": "OK"
}
},
{
"bucketName": "bucket2",
"settings": {
"status": "PENDING-DELETION"
}
}
]
}
]
},
{
"tenantName": "mattcorp",
"keypairConfigurations": [
{
"accessKey": "key3",
"secretKey": "jhgf785tkjjhg",
"port": "60010",
"mountPath": "/tmp/mnt/scoutfs/mattcorp",
"buckets": [
{
"bucketName": "bucket10",
"settings": {
"status": "OK"
}
},
{
"bucketName": "bucket11",
"settings": {
"status": "PENDING-DELETION"
}
}
]
}
]
}
]
}
剧本:
---
- name: debug_playbook
hosts:
- localhost
gather_facts: true
vars:
var_client_buckets_diff_state_current: ['/tmp/mnt/scoutfs/mattcorp/bucket12', '/tmp/mnt/scoutfs/mattcorp/bucket22', '/tmp/mnt/scoutfs/patcorp/bucket07']
fact_json_updated: []
tasks:
## Find collections path via ansible.cfg
- name: debug_read_collections_path
ansible.builtin.debug:
msg: "{{ lookup('config', 'COLLECTIONS_PATHS') }}"
run_once: true
register: reg_debug_read_collections_path
## Read JSON file to create stdout
- name: cat_scoutam_client_master_assignments_raw
ansible.builtin.shell:
cmd: cat ./files/scoutam_client_master_assignments_2.json
run_once: true
delegate_to: localhost
register: reg_scoutam_client_master_assignments_raw
## Convert JSON input file into fact
- name: fact_scoutam_client_master_assignments
ansible.builtin.set_fact:
fact_scoutam_client_master_assignments: "{{ reg_scoutam_client_master_assignments_raw.stdout | from_json }}"
run_once: true
delegate_to: localhost
# - name: debug_scoutam_client_tenant_ids
# debug:
# msg: "{{ fact_scoutam_client_master_assignments | json_query(jmesquery) }}"
# vars:
# jmesquery: 'configurations[*].tenantName'
# run_once: true
# delegate_to: localhost
# register: reg_debug_scoutam_client_tenant_ids
## Create duplicate fact file with only diffs found from 'var_client_buckets_diff_state_current'
- name: create json file structure for new fact
ansible.builtin.set_fact:
fact_json_new:
siteIdentifier:
configurations:
- {tenantName: "{{ var_tenantName }}", keypairConfigurations: [{accessKey: , secretKey: , port: , mountPath: , buckets: [{bucketName: "{{ var_bucketName }}", settings: {status: }}] }]}
vars:
var_tenantName: "{{ item.split('/')[4] }}" ## take 'item' and find tenant name
var_bucketName: "{{ item.split('/')[5] }}" ## take 'item' and find bucket name
loop: "{{ var_client_buckets_diff_state_current }}" ## loop through buckets not within input file
register: reg_fact_json_new
## Create new JSON file (input file + any new buckets found )
- name: combine fact
ansible.builtin.set_fact:
fact_json_updated: "{{ fact_json_updated + item }}"
loop: "{{ reg_fact_json_new.results | json_query('[*].ansible_facts.fact_json_new') }}"
- name: debug fact - fact_json_updated
debug:
var: fact_json_updated
## End of playbook message
- name: "debug_playbook_{{ ansible_play_name }}_completion"
ansible.builtin.debug:
msg: "Playbook {{ playbook_dir }}/{{ ansible_play_name }} has finished."
...
[为新事实创建 json 文件结构,结合事实] 的输出
TASK [combine fact] ************************************************************************************************************************************************************************************************************
task path: /app/ansible/ansible_linux_playbooks/playbooks/TEST_debug_facts_playbook/debug_playbook.yml:70
Thursday 28 October 2021 10:06:50 -0400 (0:00:00.078) 0:00:01.417 ******
ok: [localhost] => (item={'siteIdentifier': None, 'configurations': [{'tenantName': 'mattcorp', 'keypairConfigurations': [{'accessKey': None, 'secretKey': None, 'port': None, 'mountPath': None, 'buckets': [{'bucketName': 'bucket12', 'settings': {'status': None}}]}]}]}) => {
"ansible_facts": {
"fact_json_updated": [
{
"configurations": [
{
"keypairConfigurations": [
{
"accessKey": null,
"buckets": [
{
"bucketName": "bucket12",
"settings": {
"status": null
}
}
],
"mountPath": null,
"port": null,
"secretKey": null
}
],
"tenantName": "mattcorp"
}
],
"siteIdentifier": null
}
]
},
"ansible_loop_var": "item",
"changed": false,
"item": {
"configurations": [
{
"keypairConfigurations": [
{
"accessKey": null,
"buckets": [
{
"bucketName": "bucket12",
"settings": {
"status": null
}
}
],
"mountPath": null,
"port": null,
"secretKey": null
}
],
"tenantName": "mattcorp"
}
],
"siteIdentifier": null
}
}
ok: [localhost] => (item={'siteIdentifier': None, 'configurations': [{'tenantName': 'mattcorp', 'keypairConfigurations': [{'accessKey': None, 'secretKey': None, 'port': None, 'mountPath': None, 'buckets': [{'bucketName': 'bucket22', 'settings': {'status': None}}]}]}]}) => {
"ansible_facts": {
"fact_json_updated": [
{
"configurations": [
{
"keypairConfigurations": [
{
"accessKey": null,
"buckets": [
{
"bucketName": "bucket12",
"settings": {
"status": null
}
}
],
"mountPath": null,
"port": null,
"secretKey": null
}
],
"tenantName": "mattcorp"
}
],
"siteIdentifier": null
},
{
"configurations": [
{
"keypairConfigurations": [
{
"accessKey": null,
"buckets": [
{
"bucketName": "bucket22",
"settings": {
"status": null
}
}
],
"mountPath": null,
"port": null,
"secretKey": null
}
],
"tenantName": "mattcorp"
}
],
"siteIdentifier": null
}
]
},
"ansible_loop_var": "item",
"changed": false,
"item": {
"configurations": [
{
"keypairConfigurations": [
{
"accessKey": null,
"buckets": [
{
"bucketName": "bucket22",
"settings": {
"status": null
}
}
],
"mountPath": null,
"port": null,
"secretKey": null
}
],
"tenantName": "mattcorp"
}
],
"siteIdentifier": null
}
}
ok: [localhost] => (item={'siteIdentifier': None, 'configurations': [{'tenantName': 'patcorp', 'keypairConfigurations': [{'accessKey': None, 'secretKey': None, 'port': None, 'mountPath': None, 'buckets': [{'bucketName': 'bucket07', 'settings': {'status': None}}]}]}]}) => {
"ansible_facts": {
"fact_json_updated": [
{
"configurations": [
{
"keypairConfigurations": [
{
"accessKey": null,
"buckets": [
{
"bucketName": "bucket12",
"settings": {
"status": null
}
}
],
"mountPath": null,
"port": null,
"secretKey": null
}
],
"tenantName": "mattcorp"
}
],
"siteIdentifier": null
},
{
"configurations": [
{
"keypairConfigurations": [
{
"accessKey": null,
"buckets": [
{
"bucketName": "bucket22",
"settings": {
"status": null
}
}
],
"mountPath": null,
"port": null,
"secretKey": null
}
],
"tenantName": "mattcorp"
}
],
"siteIdentifier": null
},
{
"configurations": [
{
"keypairConfigurations": [
{
"accessKey": null,
"buckets": [
{
"bucketName": "bucket07",
"settings": {
"status": null
}
}
],
"mountPath": null,
"port": null,
"secretKey": null
}
],
"tenantName": "patcorp"
}
],
"siteIdentifier": null
}
]
},
"ansible_loop_var": "item",
"changed": false,
"item": {
"configurations": [
{
"keypairConfigurations": [
{
"accessKey": null,
"buckets": [
{
"bucketName": "bucket07",
"settings": {
"status": null
}
}
],
"mountPath": null,
"port": null,
"secretKey": null
}
],
"tenantName": "patcorp"
}
],
"siteIdentifier": null
}
}
TASK [debug fact - reg_fact_json_updated] **************************************************************************************************************************************************************************************
task path: /app/ansible/ansible_linux_playbooks/playbooks/TEST_debug_facts_playbook/debug_playbook.yml:77
Thursday 28 October 2021 10:06:50 -0400 (0:00:00.100) 0:00:01.518 ******
ok: [localhost] => {
"fact_json_updated": [
{
"configurations": [
{
"keypairConfigurations": [
{
"accessKey": null,
"buckets": [
{
"bucketName": "bucket12",
"settings": {
"status": null
}
}
],
"mountPath": null,
"port": null,
"secretKey": null
}
],
"tenantName": "mattcorp"
}
],
"siteIdentifier": null
},
{
"configurations": [
{
"keypairConfigurations": [
{
"accessKey": null,
"buckets": [
{
"bucketName": "bucket22",
"settings": {
"status": null
}
}
],
"mountPath": null,
"port": null,
"secretKey": null
}
],
"tenantName": "mattcorp"
}
],
"siteIdentifier": null
},
{
"configurations": [
{
"keypairConfigurations": [
{
"accessKey": null,
"buckets": [
{
"bucketName": "bucket07",
"settings": {
"status": null
}
}
],
"mountPath": null,
"port": null,
"secretKey": null
}
],
"tenantName": "patcorp"
}
],
"siteIdentifier": null
}
]
}
预期输出 JSON:
{
"siteIdentifier": "mas1-sos07",
"configurations": [
{
"tenantName": "patcorp",
"keypairConfigurations": [
{
"accessKey": "key1",
"secretKey": "kjjhgghfdgf765ghdf",
"port": "60008",
"mountPath": "/tmp/mnt/scoutfs/patcorp",
"buckets": [
{
"bucketName": "bucket1",
"settings": {
"status": "OK"
}
},
{
"bucketName": "bucket2",
"settings": {
"status": "PENDING-DELETION"
}
},
{
"bucketName": "bucket07",
"settings": {
"status": null
}
}
]
}
]
},
{
"tenantName": "mattcorp",
"keypairConfigurations": [
{
"accessKey": "key3",
"secretKey": "jhgf785tkjjhg",
"port": "60010",
"mountPath": "/tmp/mnt/scoutfs/mattcorp",
"buckets": [
{
"bucketName": "bucket10",
"settings": {
"status": "OK"
}
},
{
"bucketName": "bucket11",
"settings": {
"status": "PENDING-DELETION"
}
},
{
"bucketName": "bucket12",
"settings": {
"status": null
}
},
{
"bucketName": "bucket22",
"settings": {
"status": null
}
}
]
}
]
}
]
}
解决方案
推荐阅读
- c# - 使用不同的格式参数格式化金额
- python - 视差滚动弄乱了射弹的位置
- sql - SQL 中 TOTAL 的百分比
- serilog - 如何将磁盘上的 Serilog 文件接收器写入的数据推送到另一个 Serilog 接收器?
- wordpress - $ 不是一个函数 - 如何解决它
- python - 如何使用 Selenium python 录制视频
- java - Android 中的 Apache Velocity(“无法找到资源”)
- javascript - 更改电子商务购物车的数量时更改产品价格
- python - 多变量 LSTM 输入形状
- sql - 使用 to_char 的 Postgresql 小数点