What is Ansible in the end?

Foreword

Recently refactored a command line tool and redeveloped using golang, but it needs to continue to maintain the original command and add new commands.

During the reconfiguration process, the command output results of the current command line tools and the original command line tools need to be compared to ensure consistency (project requirements). The command line tools need to be used after the system is deployed and each system is completed. The deployment components are slightly different. Therefore, it is necessary to test on multiple sets of service hosts.

Need to do these actions:

Copy some configuration files to the host: user configuration, IP, and port files

Install command-line tools to make sure they are available on the service host

Execute a bunch of test commands

It stands to reason that I can continue to copy the required configuration and binary files to the host for testing.

However, there are several problems in the use of the following:

The test found that when the result is wrong, you need to modify the code in time and copy the binary file to the host again.

The host environment needs to be pushed down several times, redeployed, and verifying the version update issue

Need to manually execute one command one by one

Test a few sets of hosts

The manual method seems a bit laborious.

At present, my work is PaaS deployment-related. Deployment-level scripting, component installation, and service startup are all performed using Ansible. The specific scripting is done by other colleagues. I only know what this thing is doing. No substantive learning. So I wanted to take this opportunity to actively study under Ansible.

Wherever I studied, I almost made the old problem: I looked at official documents from the beginning, but did not pay attention to the problems that need to be solved.

Because in fact, there is a lot of Ansible's content system. Not paying attention to the problems that need to be solved will lead you to lose focus.

Realize that after the focus on the current need to solve the problem:

Copy configuration files and installation scripts to multiple hosts

Test command line tools on multiple hosts

Ansible

Looking at the background of the above events, you probably know what the Ansible is.

Ansible is a configuration management and application deployment tool that operates on the management host to perform corresponding actions on the node host. Written by Python, it is composed of modules, that is, the entities that perform the actions, all rely on the corresponding modules to perform actions on the ansible, such as copying the copy module, executing the command module, the shell module, and the file file module.

Ansible's goals are as follows:

Automated deployment of applications

Automated management configuration

Automated continuous delivery

Automated (AWS) cloud service management.

principle

The management host reads the host list from the hosts and performs the corresponding operations on the managed host in sequence through the playbook.

Pictured:

The management host is mainly the definition and configuration of the host, write playbook (that is, the execution action of the node host).

run:

Command line

Ansible all -m ping

2. playbook

Ansible-playbook example.yml

Host list

Edit the file: /etc/ansible/hosts

That is: define the host name, variables, etc.

What are the variables of the host include: host's execution user, connection port, password, etc.

Similar to ini format file

[test-new-cli] 10.62.60.72 [test-old-cli] 10.62.62.88

The above example: Divides two hosts into two groups: test-new-cli and test-old-cli

The host variables are so:

Ansible_ssh_host The name of the remote host that will be connected. This is different from the alias of the host you want to set. This can be set via this variable.

Ansible_ssh_portssh port number. If this is not the default port number, set this variable.

Ansible_ssh_user The default ssh username

Ansible_ssh_passssh password (this is not secure, we highly recommend using -ask-pass or SSH key)

Ansible_sudo_passsudo password (this is not safe, we strongly recommend using -ask-sudo-pass)

Ansible_sudo_exe (new in version 1.8) sudo command path (for 1.8 and above)

Ansible_connection is the connection type with the host. For example: local, ssh or paramiko.

Private key file used by ansible_ssh_private_key_filessh. Applies to situations where there are multiple keys and you do not want to use SSH proxy.

Ansible_shell_type The shell type of the target system. By default, the execution of the command uses the 'sh' syntax, which can be set to 'csh' or 'fish'.

Ansible_python_interpreter target host's python path.

Do not understand how to use:

For example, you want to connect to the host 192.168.100.100, switch to the root user to perform the corresponding operation.

If you directly ssh .100 will ask you to enter your username and password.

What if I edit the host list so that I don't need to enter my username and password?

[test-new-cli] example ansible_ssh_host=192.168.100.100 ansible_ssh_user=username ansible_ssh_pass=root

That is, the host alias configured with 192.168.100.100 is example, and the username and password for the host is: username/root.

Yaml

There are three types:

Key-value pair: key: value

An array

Strict: Integer, String, Boolean

This is well understood: if you are familiar with Python, these three types are equivalent to: map, list, variables

If you are familiar with golang, these three types are equivalent to: map, array, variable

Example:

--- - name: "execute command nodepool node list by { {item.name} }" shell: "{ {item.cli} } nodepool node list {{item.id} }" register: result - name: show result Debug: msg: "{ {result.stdout_lines} }" with_items: - { name: "new-cli", cli: "new-cli", id: "1" } - { name: "old-cli", cli : "old-cli", id: "1" }

Modules

Ad-doc

Ansible command line, suitable for executing a single command.

# Operate the 192.168.100.100 host to see if the management host is connected to the host of 192.168.100.100 ansible example -m ping # Operate the 192.168.100.100 host and copy the /root/opcli/conf file from the management host to the node host /etc/opcli/ Ansible test-new-cli under conf -m copy -a="src=/root/opcli/conf dest=/etc/opcli/conf"

