<p>I started to develop a python module for the task. It had not been a big job, but honestly I only want to change some values yet, so I finally shortcutted it with this workaround:</p>
<pre><code>- name: lxc config set core.https_address &quot;0.0.0.0:8443&quot;
  ansible.builtin.shell: if [ &quot;$(lxc config get core.https_address)&quot; = &quot;0.0.0.0:8443&quot; ]; then exit 0; else lxc config set core.https_address &quot;0.0.0.0:8443&quot; &amp;&amp; exit 255; fi
  register: script_res
  changed_when: script_res.rc == 255
  failed_when: script_res.rc not in [0, 255]
</code></pre>
<p>Idea is that I substitute it with a singleline shellscript. This oneliner exists with 0 if the settings was already what I want, and with 255 if it was only now changed. In the case of any error, it exits with a non-zero exit code.</p>
<p>On this way, the <code>change_when</code> field can be used to detect if there was a change (so the task must be yellow) or not, while also errors (making the task red) will be detected nearly correctly (because they cause a non-zero exit code, well this workaround has the potential bug, if the error results a 255 exit code which would be detected as change, but its chance is very small).</p>
<p>While that is a nearly good solution, although it would be better if the related lxd module had been already developed. The same solution works also for incus (as I can see, the incus ansible module is essentially a clone of the lxc).</p>

CC BY-SA 4.0
2024-03-31T22:28:01.220Z
If a better answer appears, that will get the pipe.
peterh
CC BY-SA 4.0
2024-03-31T20:01:39.830Z
2024-04-14T14:24:17.843Z
2024-04-14T14:24:17.843Z
peterh
<p>I am trying to set up the lxd global, server settings with ansible. Note, these are <em>not</em> the instance or container settings, these are the global settings of the lxd daemon. For example, <code>core.https_address</code>:</p>
<pre><code># lxc config show
config:
  core.https_address: 0.0.0.0:8443
</code></pre>
<p>How can I modify them with the super idempotent way by ansible modules? My impression about the docs of the relevant modules (<code>community.general.lxd_container</code>, <code>community.general.lxd_profile</code>, <code>community.general.lxc_container</code>, <code>community.general.lxd_project</code>) that they are doing everything, except this.</p>

<ansible><containers><infrastructure-as-code>
How to manage lxd server configuration by ansible?
peterh
# How to manage lxd server configuration by ansible?

(Asked by: username_0 on 2024-03-31)

I am trying to set up the lxd global, server settings with ansible. Note, these are not the instance or container settings, these are the global settings of the lxd daemon. For example, core.https_address:
# lxc config show
config:
  core.https_address: 0.0.0.0:8443

How can I modify them with the super idempotent way by ansible modules? My impression about the docs of the relevant modules (community.general.lxd_container, community.general.lxd_profile, community.general.lxc_container, community.general.lxd_project) that they are doing everything, except this.

**Tags:** ansible, containers, infrastructure-as-code


---

## 1 answers

### Answer by username_0 on 2024-03-31. Score: 1 | Accepted answer

I started to develop a python module for the task. It had not been a big job, but honestly I only want to change some values yet, so I finally shortcutted it with this workaround:
- name: lxc config set core.https_address "0.0.0.0:8443"
  ansible.builtin.shell: if [ "$(lxc config get core.https_address)" = "0.0.0.0:8443" ]; then exit 0; else lxc config set core.https_address "0.0.0.0:8443" && exit 255; fi
  register: script_res
  changed_when: script_res.rc == 255
  failed_when: script_res.rc not in [0, 255]

Idea is that I substitute it with a singleline shellscript. This oneliner exists with 0 if the settings was already what I want, and with 255 if it was only now changed. In the case of any error, it exits with a non-zero exit code.
On this way, the change_when field can be used to detect if there was a change (so the task must be yellow) or not, while also errors (making the task red) will be detected nearly correctly (because they cause a non-zero exit code, well this workaround has the potential bug, if the error results a 255 exit code which would be detected as change, but its chance is very small).
While that is a nearly good solution, although it would be better if the related lxd module had been already developed. The same solution works also for incus (as I can see, the incus ansible module is essentially a clone of the lxc).

> **Comments:**
> **username_0**: If a better answer appears, that will get the pipe.  

---

