来自stdout的ansible解析文本字符串 [英] ansible parse text string from stdout

查看:752
本文介绍了来自stdout的ansible解析文本字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是解析和解析标准输出.我需要从ansible播放中捕获stdout,并将此输出解析为stdout中的特定子字符串,然后保存到var中.我的特定用例如下

- shell: "vault.sh --keystore EAP_HOME/vault/vault.keystore |
          --keystore-password vault22 --alias vault --vault-block |
          vb --attribute password --sec-attr 0penS3sam3 --enc-dir |
          EAP_HOME/vault/ --iteration 120 --salt 1234abcd" 
  register: results
  become: true

这将产生以下一行输出,目标是捕获jboss Vault生成的掩码密钥并将其保存在ansible var中,以便我可以使用它来配置standalone.xml模板:

vault-option name="KEYSTORE_PASSWORD" value="MASK-5dOaAVafCSd"/>

我需要一种使用可能的正则表达式解析此字符串并将"MASK-5dOaAVafCSd"子字符串保存到使用set_facts模块或任何其他ansible模块的ansible var中的方法.

当前我的代码如下

#example stdout
results: vault-option name=\"KEYSTORE_PASSWORD\" value=\"MASK-5dOaAVafCSd\"/>
- name: JBOSS_VAULT:define keystore password masked value variable
    set_fact:
    masked_value: |
       "{{ results.stdout | 
        regex_replace('^.+(MASK-.+?)\\.+','\\\1') }}"

此代码将masked_value定义为results.stdout,而不是预期的捕获组.

解决方案

您非常亲密.我建议您使用 regex101.com 来测试正则表达式.

这是我的解决方案:

---
- hosts: localhost
  gather_facts: no
  tasks:
    - shell: echo 'vault-option name="KEYSTORE_PASSWORD" value="MASK-5dOaAVafCSd"'
      register: results
    - set_fact:
        myvalue: "{{ results.stdout | regex_search(regexp,'\\1') }}"
      vars:
        regexp: 'value=\"([^"]+)'
    - debug:
        var: myvalue

结果:

ok: [localhost] => {
    "myvalue": [
        "MASK-5dOaAVafCSd"
    ]
}

更新:

regex_search返回找到的匹配项的列表,因此仅用于第一个用途:

{{ results.stdout | regex_search(regexp,'\\1') | first }}

My problem is with ansible and parsing stdout. I need to capture the stdout from an ansible play and parse this output for a specific substring within stdout and save into a var. My specific use case is below

- shell: "vault.sh --keystore EAP_HOME/vault/vault.keystore |
          --keystore-password vault22 --alias vault --vault-block |
          vb --attribute password --sec-attr 0penS3sam3 --enc-dir |
          EAP_HOME/vault/ --iteration 120 --salt 1234abcd" 
  register: results
  become: true

This generates an output with the following line, the goal is to capture the masked key that jboss vault generates and save that in an ansible var so I can use it to configure the standalone.xml template:

vault-option name="KEYSTORE_PASSWORD" value="MASK-5dOaAVafCSd"/>

I need a way parse this string with possibly regex and save the "MASK-5dOaAVafCSd" substring into an ansible var using set_facts module or any other ansible module.

Currently my code looks like this

#example stdout
results: vault-option name=\"KEYSTORE_PASSWORD\" value=\"MASK-5dOaAVafCSd\"/>
- name: JBOSS_VAULT:define keystore password masked value variable
    set_fact:
    masked_value: |
       "{{ results.stdout | 
        regex_replace('^.+(MASK-.+?)\\.+','\\\1') }}"

This code is defining masked_value as the results.stdout, not the expected capture group.

解决方案

You are very close. I advice you to use regex101.com to test regular expressions.

Here is my solution:

---
- hosts: localhost
  gather_facts: no
  tasks:
    - shell: echo 'vault-option name="KEYSTORE_PASSWORD" value="MASK-5dOaAVafCSd"'
      register: results
    - set_fact:
        myvalue: "{{ results.stdout | regex_search(regexp,'\\1') }}"
      vars:
        regexp: 'value=\"([^"]+)'
    - debug:
        var: myvalue

result:

ok: [localhost] => {
    "myvalue": [
        "MASK-5dOaAVafCSd"
    ]
}

Update:

regex_search returns a list of found matches, so to get only first one use:

{{ results.stdout | regex_search(regexp,'\\1') | first }}

这篇关于来自stdout的ansible解析文本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