8

What is Ansible?

Ansible is a configuration management and configuration tool. It uses SSH to connect to the server and run the configured tasks. Only ssh needs to be turned on on the server, and all work is handed over to ansible on the client side.

When we need to deploy in batches, we can write our own scripts, but Ansible is more recommended. To use Ansible, you only need to configure the yaml file without coding, and Ansible has built-in functions such as idempotence and concurrency control, which greatly reduces the workload during batch deployment.

The schematic diagram of Ansible principle is as above, we need to pay attention to the following 3 points to grasp the general principle of Ansible. First, the role of the hosts configuration file is to tell Ansible which machines your program should be deployed to; second, the role of the yaml file is to tell Ansible which operations to perform on the target machine. Third, Ansible does not need to install a client on the target machine. It sends the instructions and the program 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

Configure Ansible

  • configuration.ansible.cfg file

The path of .ansible.cfg: ~/.ansible.cfg

Write the following to the .ansible.cfg file:

[defaults]
# inventory 是声明 hosts 配置文件
inventory=~/.ansible/hosts
  • SSH Use the key to log in to the server

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

  • Edit the hosts file

The format of the hosts configuration file is ini. The example is as follows

# serviceA 是集群名称
[serviceA]
# 枚举 serviceA 集群的 ip 地址
192.168.33.10

Edit YAML files

A YAML file that tells Ansible what operations to perform on the target machine. Ansible refers to this type of file as a "playbook".

Let's write a playbook named hello.yml together. The role of this playbook is to send the helloworld file to the serviceA cluster.

# hosts 是要部署服务的集群
- hosts: serviceA
# remote_user 是以 root 用户登录远程机器
  remote_user: root
# vars 是定义一些变量。这些变量可以在接下来的 tasks 中使用。
  vars:
     src: /Users/yutou/mywork/ansible-playbook
# tasks 是在远程机器上具体的执行动作。
  tasks:
      # name 是该动作的名称
      - name: upload helloworld
        # copy 是要具体执行的动作。copy 是 Ansible 模块,它的作用是把本地文件上传到目标机器上去。
        # {{ src }} 是 Jinja2 模板语法,Jinja2 模板语法不懂的话可自行百度。
        copy: src={{ src }}/helloworld dest=/home

release


ansible-playbook hello.yml

Ansible playbook common modules

Many ansible modules can be "see the name, know the meaning", many modules are imitating or encapsulating Linux commands, more modules can be found in the official documentation. Let's first pick a few modules and briefly introduce them:

  • Synchronize, copy, unarchive can upload files.
  • ping: Check whether the specified node machine can still be connected. If the host is online, it will reply pong.
  • yum, apt: Both of these modules are installed on the remote system.
  • pip: Python installation package on the remote machine.
  • user, group: user managed.
  • service: Management service, similar to service on centos7.

The template module and the module that executes Linux commands on a remote machine are very important modules, so let's focus on introducing them next.

Ansible playbook common modules

A feature of the configuration file is that the files on each machine are different, and some personalized configuration is required, such as “hello world” for machine A and “hello Liming” for machine B. This requirement requires the template module to achieve.

The template module uses Jinja2 syntax to render the template file, and then uploads the rendered file to the target machine. The variables used in rendering can be read from 3 places:

  • ansible built-in variables;
  • The variables defined in the hosts file are as shown above;
  • Variables defined by vars in the playbook.

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

hello {{ name }}

The hosts file configuration is as follows:


[serviceA]
192.168.33.10 name=world
192.168.33.11 name=Liming

The configuration in playbook hello_x.yml is as follows:


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

After executing ansible-playbook hello_x.yml, the content of the /home/hello_x file on 192.168.33.10 is hello world, and 192.168.33.11 is hello Liming

Execute Linux commands on the remote machine

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

  • Normally use command
  • Special characters in the command use shell
  • raw is to execute the original command directly, without module encapsulation, it is not recommended.

Note that the content of the command is generally caused by "", otherwise an error may be reported when the template is rendered:


    - name: start datanode
      command: "/hadoop-2.7.5/sbin/hadoop-daemon.sh start datanode"

Recommended reading

Redis store object information using Hash or String

Practical notes: The mental journey of configuring monitoring services for


云叔_又拍云
5.9k 声望4.6k 粉丝

又拍云是专注CDN、云存储、小程序开发方案、 短视频开发方案、DDoS高防等产品的国内知名企业级云服务商。