lost and found ( for me ? )

Manage LXD containers with Ansible lxd_container

Here are sample playbooks to manage LXD container with Ansible.

Referecne
https://docs.ansible.com/ansible/lxd_container_module.html

install ansible
$ sudo apt install -y virtualenv build-essential python-dev libffi-dev libssl-dev
$ mkdir env01
$ cd env01/
$ virtualenv venv
$ source venv/bin/activate
$ pip install git+https://github.com/ansible/ansible
$ ansible --version
ansible 2.4.0

ansible.cfg
$ cat ansible.cfg
[defaults]
inventory = hosts

hosts
$ cat hosts
[mycontainer]
f-cent01 ansible_connection=lxd
f-cent02 ansible_connection=lxd

two containers are running
$ lxc list f- -cn4
+----------+--------------------+
|   NAME   |        IPV4        |
+----------+--------------------+
| f-cent01 | 10.0.105.33 (eth0) |
+----------+--------------------+
| f-cent02 | 10.0.105.89 (eth0) |
+----------+--------------------+

run playbook
$ ansible-playbook test.yml

PLAY [mycontainer] ********************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************
ok: [f-cent02]
ok: [f-cent01]

TASK [debug] **************************************************************************************************************
ok: [f-cent01] => {
   "msg": "ipv4_address=10.0.105.33"
}
ok: [f-cent02] => {
   "msg": "ipv4_address=10.0.105.89"
}

PLAY RECAP ****************************************************************************************************************
f-cent01                   : ok=2    changed=0    unreachable=0    failed=0
f-cent02                   : ok=2    changed=0    unreachable=0    failed=0

- restart an existing container

$ cat restart.yml
---
- hosts: localhost
 connection: local
 tasks:
   - name: Restart a container
     lxd_container:
       name: f-cent01
       state: restarted

$ ansible-playbook restart.yml

- delete a container

$ cat delete_container.yml
---
- hosts: localhost
 connection: local
 tasks:
   - name: delete a container
     lxd_container:
       name: f-cent02
       state: absent

- create containers

$ cat create-container.yml
---
- hosts: localhost
 connection: local
 tasks:
   - name: Create a started container
     lxd_container:
       name: my-container01
       state: started
  source:
       type: image
       mode: pull
       server: https://images.linuxcontainers.org
       protocol: lxd
       alias: centos/7/amd64
       profiles: ["default"]
       wait_for_ipv4_addresses: true
       timeout: 600

$ ansible-playbook create-container.yml

$ lxc list my-container01
+----------------+---------+------+------+------------+-----------+
|      NAME      |  STATE  | IPV4 | IPV6 |    TYPE    | SNAPSHOTS |
+----------------+---------+------+------+------------+-----------+
| my-container01 | RUNNING |      |      | PERSISTENT | 0         |
+----------------+---------+------+------+------------+-----------+

- create a container and install some packages via yum

(venv) hattori@ubuntu05:~/Ansible_works/env01$ cat ansible.cfg
[defaults]
inventory = hosts

(venv) hattori@ubuntu05:~/Ansible_works/env01$ cat hosts
[mycontainer]
f-cent01 ansible_connection=lxd
my-container01 ansible_connection=lxd
[test]
f-test01 ansible_connection=lxd

(venv) hattori@ubuntu05:~/Ansible_works/env01$ cat test01.yml
---
- hosts: localhost
 connection: local
 tasks:
   - name: Create a started container
     lxd_container:
       name: f-test01
       state: started
       source:
         type: image
         alias: cent7
       profiles: ["default"]
       wait_for_ipv4_addresses: true
       timeout: 600
- hosts: test
 remote_user: root
 tasks:
   - name: install httpd
     yum:
       name: httpd
       state: present

$ lxc exec f-test01 bash
[root@f-test01 ~]# rpm -qa | grep http
httpd-2.4.6-45.el7.centos.4.x86_64
httpd-tools-2.4.6-45.el7.centos.4.x86_64

- transfer a file from a localhost to an existing container

$ cat copy-a-file.yml
---
- hosts:
   - test
 remote_user: root
 tasks:
  - copy:
     src: /etc/hosts
     dest: /tmp/hosts
     owner: root
     group: root
     mode: 0664

$ cat ansible.cfg
[defaults]
inventory = hosts

$ cat hosts
[mycontainer]
f-cent01 ansible_connection=lxd
my-container01 ansible_connection=lxd
[test]
f-test01 ansible_connection=lxd

$ ansible-playbook create-container-install.yml

$ lxc exec f-test01 -- ls /tmp/
hosts