User Tools
Writing /app/www/public/data/meta/development/applications/ansible.meta failed
development:applications:ansible
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| development:applications:ansible [2018/08/30 15:17] – mzubal | development:applications:ansible [2021/06/25 10:09] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Ansible ====== | ||
| + | Ansible is an open source command line tool for automating server configuration and deployment. Ansible is designed to be set up and operated entirely from the controlling machine - the servers being configured do not need to have any sort of ansible server software - they just need Python and to be accessable via SSH. This makes it extremely simple to get existing ansible playbooks running on a new server. | ||
| + | |||
| + | An ansible configuration is broken into several parts, each defined by their own [[https:// | ||
| + | |||
| + | Overall Ansible is extremely flexable and powerful and capable of a lot more than the simple examples shown below. For (much) more information see the [[http:// | ||
| + | |||
| + | Going through an example Ansible setup such as the [[https:// | ||
| + | |||
| + | |||
| + | ===== Modules ===== | ||
| + | |||
| + | To perform any action with ansible you must specify a module to use. [[http:// | ||
| + | |||
| + | ===== Tasks ===== | ||
| + | |||
| + | A task is exactly what is sounds like. Tasks are defined in roles and look like this: | ||
| + | |||
| + | < | ||
| + | - name: add server cluster to hostfile | ||
| + | lineinfile: | ||
| + | path: /etc/hosts | ||
| + | line: " | ||
| + | with_items: | ||
| + | - " | ||
| + | - " | ||
| + | - " | ||
| + | become: yes | ||
| + | </ | ||
| + | |||
| + | The first line simply defines the name of the task which also describes its purpose. The second line defines that the task is using the lineinfile module. This module then takes two parameters - the path to a text file and a string that we want to make sure exists as a line in the file. If the line already exists no action is taken, otherwise it is inserted. An important factor in creating tasks is that they should always be idempotent - i.e. running them multiple times should have no effect after the first run. | ||
| + | |||
| + | In this case the line is not set to a normal string but to a variable named ' | ||
| + | |||
| + | So this task simply makes sure that the hosts file contains entries for server1, server2 and server3 and adds them if they don't already exist. | ||
| + | |||
| + | ===== Roles ===== | ||
| + | |||
| + | Roles are a way of organising tasks for reuse with a defined file structure. Roles can vary in structure but a typical role has a tasks subdirectory which contains a file called " | ||
| + | |||
| + | Example role structure: | ||
| + | |||
| + | < | ||
| + | roles/ | ||
| + | ├── role1/ | ||
| + | │ | ||
| + | │ | ||
| + | │ | ||
| + | │ | ||
| + | │ | ||
| + | │ | ||
| + | │ | ||
| + | </ | ||
| + | |||
| + | For more details on this see the [[http:// | ||
| + | |||
| + | For more information on our existing roles, refer: [[development: | ||
| + | |||
| + | |||
| + | ===== Inventory ===== | ||
| + | |||
| + | Inventory files define a list of servers that roles are to be used against. It can simply be a list of hostnames or it can be broken up into groups denoted by square brackets. | ||
| + | |||
| + | Example inventory: | ||
| + | |||
| + | < | ||
| + | [cluster] | ||
| + | server1 | ||
| + | server2 | ||
| + | server3 | ||
| + | |||
| + | [database] | ||
| + | server3 | ||
| + | |||
| + | [applications] | ||
| + | server1 | ||
| + | server2 | ||
| + | |||
| + | [backups] | ||
| + | server2 | ||
| + | server3 | ||
| + | </ | ||
| + | |||
| + | More complicated inventory structures are possible including setting variables in the inventory file and child groups. See [[http:// | ||
| + | |||
| + | ===== Playbooks ===== | ||
| + | |||
| + | Playbooks are YAML files that bring everything else together - they call tasks or roles to be acted against specific hosts or host groups as defined in the inventory file. Simple example playbook: | ||
| + | |||
| + | |||
| + | < | ||
| + | --- | ||
| + | - hosts: all | ||
| + | roles: | ||
| + | - {role: user-setup} | ||
| + | - {role: configure-network-settings} | ||
| + | |||
| + | - hosts: applications | ||
| + | roles: | ||
| + | - {role: install-tomcat} | ||
| + | - {role: install-java} | ||
| + | - {role: create-app-environments} | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Variables ===== | ||
| + | |||
| + | I haven' | ||
| + | |||
| + | There' | ||
| + | |||
| + | Sometimes variables can be defined within the task itself such as the use of the item variable above. | ||
| + | |||
| + | ===== Tips& | ||
| + | ==== Ansible settings ==== | ||
| + | There are couple of useful settings, which you likely want to set globally for all your ansible stuff. | ||
| + | These get configured in ~/ | ||
| + | Example of ~/ | ||
| + | < | ||
| + | [defaults] | ||
| + | host_key_checking = False | ||
| + | timeout = 30 | ||
| + | |||
| + | [ssh_connection] | ||
| + | pipelining = True | ||
| + | ssh_args = -o ControlMaster=auto -o ControlPersist=600s | ||
| + | </ | ||
| + | |||
| + | ===== Troubleshooting ===== | ||
| + | ==== Install libselinux-python when it doesn' | ||
| + | |||
| + | Sometimes the libselinux-python cannot be installed because yum cannot find it. | ||
| + | In that case it is needed to figure the libselinux version first: | ||
| + | < | ||
| + | [scotty@Blade5(sfqadb1) ~]$yum list | grep libselinux | ||
| + | libselinux.x86_64 | ||
| + | libselinux-python.x86_64 | ||
| + | libselinux-utils.x86_64 | ||
| + | </ | ||
| + | |||
| + | And then download the according rpm of libselinux-python: | ||
| + | < | ||
| + | wget ftp:// | ||
| + | </ | ||
| + | |||
| + | And then install the rpm: | ||
| + | < | ||
| + | sudo rpm -Uvh libselinux-python-2.0.94-5.8.el6.x86_64.rpm | ||
| + | </ | ||