📜 ⬆️ ⬇️

Manage Vscale Servers via Ansible



The Vscale project was launched just six months ago, and now it is being actively developed. Intensive development is largely made possible by the community: a huge contribution to the development of the service is made by users who create libraries for working with API and share them with a wide audience on GitHub. Interesting developments include clients for Go , Ruby , Java , as well as a plugin for the Docker Machine .

For our part, we would like to offer the community another useful tool: the module for the Ansible configuration management system, with which you can deploy a virtual infrastructure based on Vscale .

Module features


By installing our module, you can write a script for automatic deployment of a virtual infrastructure based on Vscale . With it, you can perform the following operations:
')

In this article we will look at several examples of its use. All its capabilities are described in detail in the documentation for the module .

Download and initial setup


Before you start working with the module, you need to perform several preparatory operations. First, we will generate a token in the control panel to access the API.
Then we add the VS_API_KEY environment VS_API_KEY and specify the resulting token as its value:

 $ export VS_API_KEY=5c22a088f3b37c933e7480399f1e09258d6977bcd1eb2401de29e8001c9bedc36 

Create directories for SSH keys and libraries:

 $ mkdir -p vscale-ansible/{credentials,group_vars} 

After that, we will generate an SSH key to access the servers. If you need to add an existing SSH key, simply enter the path to it in the configuration file (see below):

 $ ssh-keygen -f ./vscale-ansible/credentials/ansible 

Open the configuration file vscale-ansible / ansible.cfg and write the following settings:

 sudo_user = root remote_user = root host_key_checking = False private_key_file = ./credentials/ansible [ssh_connection] control_path = %(directory)s/%%h-%%r 

Let's also create a vscale-ansible / inventory file in which the names of Ansible hosts will be specified:

 [fe-servers] fe-[01:02] [local] localhost 

After that you can load the module:

 $ git clone https://github.com/vscale/ansible-vscale-modules.git ./library 

For the convenience of further work, we define several variables in the file vscale-ansible / group_vars / all.yml:

 --- vscale_token: "{{ lookup('env', 'VS_API_KEY')}}" key: "{{ lookup('file', 'credentials/ansible.pub') }}" 


Test script: server creation


Let's try to write a script that will create a new server in Vscale with the characteristics we need. Create a file createserver.yml in a text editor and add the following lines:

 --- - name: Add SSH key connection: local gather_facts: no hosts: fe-servers tasks: - name: Add SSH key to Vscale account run_once: true vscale_ssh: token: "{{ vscale_token }}" name: "Ansible" public_key: "{{ key }}" state: present 

In this snippet, we described the procedure for adding an SSH key using two previously defined variables ( token and public_key ).
Next, we describe the parameters of the server you want to create:

 - name: Create scalet for inventory hosts vscale_scalets: token: "{{ vscale_token }}" name: "{{ inventory_hostname }}" plan: small location: spb0 image: ubuntu_14.04_64_002_master key_name: "Ansible" collect_facts: "yes" power_state: "started" state: present register: server - set_fact: ansible_ssh_host: "{{ server['scalet']['public_address']['address'] }}" 

Next, install Nginx on our server:

 - name: Install NGINX and fill disk remote_user: root hosts: - fe-servers tasks: - name: Install NGINX apt: pkg: nginx update_cache: yes state: latest - name: Filling disk command: dd if=/dev/zero of=/large.file bs=100M count=150 creates=/large.file 

In this snippet, we not only described the standard procedure for installing Nginx, but also filled the disk. We did this on purpose so that you could test the following part of our script:

 - name: Check free disk space remote_user: root hosts: - fe-servers tasks: - name: Gather facts setup: - name: Upgrade server if disk has than 3Gb left hosts: - fe-servers connection: local tasks: - name: Upgrade vscale_scalets: token: "{{ vscale_token }}" name: "{{ inventory_hostname }}" plan: medium upgrade: yes state: present when: "{{ (ansible_mounts[0].size_available)/2**30 < 3*(2**30) }}" 

In it, we run a check for free disk space. If the free disk space is less than 3 Gb, the server configuration will be automatically updated to a more productive one.

That's all. Save the changes and execute the command:

 $ ansible-playbook -i inventory createserver.yml 

You can find the described script for testing in our repository .

Software installation on servers


We just looked at an example script that simply creates a new server. Now let's try to automate the installation process.

This can be done using ready-made scripts and roles - take and use:


If you have not yet installed the module using the instructions above, you can simply clone the repository with scripts, and then connect our module using the git submodule command:

 $ git clone https://github.com/vscale/ansible-playbooks.git $ cd ansible-playbooks $ git submodule init $ git submodule update 

This method has certain advantages: if we connect some repository in the form of a submodule, it always remains in a working, consistent state. Even if we update the repository in the main branch and something “breaks” in it, your submodule will not be changed after the update.

Do not forget to generate the SSH-key and add the path to it in the configuration file :)

The structure and syntax of scripts are quite simple, and you can certainly write something of your own - API documentation to help you.

Conclusion


We invite everyone to try our Ansible module and hope that it will be useful for you. We welcome any comments and suggestions for its further improvement.
We also urge all those who write client libraries for Vscale, not to stew in their own juice, but to acquaint the community with the results of their work. We will definitely write about the most interesting developments, and reward the most active and talented.

Source: https://habr.com/ru/post/274207/


All Articles