gpt4 book ai didi

python - 在 Ansible 中将 Jboss (WildFly) cli 输出转换为 json

转载 作者:太空宇宙 更新时间:2023-11-04 09:49:07 24 4
gpt4 key购买 nike

我正在努力通过其 cli 将我的部署过程自动化到 WildFly。 cli 本身不是幂等的,所以如果我想使用 ansible 添加一些服务器属性,它只是在它已经存在的情况下抛出一个错误(官方 cli 文档只提供 3 种方法添加、读取、删除 https://docs.jboss.org/author/display/WFLY10/CLI+Recipes - 没有替换或强制添加)。所以我需要先创造一些条件,最简单的就是ready(just if exists):

Ansible 代码:

- name: Check Server Runtime properties
shell: ./jboss-cli.sh --commands="connect {{ wf_server }},/core-service=platform-mbean/type=runtime:read-attribute(name=system-properties)" $AUTH_DATA
args:
chdir: "{{ wf_path }}/bin"
register: srp_chk
- name: Configure Some properties
shell: ./jboss-cli.sh --commands="connect {{ wf_server }},/system-property={{ item.mkey }}:add(value=\"{{ item.mval }}\")" $AUTH_DATA
args:
chdir: "{{ wf_path }}/bin"
when: "not '{{ item.mkey }}' in srp_chk.stdout"
with_items:
- { mkey: some.property1, mval: "{{ some_value1 }}" }
- { mkey: some.property2, mval: "{{ some_value2 }}" }

在我需要更改 some_value1 或 some_value2 之前,它工作正常。在那种情况下,如您所知,我需要删除属性并重新创建。

所以我们几乎接近我的问题:)

我知道这些值可以在 srp_chk.stdout 中找到,我可以为 ex 创建相同的检查:

when: "not '{{ item.mval }}' in srp_chk.stdout" 

但是万一我有更复杂的结构并且我需要在 Ansible 中比较两个 json 呢?

这就是我的问题开始的地方 :))

在我的示例 srp_chk.stdout 中,Jboss cli 以 Json 格式输出,但在一行中,不幸的是,当我尝试 Jinja2 过滤器“from_json”时,它给出了错误:

the field 'args' has an invalid value ([]), and could not be converted to an dict

“to_json”和“to_nice_json”给出了一些奇怪的不可读数据和错误:

template error while templating string: expected token 'end of print statement', got 'outcome'.

“结果”- 是我的字符串的内容。

所以,我有变量 srp_chk.stdout_lines,它在 json 格式方面更具可读性,并且有这样的东西:

"stdout_lines": [
"{",
" \"outcome\" => \"success\",",
" \"result\" => {",
" \"PID\" => \"76462\",",
" \"[Standalone]\" => \"\",",
" \"awt.toolkit\" => \"sun.awt.X11.XToolkit\",",
" \"some.property1\" => \"some.value1\",",
" \"some.property2\" => \"some.value2\",",

等但它的列表。

问题:如何将此列表转换为 json 数据,以便在 Ansible 中我可以从变量“srp_chk.stdout_lines.result.some.property1”中获取“some.value1”?

附言请不要建议总是删除和添加属性,因为在每次部署时我都需要重新加载我的服务器 - 当我们谈论 PROD 时这是非常糟糕的情况。

最佳答案

下一版本的 WildFly(又名 12)将支持输出到 JSON https://issues.jboss.org/browse/WFCORE-3363

关于python - 在 Ansible 中将 Jboss (WildFly) cli 输出转换为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48580822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com