Ansible How to use

Source: Internet
Author: User


First, Introduction

Ansible is a cluster management tool similar to puppet, Saltstack, with the advantage that only ssh and Python are needed, not the client, like puppet and Saltstack. Puppet and Saltstack these 2 software need to install the client, and Saltstack and ansible very similar, all belong to the Python stream, but Saltstack is not very stable, puppet although stable, but the command execution, the need to configure the module, Very troublesome, but also needs to install the client, the study cost is high. Ansible in terms of performance is not weaker than these two tools, and the use is not cumbersome, the key ansible is based on Paramiko development, Paramiko is a pure Python implementation of the SSH Protocol library. Ansible does not need to install client/agents on the remote host because it communicates with the remote host based on SSH.

Other Features:

Ansible provides two ways to complete the task, one is the Ad-hoc command, one is to write Ansible playbook. The former can solve some simple tasks, the latter solves the more complex task.

It is based on modules and can be used in any language development module;

Custom Script playbook with Yaml language;

In general, after learning playbooks, you can realize the Ansible of the real power.

Second, installation

It can be installed from GitHub or installed in the system yum or apt source, or PIP. For convenience, direct PIP installation is possible.

Already Ubuntu for example:


#apt-get Install Python-pip

#pip Install Ansible

Third, basic use

(1) Main profile of the Ansible application:/etc/ansible/ansible.cfg

It is important to note that communication with the remote node is through a parallel mechanism, which can be done by passing the ' –orks ' parameter setting, or by editing it in the config file. The default is 5 threads, which is more conservative and can be set to a larger value if you have enough memory.


[Defaults]

# some basic default values ...

#inventory =/etc/ansible/hosts

#library =/usr/share/my_modules/

#remote_tmp = $HOME/.ansible/tmp

#forks = 5

Forks = 100

#poll_interval = 15

#sudo_user = root

#ask_sudo_pass = True

#ask_pass = True

#transport = Smart

#remote_port = 22

#module_lang = C

(2) Host Inventory definition control console:/etc/ansible/hosts

The format of the/etc/ansible/hosts file is similar to the INI configuration file for Windows:


10.13.25.3

badwolf.example.com:5309

[Dbservers]

One.example.com

Two.example.com

Three.example.com

[Webservers]

Www[01:50].example.com

[Databases]

Db-[a:f].example.com

which

A single host is generally placed on top of the server group and can be used with IP and domain names;

The square brackets [] are the group names, which are used to classify the system so as to facilitate the individual management of different systems;

If the SSH port of the host is not a standard 22 port, you can append the port number to the host name, separated by a colon. The port numbers listed in the SSH configuration file are not used in the Paramiko connection and are used in the OpenSSH connection.

A similar set of hostname that can be abbreviated

(3)

Ad-hoc command:

Ansible has a number of modules, the default is ' command ', which is the order module, we can use the-M option to specify different modules; -A option to specify the module arguments

Common modules: Copy, command, service, yum, apt, file, raw, Shell, script, cron, user, state, template,

e.g.


#ping Test

$sudo ansible all-m ping

#管理软件包

#安装

$ansible webservers-m apt-a "Name=lrzsz state=present"

#卸载

$ansible webservers-m apt-a "Name=lrzsz state=absent"

#启动服务

$ansible webservers-m service-a "name=httpd state=started"

#停止服务

$ ansible webservers-m service-a "name=httpd state=stopped"

Wkiom1cl75batte8aaa5v-cnn1q691.png

Iv. Ansible playbooks

Playbooks is the configuration, deployment, and orchestration language of ansible. They can be described as a scenario that requires a remote host to execute a command, or a set of commands that the IT program runs. The actual playbook is a file that stacks up multiple modules with YAML syntax.

1, where Yaml has a small quirk, all yaml files (regardless of the relationship with Ansible) Start line should be---. This is part of the YAML format, which indicates the beginning of a file.

All members of the list start at the same indentation level and use a "-" as the beginning (a bar and a space):

Example YML, installing HTTP YML:


---

-Hosts:webservers

VARs

Http_port:80

max_clients:200

Remote_user:root

Tasks

-Name:ensure Apache is at the latest version

YUM:PKG=HTTPD State=latest

-Name:write the Apache config file

Template:src=/srv/httpd.j2 dest=/etc/httpd.conf

Notify

-Restart Apache

-Name:ensure Apache is running

SERVICE:NAME=HTTPD state=started

Handlers:

-Name:restart Apache

SERVICE:NAME=HTTPD state=restarted

2, test playbook, in the Openstack_test group in the host installed lftp, then uninstall Lftp, and finally start MySQL.

The Yml file is as follows:

Wkiol1cl8xoh_u0oaaa87gyu8f4782.png

Run, TASK, start MySQL display failed, is because this four server does not have the MySQL software installed, here to test to see the performance:

Wkiom1cl8kphubnoaaf4lpznlaw555.png

Note that when running playbook (from top to bottom), if a host fails to execute a task, the host will be removed from the entire playbook rotation. If an execution failure occurs, fix the error in playbook and then re-execute it.

3, separate roles

The roles is used to implement code reuse.

Roles just separated the mission. You can perform these tasks as long as you call this role in the playbook file.

If we define a very complex task, but to be used in another host group or just want to use a single host to modify the file, the total modification is not a method. You can copy multiple copies, but sometimes it's not flexible enough.

So you can use role to separate the task subject, only in the playbook to write some extra things, such as variables, hosts and so on.

Roles organizes the main elements in the Playbook (VARs, tasks, handlers) in a specific hierarchical format. Each of the main elements is represented by a directory.

The directories are as follows:

Files: All files used in this role are placed in this directory, corresponding to the copy module.

Templates:jinja2 template file storage location; Corresponds to the template module.

Tasks: Task list file, there can be more than one file, but at least a file called main;

Handlers: Processor list file, there can be more than one file, but at least a file called main;

VARs: Variable dictionary file, there can be more than one file, but at least a file called main;

Meta: Special settings and dependencies for this role;

Handlers is also a list of tasks, referenced by names, and they are no different from normal tasks. The handlers is notify by the notifier and will not be executed if it is not notify,handlers. No matter how many of the notifier have been notify, the handlers will only be executed once all the tasks in play have been completed.

Here is an example of a handlers:


Handlers:

-Name:restart memcached

Service:name=memcached state=restarted

-Name:restart Apache

Service:name=apache state=restarted

The best application scenario for handlers is to restart the service or to trigger a system restart. It is seldom used.

Roles is a great product, so when we write playbooks it is recommended to use roles.

The following is an example of installing the lamp environment with the playbook one-click, refer to its specification write Ansible playbook:

Https://github.com/ansible/ansible-examples/tree/master/lamp_simple

Attached to Ansible's Chinese translation document, we recommend that you study with the official documents in English:

http://www.ansible.com.cn/


This article is from the "7464112" blog, please be sure to keep this source http://7474112.blog.51cto.com/7464112/1793134

Ansible How to use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.