arniom
arniom

Reputation: 1

Configuring LXD storage pools with Ansible

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

Upvotes: 0

Views: 180

Answers (1)

Ryan
Ryan

Reputation: 1628

Generally running shell commands 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_container or 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 the Note section at the top of the documentation.

ansible-galaxy collection install community.general

If there isn't a module for doing what you need, perhaps something as basic as:

- name: Check
  command: lxc storage list --format=json
  register: results
  changed_when: false
  failed_when: false

- name: Create
  command: lxc storage create nas dir source=/data/nas/pool/lxd
  when: "'nas' not in results.stdout"

?

Upvotes: 0

Related Questions