Ansible Dynamic Inventory#

To manage infrastructure in K2 Cloud, you can use Ansible Dynamic Inventory, which aggregates available virtual machines by various parameters.

Installation and configuration#

Clone the repository and set dependencies:

git clone https://github.com/C2Devel/ansible
cd ansible
python3.6 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

To work with Ansible Dynamic Inventory, you have to get the API access settings. In the cloud management console, go to the profile settings and click Get API access settings.

Ansible Dynamic Inventory uses AWS configuration, so you should configure AWS CLI according to the following manual.

To configure the contrib/inventory/ec2.ini file, specify information from the API settings:

aws_access_key_id = <YOUR_ACCESS_KEY>
aws_secret_access_key = <YOUR_SECRET_KEY>

Usage example#

To get a list of all possible host aggregations, use:

ansible-inventory --list -i contrib/inventory/ec2.py

Using this information, you need to supplement your playbooks by the desired description of the host group in the hosts file:

Hosts in a specific availability zone:

- hosts: ru-msk-comp1p
  connection: local
  gather_facts: false
  tasks:
    - name: Example
      command: date

Hosts with a specific name:

- hosts: tag_Name_web_server # Where the name is — web_server
  connection: local
  gather_facts: false
  tasks:
    - name: Example
      command: date