0

Facing an issue with ansible when condition. The register returns a json array list but 'when' condition won't work. Will explain what i am facing here:

First i register the directories:

- name: check if log dir exists
  win_stat:
    path: some\path
  register: register_some_dir

This is the register output:

"changed": false,
"register_some_dir": {
    "msg": "All items completed",
    "changed": false,
    "results": [
        {
            "_ansible_parsed": true,
            "stat": {
                "isdir": true,
                "isarchive": false,
                "exists": true,
                "isreadonly": false,
                "creationtime": 1535985185.638003,
                "isjunction": false,
                "lastaccesstime": 1566915269.0181007,
                "owner": "BUILTIN\\Administrators",
                "nlink": 1,
                "isreg": false,
                "lastwritetime": 1566915269.0181007,
                "islnk": false,
                "attributes": "Directory",
                "path": "f:\\some\\paths",
                "filename": "logging",
                "ishidden": false,
                "isshared": false,
                "hlnk_targets": [],
                "size": 4619
            },
            "changed": false,
            "_ansible_no_log": false,
            "item": "Folder_One",
            "_ansible_item_result": true,
            "failed": false,
            "_ansible_ignore_errors": null,
            "_ansible_item_label": "Folder_One"
        },
        {
            "_ansible_parsed": true,
            "stat": {
                "isdir": true,
                "isarchive": false,
                "exists": true,
                "isreadonly": false,
                "creationtime": 1535985188.0229728,
                "isjunction": false,
                "lastaccesstime": 1568382094.0390675,
                "owner": "BUILTIN\\Administrators",
                "nlink": 1,
                "isreg": false,
                "lastwritetime": 1568382094.0390675,
                "islnk": false,
                "attributes": "Directory",
                "path": "f:\\some\\other\\paths",
                "filename": "logging",
                "ishidden": false,
                "isshared": false,
                "hlnk_targets": [],
                "size": 248191373
            },
            "changed": false,
            "_ansible_no_log": false,
            "item": "Folder_Two",
            "_ansible_item_result": true,
            "failed": false,
            "_ansible_ignore_errors": null,
            "_ansible_item_label": "Folder_Two"
        },
        {
            "_ansible_parsed": true,
            "stat": {
                "isdir": true,
                "isarchive": false,
                "exists": true,
                "isreadonly": false,
                "creationtime": 1535985191.886996,
                "isjunction": false,
                "lastaccesstime": 1566829433.7600543,
                "owner": "BUILTIN\\Administrators",
                "nlink": 1,
                "isreg": false,
                "lastwritetime": 1566829433.7600543,
                "islnk": false,
                "attributes": "Directory",
                "path": "f:\\another\\paths",
                "filename": "logging",
                "ishidden": false,
                "isshared": false,
                "hlnk_targets": [],
                "size": 0
            },
            "changed": false,
            "_ansible_no_log": false,
            "item": "Folder_Three",
            "_ansible_item_result": true,
            "failed": false,
            "_ansible_ignore_errors": null,
            "_ansible_item_label": "Folder_Three"
        }
    ]
},
"_ansible_verbose_always": true,
"_ansible_no_log": false

With json filter i can filter the path like:

- set_fact:
    log_paths: "{{ register_some_dir | json_query('results[*].stat.path') }}"
  when: register_some_dir.results[*].stat.exists == true

set_fact module returns the path perfectly whithout the when condition.

But 'when' condition doesn't accept the [*] it says that it is unexpected. But it is required for an array.

if i remove the [*] from the when condition it says that 'stat' has no arrtibuut.

How can i set the when condition to work

1 Answer 1

1

You would have to loop through your result and test each individual element in your when condition.

Fortunately, you can do what you want by integrating the filter in a single jsmepath expression in your json_query

- set_fact:
    log_paths: "{{ register_some_dir | json_query('results[?stat.exists].stat.path') }}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.