Search code examples
ansiblelxc

How to test a string condtion deep in Ansible list variable?


I register a list-variable lxcs_info.results in my Ansible playbook that is populated with following LXC-related data:

ok: [webserver] => {
    "lxcs_info": {
        "changed": false, 
        "results": [
            {
                "_ansible_item_result": true, 
                "changed": false, 
                "invocation": {
                    "module_args": {
                        "name": "cndev", 
                        "state": "started", 
                        "template": "ubuntu", 
                    }, 
                    "module_name": "lxc_container"
                }, 
                "item": {
                    "backing_store": "dir", , 
                    "container_config": [
                        "lxc.group = dev", 
                        "lxc.group = mdblxc", 
                        "lxc.network.type = veth", 
                        "lxc.network.link = lxcbr0"
                    ], 
                    "name": "cndev", 
                    "state": "started", 
                    "template": "ubuntu",
                }, 
                "lxc_container": {
                    "interfaces": [
                        "eth0", 
                        "lo"
                    ], 
                    "ips": [
                        "10.0.3.2"
                    ], 
                    "name": "cndev", 
                    "state": "running"
                }
            },
            {
            # another result-item like the above
            },
            {
            # yet another item with same structure as above
            }
        ]
    }
}

As I'm interested in the container_config-section mostly, I need a task that executes a command based on the condition of the contents of those items, specifically on condition that content in the item.container_config is excactly lxc.group = mdblxc.

How should I write the when-clause for it? I have tried with the below task,

- name: Test task
  debug: msg="Found mdblxc in {{ item }}"
  with_items: lxcs_info.results
  when: item.item.container_config.0.lxc.group == "mdblxc"

but it does not work - ansible-playbook fails with error:

fatal: [webserver]: FAILED! => {
  "failed": true,
  "msg": "The conditional check '( item.item.container_config.0.lxc.group == \"mdblxc\")' failed.
          The error was: error while evaluating conditional (( item.item.container_config.0.lxc.group == \"mdblxc\" ): 'unicode object' has no attribute 'lxc'

          The error appears to have been in 'mytask.yaml': line nnn, column 3, but may
          be elsewhere in the file depending on the exact syntax problem.

          The offending line appears to be:
            with_items: lxcs_info.results
          - name: Test task
            ^ here
         "
}

Solution

  • If you pay close attention to container_config, you notice that it is a list of string items.
    You can't access lxc.group of string "lxc.group = mdblxc".

    So you when statement should look like this:

    when: '"lxc.group = mdblxc" in item.item.container_config'