Configure instance to be an Ansible controller (original) (raw)

View this page

Toggle table of contents sidebar

This slightly more complex example demonstrates how to set up an Ansible controller host on boot. The example installs a playbook repository from a remote private repository and then runs two of the plays.

For a full list of keys, refer to the Ansible module schema.

#cloud-config

Update, upgrade and install packages

------------------------------------

package_update: true package_upgrade: true packages: ['git', 'python3-pip']

Set up an Ansible user

----------------------

We give the local Ansible user password-less sudo so that Ansible can

write to a local root-only file.

users:

Initialize LXD using cloud-init

-------------------------------

A LXD container is started (using Ansible) on boot, so we must

initialize LXD.

lxd: init: storage_backend: dir

Configure and run Ansible on boot

---------------------------------

First we install Ansible using pip, and ensure that the

community.general collection is installed (it is likely to be already

installed by pip).

Then we use a deploy key to clone a remote private repository and run two

playbooks:

* The first starts a LXD container and creates a new inventory file

* The second connects to and configures the container using Ansible

The public version of the playbooks can be inspected at this URL:

https://github.com/holmanb/ansible-lxd-public

ansible: install_method: pip package_name: ansible run_user: ansible galaxy: actions: ['ansible-galaxy', 'collection', 'install', 'community.general'] setup_controller: repositories: - path: /home/ansible/my-repo/ source: git@github.com:holmanb/ansible-lxd-private.git run_ansible: - playbook_dir: /home/ansible/my-repo playbook_name: start-lxd.yml timeout: 120 forks: 1 private_key: /home/ansible/.ssh/id_rsa - playbook_dir: /home/ansible/my-repo playbook_name: configure-lxd.yml become_user: ansible timeout: 120 forks: 1 private_key: /home/ansible/.ssh/id_rsa inventory: new_ansible_hosts

Write a deploy key to the filesystem for Ansible

------------------------------------------------

This deploy key is tied to a private GitHub repository_. It exists to

demonstrate how deploy keys are used in Ansible. A duplicate public copy

of the repository exists here_.

write_files: