I'm trying to configure LXD storage pools with Ansible with idempotent result
I want to check if the storage pool already exist and do the command only if it's not already present or not correctly configured.
I'm trying to do something like that :
- name: LXD config dump
shell:
cmd: lxd init --dump
register: lxd_dump
- name: LXD storage pool on NAS
shell:
cmd: lxc storage create nas dir source=/data/nas/pool/lxd
when: (lxd_dump.stdout | from_yaml).storage_pools.config.name is undefined or (lxd_dump.stdout | from_yaml).storage_pools.name != "nas" or (lxd_dump.stdout | from_yaml).storage_pools.config.source != /data/nas/pool/lxd
Here's an example of the content of (lxd_dump.stdout | from_yaml).storage_pools :
"(lxd_dump.stdout | from_yaml).storage_pools": [
{
"config": {
"source": "/data/nas/pool/lxd"
},
"description": "",
"driver": "dir",
"name": "nas"
},
{
"config": {
"source": "/var/lib/lxd/storage-pools/default"
},
"description": "",
"driver": "dir",
"name": "default"
}
]
I know that the content of the "when" is incorrect. I've tried to use json_query without success
Generally running
shellcommands with Ansible is an anti-pattern for the reasons you're facing and many more. Ansible modules will handle state and idempotency for you if they are configured correctly.Read the documentation for
lxd_containeror other lxc/lxd modules, and see if it meets your needs, refactor these Ansible steps to use the module. Note, that this module is in a community collection and you will need to install it as indicated in theNotesection at the top of the documentation.If there isn't a module for doing what you need, perhaps something as basic as:
?