Ansible from entry to mastery [1]

Hello everyone, I am from 9 in the morning to 12 in the evening, and I am currently doing operation and maintenance related work. Blogging is for accumulation, I hope everyone will make progress together!
My homepage: 9 am to 12 pm
Column name: Ansible from entry to proficiency and determined to become an ansible boss


Please add a picture description

Introduction to Ansible

Ansible is developed based on Python, which 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 operation commands. The Ansible architecture is relatively simple, and it only needs to connect to the client through SSH to perform tasks.
Ansible works based on modules and does not have the ability to deploy in batches. What really has batch deployment is the module run by ansible, and ansible just provides a framework. mainly include:

(1)、连接插件connection plugins:负责和被监控端实现通信;
(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
(3)、各种模块核心模块、command模块、自定义模块;
(4)、借助于插件完成记录日志邮件等功能;
(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

Environmental preparation

First of all, we need to prepare two machines, one is the main control machine where ansible is installed, and the other is the test machine

#主控机
yum -y install ansible

The main components of Ansible

ANSIBLE PLAYBOOKS: task script (task set), arranges configuration files defining Ansible task sets, and is executed sequentially by Ansible, usually a YML file INVENTORY in JSON format ; Ansible management host list /etc/anaible/hostsMODULES ; Ansible executes commands Function modules , most of which are built-in core modules, and PLUGINS can also be customized :; module function supplements, such as connection type plug-ins, loop plug-ins, variable plug-ins, filter plug-ins, etc., this function is not commonly used
API: an application for third-party programs to call Programming interface
ANSIBLE: The green box combining INVENTORY, API, MODULES, PLUGINS can be understood as ansible command tool, which is the core execution tool

Ansible configuration file details

Inventory host list

The main function of ansible is to operate in batches of hosts. In order to conveniently use some of the hosts, they can be grouped and named in the inventory file. The default inventory file
is /etc/ansible/hosts.
Inventory to dynamically generate
the /etc/ansible/hosts file format. The inventory file follows the INI file style, and the characters in square brackets are group names. The same host can be merged into multiple different groups at the same time; in addition, if the target host uses a non-default SSH port, it can be marked with a colon and a port number after the host name.
For example

[test]
192.168.100.100
test1.example.com

If the hostname follows the same naming format, it can also be marked as a list

[test]
192.168.100.[1:100]
test[1:100].example.com
test[a:z].example.com

Configuration file /etc/ansible/ansible.cfg

This file is the main configuration file of ansible, all the content is defined in it, generally in the default state, we usually only operate **[defaults]** grouping

#inventory = /etc/ansible/hosts # 主机列表配置文件
#library = /usr/share/my_modules/ # 库文件存放目录
#remote_tmp = SHOME/.ansible/tmp #临时py命令文件存放在远程主机目录
#local_tmp= $HOME/.ansible/tmp # 本机的临时命令执行目录
#forks= 5 # 默认并发数,即同时执行五台机器
#sudo user = root # 默认sudo 用户
#ask_sudo_pass = True #每次执行ansible命令是否询问ssh密码
#ask _pass= True
#remote_port = 22 #默认使用22端口远程等登录
#host_key_checking = False # 检查对应服务器的host key,建议取消注释
#log_path=/var/log/ansible.log #日志文件

The main thing to pay attention to is host_key_checking. If you don’t uncomment it, you can’t realize batch password-free login. In addition, the log_path is not recorded by default. If it is enabled, the service will take effect immediately without restarting

Ansible command execution source

USER,普通用户,即SYSTEM ADMINISTRATOR
CMDB (配置管理数据库) API 调用
PUBLIC/PRIVATE CLOUD API调用
USER-> Ansible Plavbook -> Ansibile利用ansible实现管理的方式:
Ad-Hoc 即ansible命令,主要用于临时命令使用场景
Ansible-playbook 主要用于长期规划好的,大型项目的场景,需要有前提的规划

Ansible-Playbook execution process

将已有排好的任务集写入Ansible-Playbook
通过ansible-playbook命令分拆任务集至逐条ansible命令,按预定规则逐条执行
Ansible主要操作对象 :
HOSTS主机
NETWORKING网络设备
注意事项
执行ansible的主机一般称为主控端,中控,master或堡垒机
主控端Python版本需要2.6或以上
被控端Python版本小于2.4需要安装python-simplejson
被控端如开启SELinux需要安装libselinux-python
windows不能做为主控端

Code words are not easy. If the article is hopeful for you, please support it three times.
If you have any questions, please leave a message and discuss together, thank you.
You can also pay attention to the official account below, and will reply as soon as possible after seeing the message.

Guess you like

Origin blog.csdn.net/tootsy_you/article/details/130880780