Home > other >  Getting regex value from ansible stdout
Getting regex value from ansible stdout

Time:11-12

I want to extract an boolean value from ansible command stdout:

 - name: Get Status
  ansible.builtin.command:
    cmd: "vault status"
  register: vault_status
  ignore_errors: true

- ansible.builtin.set_fact:
    vault_sealed: "{{ (vault_status.stdout | regex_search(regexp,'\\1')) == 'true' }} "
  vars:
    regexp: "Sealed\\s (. )\\n"

It brings that output

TASK [Get Status] **************************************************************
task path: /ansible/tasks/vault_setup.yml:124
fatal: [vault_instance]: FAILED! => {"changed": true, "cmd": ["vault", "status"], "delta": "0:00:00.071161", "end": "2022-11-11 21:32:06.405020", "msg": "non-zero return code", "rc": 2, "start": "2022-11-11 21:32:06.333859", "stderr": "", "stderr_lines": [], "stdout": "Key                Value\n---                -----\nSeal Type          shamir\nInitialized        true\nSealed             true\nTotal Shares       6\nThreshold          3\nUnseal Progress    1/3\nUnseal Nonce       2e079ec7-772f-7811-801e-c098c68c0400\nVersion            1.12.1\nBuild Date         2022-10-27T12:32:05Z\nStorage Type       file\nHA Enabled         false", "stdout_lines": ["Key                Value", "---                -----", "Seal Type          shamir", "Initialized        true", "Sealed             true", "Total Shares       6", "Threshold          3", "Unseal Progress    1/3", "Unseal Nonce       2e079ec7-772f-7811-801e-c098c68c0400", "Version            1.12.1", "Build Date         2022-10-27T12:32:05Z", "Storage Type       file", "HA Enabled         false"]}
...ignoring

TASK [ansible.builtin.set_fact] ************************************************
task path: /ansible/tasks/vault_setup.yml:130
ok: [vault_instance] => {"ansible_facts": {"vault_sealed": "False "}, "changed": false}

Can anybody tell me, how to get the vault_sealed to the right value "true" ?

Thanks guys !

CodePudding user response:

In case you want to check if there is a match for a string, with the regex_search filter, you don't need any parameter other than the regex.

To determine the end of line, rather use the option multiline=True and end your regex with a $, if you want to search from the beginning of the line, start with a ^.

A successful or unsuccessful matching can be determined with is not none or is none.
In your case, to determine the successful matching of the regex, you have to use is not none.

Your task should work like this:

- ansible.builtin.set_fact:
    vault_sealed: "{{ vault_status.stdout | regex_search(regexp, multiline=True) is not none }}"
  vars:
    regexp: "^Sealed\\s . $"

If you just want to detect the beginning of the line with the word Sealed followed by a space, you can also write your regex like this: "^Sealed\\s"

If you want to determine whether the word true follows Sealed, then you can choose the regex as: "^Sealed\\s true$"
Your task like this:

- ansible.builtin.set_fact:
    vault_sealed: "{{ vault_status.stdout | regex_search(regexp, multiline=True) is not none }}"
  vars:
    regexp: "^Sealed\\s true$"

Note: You have to make sure that you don't have a space after your jinja expression (}} "), so it should look like this: }}". Otherwise the result will be interpreted as a string instead of a truth value.


Example Task for string starting with Sealed :

- debug:
    msg: "{{ vault_status.stdout | regex_search(regexp, multiline=True) is not none }}"
  vars:
    regexp: "^Sealed\\s . $"
    vault_status:
      stdout: "Sealed some thing."

Result:

TASK [debug] ***************************
ok: [localhost] => {
    "msg": true
}

Example Tasks for string Sealed true:

- debug:
    msg: "{{ vault_status.stdout | regex_search(regexp, multiline=True) is not none }}"
  vars:
    regexp: "^Sealed\\s true$"
    vault_status:
      stdout: "{{ item }}"
  with_items:
    - "Sealed   true"
    - "Sealed true"
    - "Sealed false"
    - "Some error!"

Result:

TASK [debug] ***********************************************
ok: [localhost] => (item=Sealed   true) => {
    "msg": true
}
ok: [localhost] => (item=Sealed true) => {
    "msg": true
}
ok: [localhost] => (item=Sealed false) => {
    "msg": false
}
ok: [localhost] => (item=Some error!) => {
    "msg": false
}
  • Related