Skip to content

Instantly share code, notes, and snippets.

@zeitounator
Created May 9, 2025 13:57
Show Gist options
  • Save zeitounator/4bbc95ffb5954c75e73cbd6e95ea632c to your computer and use it in GitHub Desktop.
Save zeitounator/4bbc95ffb5954c75e73cbd6e95ea632c to your computer and use it in GitHub Desktop.
$ cat reg.yaml
- hosts: localhost
gather_facts: false
vars:
myconfig:
kindA:
regexp: "^exampleA.+$"
kindB:
regexp: "^exampleB.+$"
kindC:
regexp: "^exampleC.+$"
tasks:
- ansible.builtin.set_fact:
regex_result: "{{ regex_result | default({}) | combine({ item.key: is_matching }) }}"
vars:
is_matching: "{{ my_current_obj.description | regex_search(item.value.regexp) != None }}"
loop: "{{ lookup('dict', myconfig) }}"
- ansible.builtin.debug:
var: regex_result
$ ansible-playbook -e '{"my_current_obj": {"description": "exampleA rocks"}}' reg.yaml
PLAY [localhost] *******************************************************************************************************************************************************************************************
TASK [ansible.builtin.set_fact] ****************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'kindA', 'value': {'regexp': '^exampleA.+$'}})
ok: [localhost] => (item={'key': 'kindB', 'value': {'regexp': '^exampleB.+$'}})
ok: [localhost] => (item={'key': 'kindC', 'value': {'regexp': '^exampleC.+$'}})
TASK [ansible.builtin.debug] *******************************************************************************************************************************************************************************
ok: [localhost] => {
"regex_result": {
"kindA": true,
"kindB": false,
"kindC": false
}
}
PLAY RECAP *************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$ ansible-playbook -e '{"my_current_obj": {"description": "exampleB is the best"}}' reg.yaml
PLAY [localhost] *******************************************************************************************************************************************************************************************
TASK [ansible.builtin.set_fact] ****************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'kindA', 'value': {'regexp': '^exampleA.+$'}})
ok: [localhost] => (item={'key': 'kindB', 'value': {'regexp': '^exampleB.+$'}})
ok: [localhost] => (item={'key': 'kindC', 'value': {'regexp': '^exampleC.+$'}})
TASK [ansible.builtin.debug] *******************************************************************************************************************************************************************************
ok: [localhost] => {
"regex_result": {
"kindA": false,
"kindB": true,
"kindC": false
}
}
PLAY RECAP *************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$ ansible-playbook -e '{"my_current_obj": {"description": "exampleC rules"}}' reg.yaml
PLAY [localhost] *******************************************************************************************************************************************************************************************
TASK [ansible.builtin.set_fact] ****************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'kindA', 'value': {'regexp': '^exampleA.+$'}})
ok: [localhost] => (item={'key': 'kindB', 'value': {'regexp': '^exampleB.+$'}})
ok: [localhost] => (item={'key': 'kindC', 'value': {'regexp': '^exampleC.+$'}})
TASK [ansible.builtin.debug] *******************************************************************************************************************************************************************************
ok: [localhost] => {
"regex_result": {
"kindA": false,
"kindB": false,
"kindC": true
}
}
PLAY RECAP *************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment