What is Ansible?

Ansible is a configuration management and configuration tool. It uses SSH to connect to the server and run the configured tasks. The server only needs to turn on SSH, and Ansible on the client side is responsible for all the work.

When we need to deploy in bulk, we can write our own scripts, but we prefer to use Ansible. To use Ansible, you only need to configure YAML files without coding, and Ansible has built-in idempotency, concurrency control and other functions, which greatly reduces the workload of batch deployment.

As shown above, we need to pay attention to the following three points to master the general principle of Ansible. First, the purpose of the hosts configuration file is to tell Ansible which machines to deploy your program to. Second, the purpose of the YAML file is to tell Ansible what operations to perform on the target machine. Third, Ansible does not need to install a client on the target machine; it sends instructions and programs to be deployed to the target machine via SSH.

Install Ansile

Installation command:

Python3-m PIP install -- User Ansible ==2.5.4

Verify that the installation is correct:

ansible --version

Configuration Ansible

  • Configure. Ansible. CFG file

.ansible. CFG path: ~/.ansible

Write the following to the.ansible.cfg file:

[defaults] # inventory is the declaration of hosts configuration file inventory=~/.ansible/hosts
  • SSH uses a key to log in to the server

Set SSH to log in with a key. Use the ssh-keygen command to generate the key pair and write id_rsa.pub to the authorized_keys file of the target server.

  • Edit the hosts file

The format of the hosts configuration file is INI. The sample is as follows

192.168.33.10 IP address of Servicea cluster

Edit the YAML file

A YAML file that tells Ansible what operations to perform on the target machine. Ansible calls this type of file a “playbook”.

Let’s write a PlayBook called hello.yml. The purpose of this PlayBook is to send the HelloWorld file to the Servicea cluster.

# hosts is the cluster where the service is to be deployed - hosts: serviceA # remote_user is the root user to log in to the remote machine remote_user: root # vars is defined with some variables. These variables can be used in the following tasks. Vars: SRC: / Users/yutou/mywork/ansible - the playbook # tasks is specific to perform an action on a remote machine. Tasks: # name is the name of the action - name: Upload HelloWorld # copy is the action to be executed. Copy is the Ansible module, which is used to upload local files to the target machine. # {{SRC}} is the syntax for the Jinja2 template. copy: src={{ src }}/helloworld dest=/home

release


ansible-playbook hello.yml

Ansible PlayBook Common Module

Many modules of Ansible can achieve “know the meaning by its name”. Many modules are the imitation or encapsulation of Linux commands. For more modules, please refer to the official documents. Let’s first pick a few modules for a brief introduction:

  • Synchronize, copy, and unarchive can upload files.
  • Ping: Checks whether the machines on the specified node are still connected. If the host is online, it replies to Pong.
  • Yum, apt: Both modules install packages on remote systems.
  • PIP: Python installation package on remote machine.
  • User, group: User managed.
  • Service: A service that manages services, similar to those on Centos7.

The Template module and the module that executes Linux commands on a remote machine are very important modules, so I’ll focus on them here.

Ansible PlayBook Common Module

One of the features of configuration files is that the files are different on each machine and require some personalized configuration, such as “Hello World” for machine A and “Hello Liming” for machine B. This requirement requires the Template module.

The Template module renders the template file using the JINJA2 syntax and uploads the rendered file to the target machine. Variables used in rendering can be read in three places:

  • Ansible built-in variables;
  • Variables defined in the hosts file, as shown above;
  • Variables defined by vars in PlayBook.

For example, the template file hello_x has the following contents:

hello {{ name }}

The hosts file is configured as follows:


[serviceA]
192.168.33.10 name=world
192.168.33.11 name=Liming

The PlayBook hello_x.yml configuration is as follows:


  tasks:
      - name: upload helloworld
        template: src={{ src }}/hello_x dest=/home

After executing Ansible -Playbook hello_x.yml, the contents of /home/hello_x on 192.168.33.10 is hello world, and on 192.168.33.11 is hello Liming

Execute Linux commands on a remote machine

The raw, command, and shell modules are all used to execute Linux commands on remote machines. The three differences are roughly as follows:

  • In general, command is used
  • There are special characters in the command to use shell
  • RAW is a direct execution of the original command, no module encapsulation, not recommended.

Note that the content of the command is usually caused by using “”, otherwise the template rendering may return an error:

-Node command: "/ Hadoop -2.7.5/sbin/ Hadoop -daemon.sh start DataNode "-Node: "/ Hadoop -2.7.5/sbin/ Hadoop -daemon.sh start DataNode"

Recommended reading

Whether Redis stores object information as a Hash or a String

Practical note: the mental process of configuring a monitoring service for NSQ