0

i am trying to filter all the strings which contains "RegButton-" from the below ansible facts and use the output as list of items in the next play. trying to use json_query filter but it is failing with below error

ansible fact

{
    "ansible_facts": {
        "srcgrpname": [
            "RegButton-48773",
            "test_vio",
            "RegButton-23395",
            "RegButton-520859",
            "RegButton-743141",
            "RegButton-297578",
            "RegButton-186156"
        ]
    },
    "changed": false
}

playbook entry

  - name: "Filter Regbutton policy Names"
    set_fact:
      srcgrpname2: "{{ resultid1 | json_query(query) }}"
    vars:
        query: "ansible_facts.srcgrpname[?contains(@, 'RegButton-') == `true`]"

Error that i am receiving.

{
    "msg": "JMESPathError in json_query filter plugin:\nIn function contains(), invalid type for value: RegButton-48773, expected one of: ['array', 'string'], received: \"unknown\"",
    "_ansible_no_log": false
} 
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
mikeraj2019
  • 39
  • 2
  • 10

1 Answers1

0

It's possible to use select and regex. For example the tasks below

- set_fact:
    srcgrpname2: "{{ ansible_facts.srcgrpname|
                     select('regex', '^RegButton-(.*)$')|
                     list }}"
- debug:
    var: srcgrpname2

give

"srcgrpname2": [
    "RegButton-48773", 
    "RegButton-23395", 
    "RegButton-520859", 
    "RegButton-743141", 
    "RegButton-297578", 
    "RegButton-186156"
]


Notes
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63