Ansible quick start


What is ansible?

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

When we need batch deployment, we can write our own scripts, but ansible is more recommended. Using ansible does not need coding, only needs to configure yaml files, and ansible has built-in idempotency, concurrency control and other functions, which greatly reduces the workload during batch deployment.

Ansible quick start

The schematic diagram of ansible principle is shown above. We need to pay attention to the following three points to master the general principle of ansible. First, the role of the hosts configuration file is to tell ansible which machines your program will be deployed to; Second, the yaml file is used to tell ansible what operations to perform on the target machine. Third, ansible does not need to install the client on the target machine. It sends instructions and programs to be deployed to the target machine through SSH.

Install ansile

Installation command:

python3 -m pip install --user ansible==2.5.4

Verify correct installation:

ansible --version

Configure ansible

  • Configure the. Ansible.cfg file

Path to. Ansible.cfg: ~ /. Ansible.cfg

Write the following to the. Ansible.cfg file:

#Inventory is the declared hosts configuration file
  • SSH uses the key to log in to the server

Set SSH to log in with a key. Use the SSH keygen command to generate a key pair and put the ID_ writes the authorized of the target server_ Keys file.

  • Edit hosts file

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

#ServiceA is the cluster name
#Enumerate the IP addresses of the ServiceA cluster

Edit yaml file

Yaml files that tell ansible what operations to perform on the target machine. Ansible calls such files “playbook”.

Let’s write a playbook called hello. YML. This playbook is used to send HelloWorld files to the ServiceA cluster.

#Hosts is the cluster where the service will be deployed
- hosts: serviceA
# remote_ User is logged in to the remote machine as root
  remote_user: root
#Vars defines some variables. These variables can be used in the following tasks.
     src: /Users/yutou/mywork/ansible-playbook
#Tasks are specific actions performed on a remote machine.
      #Name is the name of the action
      - name: upload helloworld
        #Copy is the specific action to be performed. Copy is an ansible module, which is used to upload local files to the target machine.
        #{{SRC}} is jinja2 template syntax. If you don't understand jinja2 template syntax, you can baidu by yourself.
        copy: src={{ src }}/helloworld dest=/home


ansible-playbook hello.yml

Common modules of ansible Playbook

Ansible many modules can “see their name and know their meaning”. Many modules imitate or encapsulate linux commands. For more modules, see the official documents. Let’s briefly introduce several modules:

  • Synchronize, copy and unarchive can upload files.
  • Ping: check whether the specified node machine is still connected. If the host is online, reply pong.
  • Yum, apt: both modules are installed on the remote system.
  • PIP: Python installation package on remote machine.
  • User, group: user managed.
  • Service: to manage services, similar to the service on CentOS 7.

The template module and the module that executes linux commands on the remote machine are very important modules, so let’s focus on them next.

Common modules of ansible Playbook

One feature of the configuration file is that the files on each machine are different, and some personalized configurations are required, such as “Hello world” for machine a and “Hello Liming” for machine B. This requirement needs to be implemented by the template module.

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

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

For example, the template file hello_ x. The contents are as follows:

hello {{ name }}

The hosts file is configured as follows:

[serviceA] name=world name=Liming

Hello in Playbook_ x. YML is configured as follows:

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

Execute ansible playbook hello_ x. After YML, go to / home / hello on The content of the X file is hello world, and on is hello Liming

Executing linux commands on a remote machine

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

  • Generally, command is used
  • There are special characters in the command. Use shell
  • Raw directly executes the original command without module encapsulation, which is not recommended.

Note that the contents of the command are generally led up with “”, otherwise an error may be reported when rendering the template:

    - name: start datanode
      command: "/hadoop-2.7.5/sbin/ start datanode"

Recommended reading

Is redis using hash or string to store object information

Practical notes: the journey of configuring monitoring services for NSQ