m: module

a: connection parameters

It can be seen that it is suitable to execute a single command

Patterns

If you have a lot of node hosts, how does Ad-hoc select node hosts with specific characteristics?

Use class regular expressions.

For example, triggering all node hosts to perform actions:

Ansible all -m ping ansible * -m ping Both are equivalent, all select all node hosts

Example:

1. Host alias or IP

One.example.com one.example.com:two.example.com 192.168.1.50 192.168.1.*

2. One or more groups

Webservers webservers:dbservers

3. Exclude a group

Webservers:!phoenix # affiliated webservers group but not at the same time phoenix group

4. Intersection of two groups

Webservers:&staging # Both webservers and staging groups

5. List

Webservers[0] webservers[0-25]

6. Others

What are the requirements, see the official document.

Playbook

Write yaml files that are suitable for performing complex operations in multi-step operations. Can be seen as a collection of Ad-doc commands. It can even be seen as a programming language.

Execute: ansible-playbook example.yml

Follow the steps in the example.yml file to step through the tasks.

Example

Examples of commands, just to name a few, with parameters and without parameters.

Our ultimate goal is to execute these commands on the node host to compare the two results.

new version:

Command Description
Command-cli nodepool list Query resource pool
Command-cli nodepool node list Query resource pool node
Command-cli node list Query node
Command-cli node show Query a node
Command-cli task list Query deployment tasks

...

old version:

Command Description
Old-cli nodepool list Query resource pool
Old-cli nodepool node list Query resource pool node
Old-cli node list Query node
Old-cli node show Query a node
Old-cli task list Query deployment tasks

...

Directory Structure:

Demo-for-ansible: ---nodepool/nodepool-list.yml ---nodepool/nodepool-node-list.yml ---node/node-list.yml ---node/node-show.yml -- -task/task-list.yml ---main.yml

The first step: writing a list of hosts

/etc/ansible/hosts

[test_client] 192.168.100.100 ansible_ssh_user=xiewei ansible_ssh_pass=root ansible_connection=ssh 192.168.100.101 ansible_ssh_user=xiewei ansible_ssh_pass=root ansible_connection=ssh 192.168.100.102 ansible_ssh_user=xiewei ansible_ssh_pass=root ansible_connection=ssh

Define the host connection type, user name, password

Step Two: Writing a yaml file

The main action:

Create two folders on the node host: /etc/client/conf and /et/client/commands

Copy the management host directory: /etc/client/conf file to the node host: /etc/client/conf

Copy the management host binary: /root/gosrc/src/client/command-cli to the node host /etc/client/commands

Soft connect node host binary: /etc/client/commands/command-cli to node host /usr/bin/command-cli

Perform the query command in the above table: nodepool, node, task

Main.yml

--- - hosts: test_client remote_user: root become: yes become_user: root become_method: sudo tasks: # Create a directory on the node host: /etc/client/conf - name: create /etc/client/conf /etc/client/ Commands file: path: "/etc/client/{ {item} }" owner: root group: root mode: 0755 state: directory with_items: - "conf" - "commands" # Copy the management host configuration file /etc/client/ Conf and binaries to /etc/client/conf, /etc/client/commands - name: copy /etc/client/conf copy: src="{ {item.src } }" dest="{{item.dest} }" owner=root group=root mode=0644 with_items: - { src: "/etc/client/conf", dest: "/etc/client/conf" } - { src: "/root/gosrc/src/client /command-cli", dest: "/etc/client/commands"} # Soft connect to /usr/bin/command-cli - name: link /etc/client/commands file: src="/etc/client/commands /command-cli" dest="/usr/bin/command-cli" state=link # nodePool list - include_tasks: "nodepool/nodepool-list.yml" with_items: - { client: "new client", name: "command-cli"} - { client: "old client", name: "old-cli"} # nodePool node list - include_tasks: "nodepool/nodepool-node-list.yml" with_items: - { id: "1", client: "new client", name: "command-cli"} - { id: "1", client: " Old client", name: "old-cli"} # node list - include_tasks: "node/node-list.yml" with_items: - { client: "new client", name: "command-cli"} - { client: "old client", name: "old-cli"} # node show - include_tasks: "node/node-show.yml" with_items: - { client: "new client", name: "command-cli", id: 1} - { client: "old client", name: "old-cli ", id: 1} # task list - include_tasks: "task/task-list.yml" with_items: - { client: "new client", name: "command-cli"} - { client: "old client", name : "old-cli"}

Task-list.yml

--- - name:execute command task list shell: "{{item.name} }task list" register: result - name: show result debug: msg: "{ {result.stdout_lines} }"

Step 3: Check the syntax

Two methods

Ansible-playbook main.yml --syntax-check

Install pip install ansible-lint first

Ansible-lint main.yml

Step 4: Execute

Ansible-playbook main.yml

The whole process of writing is probably like this. The core is to write a yml file and call various modules supported by ansible to complete the task.

Indoor Gaming Projector

Shenzhen Happybate Trading Co.,LTD , https://www.szhappybateprojectors.com