0

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
         "
}
OpenITeX
  • 479
  • 4
  • 9

1 Answers1

1

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'
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • Thanks @konstantin-suvorov for your correct answer! I had noticed that the yaml/json list has these string items deep in the structure, but hadn't found a way to target these in comparison operations in tasks, but now your A works and in my playbook I have extended it a bit (to accomodate to my needs at the task hand). This is how the when-clause looks in the production playbook: – OpenITeX Oct 12 '16 at 11:25
  • ` when: '("lxc.group = mdblxc" in item.container_config and "dbserver" == inventory_hostname) or ("lxc.group = weblxc" in item.container_config and "webserver" == inventory_hostname) ` – OpenITeX Oct 12 '16 at 11:28
  • Hmm. seems that the markdown does not work in these comments (the "at" and back-ticks have no effect in the above comments of mine), don't know why ;) – OpenITeX Oct 12 '16 at 11:33