Ansible deployment and basic configuration

1. What is ansible:

Ansible is an open source automation platform, a configuration management tool, automated operation and maintenance tool.
Based on Python development, ansible integrates the advantages of many operation and maintenance tools (puppet, cfengine, chef, func, fabric), and realizes functions such as batch system configuration, batch program deployment, and batch running commands.
Ansible works based on modules and does not have the capability of batch deployment. The real batch deployment is the module run by ansible, and ansible just provides a framework.

Advantages of ansible:

1-1. Support cross-platform operation: ansible provides agentless support for linux, Windows, unix and network devices, suitable for physical, virtual, cloud and container environments

1-2. Strong readability: reduce human error. When the playbook is running, if the target host is in the correct state, no changes will be made.

1-3. Perfect description application: playbook (idempotent)

1-4. Easily manage version control: the playbook is plain text and can be regarded as source code

1-5. Support dynamic list

1-6. Orchestration can be easily integrated with other systems: puppet, jenkins

1-7. Infrastructure as code: simple tasks can be completed with commands

2. Ansible installation:

It needs to be downloaded from the network warehouse provided by Alibaba Cloud:

dnf install ansible -y        下载ansible
ansible --viersion			  查看当前ansible版本信息

ansible的基本配置文件信息:
/etc/ansible/ansible.conf		##全局配置文件,默认很少修改
/etc/ansible/hosts			    ##全局主机清单清单文件

The remote link of the ansible service is based on ssh. Therefore, after installing ansible on the host (server), you must establish a public and private key and passwordless connection with the ansible client host:
Insert picture description here

3. Ansible's build list:

清单就是ansible控制主机的列表
/etc/ansible/hosts                     ##默认全局清单文件,通常不编辑此清单,而是自建文件(自创用户下的ansible文件下的hosts)

#1.直接书写受管主机名或ip,每行一个
node1.westos.com
node2.westos.com
172.25.254.240


#2.设定受管主机的组[组名称]
#清单查看:
ansible 清单中组名称 [-i 清单文件]  --list-hosts
ansible ungrouped --list-hosts
ansible all --list-hosts

#单层清单#
[list1]
node1.westos.com
node2.westos.com

[list2]
node2.westos.com

[list3]
172.25.254.240

It can also be nested definitions, as follows: The two subgroups in the servers group are webservers and dbservers

[webservers]
server1.westos.org
server2.westos.org
172.25.0.1 

[dbservers]
node1.westos.org
node1.westos.org

[servers:children]
webservers
dbservers

After correctly establishing the key connection method and writing it into the corresponding client list, you can simply check: (ssh connects to all target hosts)
all means all
ansible test -m ping means testing the clients in the test group
Insert picture description here

主机规格的范围化操作#
#通过指定主机名称或IP的范围可以简化Ansible主机清单
#语法:
#[start:end]
[westostest]
172.25.254.[100:108]:22       
如果需要更改端口,直接使用 “:端口号  ” 添加即可

4. Operation example in ansible:
By default: the red font represents the execution error, once an error occurs, the following instructions will not continue to be executed; the
yellow font represents the change after the operation is executed; (executed successfully) the
green font represents the command execution Success, no errors occurred, and no changes were made;
-k means asking for the password;
-u means as the *** user;
-m means the name of the module used;
Insert picture description here
5. The regular expression of the list specified by the ansible command:

*		##所有                       注意: 在命令行中使用时(ad-hoc)要加“ ”,否则会变成当前目录中的所有;
		##172.25.254.*
		##westos*

:		##逻辑或
		##westos1:linux
		##172.25.254.100:172.25.254.200

:&		##逻辑与
		##westos1:&linux
		##主机即在westos1清单也在linux清单中

:!		##逻辑非                       需要用单引号转译 !
		##westos1:!linux
		##在westos1中不在linux中

~		##以关键字开头

~(str1|str2)	##以条件1或者条件2开头		


6.Detailed explanation of Ansible configuration file parameters:


Key points: View the detailed explanation of the parameters in each module in
ansible : ansible-doc module name to view the parameter information in the module (you can directly view the example at the end of the file content)


Group name in ansible list -m module -u remote_user

6-1: Classification and priority of configuration files:

/etc/ansible/ansible.cfg		#基本配置文件,找不到其他配置文件此文件生效,优先级最低
~/.ansible.cfg			        #当前目录中没有ansible.cfg时,用户家目录中的此文件生效
./ansible.cfg			        #当前目录中的ansible文件优先级最高

6-2: Common configuration parameters:

Insert picture description here

Guess you like

Origin blog.csdn.net/lb1331/article/details/111994276