参考:https://www.cnblogs.com/keerya/p/7987886.html
组件原理图

Ansible:Ansible核心程序。
HostInventory:记录由Ansible管理的主机信息,包括端口、密码、ip等。
Playbooks:“剧本”YAML格式文件,多个任务定义在一个文件中,定义主机需要调用哪些模块来完成的功能。
CoreModules:核心模块,主要操作是通过调用核心模块来完成管理任务。
CustomModules:自定义模块,完成核心模块无法完成的功能,支持多种语言。
ConnectionPlugins:连接插件,Ansible和Host通信使用
工作流程图

简单理解就是Ansible在运行时, 首先读取ansible.cfg中的配置, 根据规则获取Inventory中的管理主机列表, 并行的在这些主机中执行配置的任务, 最后等待执行返回的结果。
安装
安装 epel-release yum源
yum -y install epel-release
安装ansible
yum -y install ansible
查看ansible版本
ansible --version或者rpm -qa | grep ansible
检查指定主机的网络连通性
$ ansible all -i xxx.xxx.xxx.xxx, xxx.xxx.xxx.xxx -m ping
配置文件
[defaults]# Ansible需要连接管理的主机列表hosts文件路径,默认/etc/ansible/hostsinventory = /etc/ansible/hosts# ansible role存放路径,默认/etc/ansible/rolesroles_path = /etc/ansible/roles# Ansible日志路径,默认/var/log/ansible.loglog_path = /var/log/ansible.log# SSH连接超时时间,默认10stimeout = 60# ansible第一次连接客户端是是否要检查ssh密钥host_key_checking = False# ansible执行并发数,默认5forks = 100# 异步执行任务时查询间隔,默认15spoll_interval = 15# 执行ansible命令时使用的用户,默认rootsudo_user = root# 远程主机SSH端口,默认22remote_port = 22# ansible执行playbook时远程认证用户,默认rootremote_user = root# Ansible搜寻模块的位置,默认/usr/share/my_modules/# library = /usr/share/my_modules/# module_utils = /usr/share/my_module_utils/# ansible模块运行语言环境,默认Cmodule_lang = Cmodule_set_locale = False# ansible使用模块,默认commandmodule_name=command
主机清单
# 使用主机IP192.168.1.50# 使用主机名web1# 分组[webgroup]192.168.1.70
MacOS安装ansible
brew install ansibleansible --versionansible [core 2.11.4]config file = Noneconfigured module search path = ['/Users/xl/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python module location = /usr/local/Cellar/ansible/4.5.0/libexec/lib/python3.9/site-packages/ansibleansible collection location = /Users/xl/.ansible/collections:/usr/share/ansible/collectionsexecutable location = /usr/local/bin/ansiblepython version = 3.9.7 (default, Sep 3 2021, 12:45:31) [Clang 12.0.0 (clang-1200.0.32.29)]jinja version = 3.0.1libyaml = True
