# An example for creating a Ubuntu container and install python - hosts : localhost connection : local tasks : - name : Create a started container community.general.lxd_container : name : mycontainer state : started source : type : image mode : pull server : https://images.linuxcontainers.org protocol : lxd # if you get a 404, try setting protocol: simplestreams alias : ubuntu/xenial/amd64 profiles : [ "default" ] wait_for_ipv4_addresses : true timeout : 600 - name : Check python is installed in container delegate_to : mycontainer ansible.builtin.raw : dpkg -s python register : python_install_check failed_when : python_install_check.rc not in [0, 1] changed_when : false - name : Install python in container delegate_to : mycontainer ansible.builtin.raw : apt-get install -y python when : python_install_check.rc == 1 # An example for creating an Ubuntu 14.04 container using an image fingerprint. # This requires changing 'server' and 'protocol' key values, replacing the # 'alias' key with with 'fingerprint' and supplying an appropriate value that # matches the container image you wish to use. - hosts : localhost connection : local tasks : - name : Create a started container community.general.lxd_container : name : mycontainer state : started source : type : image mode : pull # Provides current (and older) Ubuntu images with listed fingerprints server : https://cloud-images.ubuntu.com/releases # Protocol used by 'ubuntu' remote (as shown by 'lxc remote list') protocol : simplestreams # This provides an Ubuntu 14.04 LTS amd64 image from 20150814. fingerprint : e9a8bdfab6dc profiles : [ "default" ] wait_for_ipv4_addresses : true timeout : 600 # An example for deleting a container - hosts : localhost connection : local tasks : - name : Delete a container community.general.lxd_container : name : mycontainer state : absent # An example for restarting a container - hosts : localhost connection : local tasks : - name : Restart a container community.general.lxd_container : name : mycontainer state : restarted # An example for restarting a container using https to connect to the LXD server - hosts : localhost connection : local tasks : - name : Restart a container community.general.lxd_container : url : https://127.0.0.1:8443 # These client_cert and client_key values are equal to the default values. #client_cert: " {{ lookup ( 'env' , 'HOME' ) }} /.config/lxc/client.crt" #client_key: " {{ lookup ( 'env' , 'HOME' ) }} /.config/lxc/client.key" trust_password : mypassword name : mycontainer state : restarted # Note your container must be in the inventory for the below example. # # [containers] # mycontainer ansible_connection=lxd # - hosts : - mycontainer tasks : - name : Copy /etc/hosts in the created container to localhost with name "mycontainer-hosts" ansible.builtin.fetch : src : /etc/hosts dest : /tmp/mycontainer-hosts flat : true # An example for LXD cluster deployments. This example will create two new container on specific # nodes - 'node01' and 'node02'. In 'target:', 'node01' and 'node02' are names of LXD cluster # members that LXD cluster recognizes, not ansible inventory names, see: 'lxc cluster list'. # LXD API calls can be made to any LXD member, in this example, we send API requests to #'node01.example.com', which matches ansible inventory name. - hosts : node01.example.com tasks : - name : Create LXD container community.general.lxd_container : name : new-container-1 state : started source : type : image mode : pull alias : ubuntu/xenial/amd64 target : node01 - name : Create container on another node community.general.lxd_container : name : new-container-2 state : started source : type : image mode : pull alias : ubuntu/xenial/amd64 target : node02