自动化利器 Ansible - 从了解到应用( 二 )

# 配置文件目录/etc/ansible/- ansible.cfg# Ansible 主配置文件- hosts# Inventory,定义目标主机清单的文件- roles# 角色目录,用于定义角色# 执行文件目录/usr/bin/# Lib库依赖目录/usr/lib/python2.7/site-packages/ansible# Help文档目录/usr/share/doc/ansible-2.9.27/# Ansible模块目录/usr/share/ansible/- collections# 目录- plugins# 插件模块目录- roles# 角色模块目录Ansible 配置文件默认Ansible配置文件存放在 /etc/ansible/ansible.cfg
# 常见参数inventory = /etc/ansible/hosts# 主机清单inventory文件的位置library = /usr/share/ansible# 存放Ansible模块的目录,支持用“:”隔开多个目录forks = 5# 并发连接数,默认为5remote_port = 22# 连接主机节点的端口,默认为22端口,建议修改host_key_checking = False# 是否检查SSH主机的密钥,值为True/Falsetimeout = 60# SSH连接的超时时间,单位为秒log_path = /var/log/ansible.log# 存储ansible日志的文件(默认不开启)Ansible 执行文件$ ll /usr/bin/*ansible*# 相关执行文件lrwxrwxrwx 1 root root20 Mar 172022 /usr/bin/ansible -> /usr/bin/ansible-2.7lrwxrwxrwx 1 root root20 Mar 172022 /usr/bin/ansible-2 -> /usr/bin/ansible-2.7-rwxr-xr-x 1 root root5933 Jan 162022 /usr/bin/ansible-2.7lrwxrwxrwx 1 root root7 Mar 172022 /usr/bin/ansible-config -> ansible-rwxr-xr-x 1 root root 13432 Jan 162022 /usr/bin/ansible-connectionlrwxrwxrwx 1 root root28 Mar 172022 /usr/bin/ansible-console -> /usr/bin/ansible-console-2.7lrwxrwxrwx 1 root root28 Mar 172022 /usr/bin/ansible-console-2 -> /usr/bin/ansible-console-2.7lrwxrwxrwx 1 root root7 Mar 172022 /usr/bin/ansible-console-2.7 -> ansiblelrwxrwxrwx 1 root root24 Mar 172022 /usr/bin/ansible-doc -> /usr/bin/ansible-doc-2.7lrwxrwxrwx 1 root root24 Mar 172022 /usr/bin/ansible-doc-2 -> /usr/bin/ansible-doc-2.7lrwxrwxrwx 1 root root7 Mar 172022 /usr/bin/ansible-doc-2.7 -> ansiblelrwxrwxrwx 1 root root27 Mar 172022 /usr/bin/ansible-galaxy -> /usr/bin/ansible-galaxy-2.7lrwxrwxrwx 1 root root27 Mar 172022 /usr/bin/ansible-galaxy-2 -> /usr/bin/ansible-galaxy-2.7lrwxrwxrwx 1 root root7 Mar 172022 /usr/bin/ansible-galaxy-2.7 -> ansiblelrwxrwxrwx 1 root root7 Mar 172022 /usr/bin/ansible-inventory -> ansiblelrwxrwxrwx 1 root root29 Mar 172022 /usr/bin/ansible-playbook -> /usr/bin/ansible-playbook-2.7lrwxrwxrwx 1 root root29 Mar 172022 /usr/bin/ansible-playbook-2 -> /usr/bin/ansible-playbook-2.7lrwxrwxrwx 1 root root7 Mar 172022 /usr/bin/ansible-playbook-2.7 -> ansiblelrwxrwxrwx 1 root root25 Mar 172022 /usr/bin/ansible-pull -> /usr/bin/ansible-pull-2.7lrwxrwxrwx 1 root root25 Mar 172022 /usr/bin/ansible-pull-2 -> /usr/bin/ansible-pull-2.7lrwxrwxrwx 1 root root7 Mar 172022 /usr/bin/ansible-pull-2.7 -> ansiblelrwxrwxrwx 1 root root26 Mar 172022 /usr/bin/ansible-vault -> /usr/bin/ansible-vault-2.7lrwxrwxrwx 1 root root26 Mar 172022 /usr/bin/ansible-vault-2 -> /usr/bin/ansible-vault-2.7lrwxrwxrwx 1 root root7 Mar 172022 /usr/bin/ansible-vault-2.7 -> ansible$Ansible 常见模块Ansible基于模块工作,本身没有批量部署的能力 。可以理解为Ansilbe提供了框架来运行具备批量部署能力的指定功能模块 。
# 通过 ansilbe-doc 命令查看模块帮助信息,类似man命令ansible-doc -l# List available pluginsansible-doc -l | grep "copy"# 查找名称包含copy字符的模块ansible-doc <module_name># 显示模块的说明信息ansible-doc -s <module_name># Show playbook snippet for specified plugin(s)# 常见模块## command在指定主机上执行命令(默认模块),不支持变量、管道、重定向等shell特性## ping测试指定主机连接性## shell在指定主机上执行命令或运行脚本,打开远程主机的shell进程的一个子shell运行命令,支持shell的变量、管道、重定向等特性## script调用本地脚本在远程主机执行## stat获取文件信息## setup主机系统信息,收集facts## copy复制文件到远程主机的指定位置## fetch复制指定主机的文件到本地## get_url在远程主机下载指定的url地址,支持文件校验## file设置文件属性## cron设置计划任务## group管理用户组## user管理用户## service管理服务状态## yum管理程序包## hostname管理主机名称## git代码及版本管理## B后台管理## assemble文件组装,可以将多份配置文件组装成一份配置文件## iniini文件管理模块## urlweb请求,发送HTTP协议请求并得到返回的状态码## aptAPT包管理模块

经验总结扩展阅读