| Ansible是一个自动化统一配置管理工具、集成了丰富模块以及功能组件, |
| 可以通过一个命令完成一系列的操作,进而能减少重复性的工作和维护成本,可以提高工作效率。 |
与其他自动化运维工具对比
| 1.puppet 学习难,安装ruby环境难,没有远程执行功能 |
| 2.ansible 轻量级,大规模环境下只通过ssh会很慢,串行的 |
| 3.saltstack 一般选择salt会使用C/S结构的模式,salt-master和salt-minion,并行的,大规模批量操作的情况下,会比Ansible速度快一些,底层使用的是zero-MQ消息队列 |
运维工程师角色
| 1.提高工作效率 |
| 2.提高了工作的准确度 |
| 3.减少人员成本 |
| 4.减少了重复的工作 |
| 1.远程执行 |
| 批量执行远程命令,可以对多台主机进行远程操作 |
| |
| 2.配置管理 |
| 批量配置软件服务,可以进行自动化方式配置,服务的统一配置管理,和启停 |
| |
| 3.事件驱动 |
| 通过Ansible的模块,对服务进行不同的事件驱动 |
| 比如: |
| 1)修改配置后重启 |
| 2)只修改配置文件,不重启 |
| 3)修改配置文件后,重新加载 |
| 4)远程启停服务管理 |
| |
| 4.管理公有云 |
| 通过API接口的方式管理公有云,不过这方面做的不如saltstack. |
| saltstack本身可以通过saltcloud管理各大云厂商的云平台。 |
| |
| 5.二次开发 |
| 因为语法是Python,所以便于运维进行二次开发。 |
| |
| 6.任务编排 |
| 可以通过playbook的方式来统一管理服务,并且可以使用一条命令,实现一套架构的部署 |
| |
| 7.跨平台,跨系统 |
| 几乎不受到平台和系统的限制 |
| 1.连接插件connection plugins:用于连接主机/被管理端 |
| 2.核心模块core modules :总控, 它负责调用具体的模块来做具体的事情 |
| 3.自定义模块custom modules :根据自己的需求编写具体的模块 |
| 4.插件plugins :完成模块功能的补充,例如Email、loggin |
| 5.任务副本/剧本Play Books :剧本playbookansible的配置文件,将多个任务定义在剧本中,由ansible自动执行 |
| 6.主机清单Host inventor :定义ansible需要操作的主机信息 |
| |
| 最重要的一点是 ansible是模块化的 它所有的操作都依赖于模块 |
| 1.Ansible读取Play Book剧本,剧本中会记录对哪些主机执行哪些任务。 |
| 2.首先Ansible通过主机清单找到要执行的主机,然后调用具体的模块。 |
| 3.然后Ansible会通过连接插件连接对应的主机并推送对应的任务列表。 |
| 4.最后被管理的主机会将Ansible发送过来的任务解析为本地Shell命令执行。 |
主机 |
IP |
身份 |
m01 |
192.168.71.11 |
控制端 |
web01 |
192.168.71.14 |
受控端 |
web02 |
192.168.71.15 |
受控端 |
| |
| [root@m01 ~] |
| |
| |
| [root@m01 ~] |
快速了解安装的相关文件,后续会展开介绍
| |
| /etc/ansible/ansible.cfg `主配置文件,配置ansible工作特性` |
| /etc/ansible/hosts `主机清单` |
| /etc/ansible/roles `存放角色的目录` |
| |
| |
| /usr/bin/ansible `主程序,临时命令执行工具` |
| /usr/bin/ansible-doc `查看配置文档,模块功能查看工具` |
| /usr/bin/ansible-galaxy `下载/上传优秀代码或Roles模块的官网平台` |
| /usr/bin/ansible-playbook `定制自动化任务,编排剧本工具` |
| /usr/bin/ansible-pull `远程执行命令的工具` |
| /usr/bin/ansible-vault `文件加密工具` |
| /usr/bin/ansible-console `基于Console界面与用户交互的执行工具` |
| |
| |
| --version |
| -v |
| -i |
| -k |
| -C |
| -T |
| |
| |
| -m |
| -a |
| --syntax-check |
| |
| [root@lb ~] |
| ansible [core 2.14.9] |
| config file = /etc/ansible/ansible.cfg |
| configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] |
| ansible python module location = /usr/lib/python3.9/site-packages/ansible |
| ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections |
| executable location = /usr/bin/ansible |
| python version = 3.9.18 (main, Sep 7 2023, 00:00:00) [GCC 11.4.1 20230605 (Red Hat 11.4.1-2)] (/usr/bin/python3) |
| jinja version = 3.1.2 |
| libyaml = True |
| [root@lb ~] |
| [root@m01 ~] |
| [defaults] |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| host_key_checking = False |
| log_path = /var/log/ansible.log |
| |
| |
| [privilege_escalation] |
| |
| |
| |
| |
| |
| [root@m01 ~] |
| [web01] |
| 192.168.71.14 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1' |
| [web02] |
| 192.168.71.15 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1' |
| |
| |
| [root@lb ~] |
| 192.168.71.14 | SUCCESS => { |
| "ansible_facts": { |
| "discovered_interpreter_python": "/usr/bin/python3" |
| }, |
| "changed": false, |
| "ping": "pong" |
| } |
| |
| [root@m01 ~] |
| [web01] |
| 192.168.71.14 ansible_ssh_pass='1' |
| [web02] |
| 192.168.71.15 ansible_ssh_pass='1' |
| |
| [root@lb ~] |
| 192.168.71.15 | SUCCESS => { |
| "ansible_facts": { |
| "discovered_interpreter_python": "/usr/bin/python" |
| }, |
| "changed": false, |
| "ping": "pong" |
| } |
| |
| [root@m01 ~] |
| [web01] |
| 192.168.71.14 |
| [web02] |
| 192.168.71.15 ansible_ssh_pass='1' |
| |
| [web01:vars] |
| ansible_ssh_pass='1' |
| [root@m01 ~] |
| [web01] |
| 192.168.71.14 ansible_ssh_pass='1' |
| [web02] |
| 192.168.71.15 ansible_ssh_pass='1' |
| |
| [web_group1] |
| 192.168.71.14 ansible_ssh_pass='1' |
| 192.168.71.15 ansible_ssh_pass='1' |
| |
| [root@lb ansible] |
| 192.168.71.15 | SUCCESS => { |
| "ansible_facts": { |
| "discovered_interpreter_python": "/usr/bin/python" |
| }, |
| "changed": false, |
| "ping": "pong" |
| } |
| 192.168.71.14 | SUCCESS => { |
| "ansible_facts": { |
| "discovered_interpreter_python": "/usr/bin/python3" |
| }, |
| "changed": false, |
| "ping": "pong" |
| 1.生成密钥对 |
| [root@m01 ~] |
| |
| 2.推送公钥 |
| [root@m01 ~] |
| [root@m01 ~] |
| [root@m01 ~] |
| [web_group1] |
| 192.168.71.14 |
| 192.168.71.15 |
本地/etc/hosts文件添加解析后,也可以把ip换成主机名
| #配置主机清单 |
| [root@m01 ~]# vim /etc/ansible/hosts |
| [web_group] |
| web01 |
| web02 |
| |
| #配置hosts |
| [root@m01 ~]# vim /etc/hosts |
| 192.168.71.14 web01 |
| 192.168.71.15 web02 |
| |
| #测试 |
| [root@lb ansible]# ansible web_group1 -m ping |
| web02 | SUCCESS => { |
| "ansible_facts": { |
| "discovered_interpreter_python": "/usr/bin/python" |
| }, |
| "changed": false, |
| "ping": "pong" |
| } |
| web01 | SUCCESS => { |
| "ansible_facts": { |
| "discovered_interpreter_python": "/usr/bin/python3" |
| }, |
| "changed": false, |
| "ping": "pong" |
| } |
| [root@m01 ~] |
| [web_group1] |
| web01 |
| web02 |
| [nfs_server] |
| nfs ansible_ssh_pass='1' |
| [rsync_server] |
| backup ansible_ssh_pass='1' |
| [db_server] |
| db01 ansible_ssh_pass='1' |
| |
| [www:children] |
| web_group1 |
| nfs_server |
| rsync_server |
| |
| |
| [root@m01 ~] |
| 192.168.71.14 web01 |
| 192.168.71.15 web02 |
| 192.168.71.16 nfs |
| 192.168.71.12 backup |
| 192.168.71.13 db01 |
| [root@m01 ~] |
| hosts (4): |
| web01 |
| web02 |
| nfs |
| backup |
ansible的Host-pattern
| 匹配主机的列表: 统一用单引号包裹,防止bash解析 |
| 单台主机: |
| ansible 'web01' -m ping |
| All :表示所有Inventory中的所有主机 |
| ansible all –m ping |
| * :通配符:必须是/etc/ansible/hosts内定义的 |
| ansible '*' -m ping (*表示所有主机) |
| ansible 'web*' -m ping |
| 逻辑或:':' |
| ansible "web02:db_server" -m ping |
| ansible "web_group1:db_server" -m ping |
| ansible “192.168.1.10:192.168.1.20” -m ping |
| 逻辑与 ':&' |
| ansible "web_group1:&db_server" -m ping |
| 逻辑非 ':!' |
| ansible 'web_group1:!db_server' -m ping |
| 综合逻辑 |
| ansible 'group1:&group2:&group3:!group4' -m ping |
| |
| ansible的host-pattern中:“与”(AND) > “或”(OR) > “非”(NOT)。 |
| python中:NOT最高,然后是AND,最后是OR。 |
| |
| |
| [root@lb ansible] |
| [group1] |
| 192.168.71.12 ansible_ssh_pass='1' |
| 192.168.71.13 ansible_ssh_pass='1' |
| [group2] |
| 192.168.71.12 ansible_ssh_pass='1' |
| 192.168.71.13 ansible_ssh_pass='1' |
| 192.168.71.14 ansible_ssh_pass='1' |
| [group3] |
| 192.168.71.12 ansible_ssh_pass='1' |
| 192.168.71.13 ansible_ssh_pass='1' |
| 192.168.71.15 ansible_ssh_pass='1' |
| [group4] |
| 192.168.71.13 ansible_ssh_pass='1' |
| [root@lb ansible] |
| 192.168.71.12 | SUCCESS => { |
| "ansible_facts": { |
| "discovered_interpreter_python": "/usr/bin/python3" |
| }, |
| "changed": false, |
| "ping": "pong" |
| } |
| |
| 正则表达式 |
| ansible '~(db|web).*' -m ping |
| ad-hoc模式简而言之就是"临时命令",执行完即结束,并不会保存 |
| 语法 |
| ansible [pattern] -m [module] -a "[module options]" |
| 比如在多台机器上查看某个进程是否启动,或拷贝指定文件到本地,等等 |
| 临时使用的命令,一次使用 |
| [root@m01 ~] |
| web01 | CHANGED | rc=0 >> |
| Filesystem Size Used Avail Use% Mounted on |
| /dev/sda3 18G 1.6G 17G 9% / |
| devtmpfs 476M 0 476M 0% /dev |
| tmpfs 487M 0 487M 0% /dev/shm |
| tmpfs 487M 7.7M 479M 2% /run |
| tmpfs 487M 0 487M 0% /sys/fs/cgroup |
| /dev/sda1 1014M 127M 888M 13% /boot |
| tmpfs 98M 0 98M 0% /run/user/0 |
| |
| [root@m01 ~] |
| web01 | CHANGED | rc=0 >> |
| total used free shared buff/cache available |
| Mem: 972 128 481 7 362 658 |
| Swap: 1023 0 1023 |
| web02 | CHANGED | rc=0 >> |
| total used free shared buff/cache available |
| Mem: 972 111 551 7 309 691 |
| Swap: 1023 0 1023 |
| 绿色: 代表被管理端主机没有被修改 |
| 黄色: 代表被管理端主机发现变更 |
| 红色: 代表出现了故障,注意查看提示 |
| command |
| shell |
| scripts |
| yum_repository |
| yum |
| copy |
| file |
| service |
| systemd |
| mount |
| cron |
| get_url |
| firewalld |
| selinux |
| setup |
| |
| [root@m01 ~] |
| web01 | CHANGED | rc=0 >> |
| total used free shared buff/cache available |
| Mem: 972 128 480 7 363 658 |
| Swap: 1023 0 1023 |
| |
| |
| [root@m01 ~] |
| web01 | FAILED | rc=1 >> |
| error: garbage option |
| Usage: |
| ps [options] |
| |
| Try 'ps --help <simple|list|output|threads|misc|all>' |
| or 'ps --help <s|l|o|t|m|a>' |
| for additional help text. |
| For more details see ps(1).non-zero return code |
| |
| [root@m01 ~] |
| web01 | CHANGED | rc=0 >> |
| root 12680 12675 0 17:51 pts/0 00:00:00 /bin/sh -c ps -ef | grep nginx |
| root 12682 12680 0 17:51 pts/0 00:00:00 grep nginx |
| |
| |
| [root@m01 ~] |
| web01 | CHANGED | rc=0 >> |
| inet 10.0.0.7 netmask 255.255.255.0 broadcast 10.0.0.255 |
| |
| [root@m01 ~] |
| web01 | CHANGED | rc=0 >> |
| 10.0.0.7 |
| [root@m01 ~] |
| |
| [root@m01 ~] |
| |
| |
| [root@m01 ~] |
| |
| |
| [root@m01 ~] |