如何在智能告警平台CA触发测试告警
987
2022-10-02
运维管理工具之Ansible
逆境前行,能帮你的只有自己
Ansible简介
Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具。它用Python写成,类似于saltstack和Puppet,但是有一个不同和优点是我们不需要在节点中安装任何客户端。它使用SSH来和节点进行通信。 Ansible基于 Python paramiko 开发,分布式,无需客户端,轻量级,配置语法使用 YMAL 及 Jinja2模板语言,更强的远程命令执行操作官方网站:https://ansible.com/
IT业界励志事件:
2015年10月,红帽(Red Hat)宣布收购软件开发公司 Ansible,消息称此次收购耗资逾 1亿美元,也有消息称接近 1.5亿美元。
Ansible 成立于 2013年,总部设在北卡罗来纳州达勒姆,联合创始人 aïd Ziouani 和高级副总裁 Todd Barr 都是红帽的老员工。Ansible 旗下的开源软件 Ansible 十分流行。这家公司还提供 Tower 软件和咨询服务,这个款软件能使开发者轻松地建立和管理规模化应用程序的 IT 基础架构。
Anisable特点
1、部署简单,只需在主控端部署Ansible环境,被控端无需做任何操作;
2、默认使用SSH协议对设备进行管理;
3、主从集中化管理;
4、配置简单、功能强大、扩展性强;
5、支持API及自定义模块,可通过Python轻松扩展;
6、通过Playbooks来定制强大的配置、状态管理
7、对云计算平台、大数据都有很好的支持;
Anisable机理
Ansible 在管理节点将 Ansible 模块通过 SSH 协议推送到被管理端执行,执行完之后自动删除,可以使用 SVN 等来管理自定义模块及编排。
由上面的图可以看到 Ansible 的组成由 5 个部分组成:
Ansible : ansible核心
Modules : 包括 Ansible 自带的核心模块及自定义模块
Plugins : 完成模块功能的补充,包括连接插件、邮件插件等
Playbooks : 剧本;定义 Ansible 多任务配置文件,由Ansible 自动执行
Inventory : 定义 Ansible 管理主机的清单 [ˈɪnvəntri] 清单
Anisable实验
实验环境
&实验环境****************************************************Anisable服务器 192.168.159.145 ansible.cn 被管理服务器1 192.168.159.145 ansible.cn被管理服务器2 192.168.159.141 minion.cn****************************************************&关闭防火墙 [root@ansible ~]# systemctl stop firewalld && systemctl disable firewalld[root@server ~]# systemctl stop firewalld && systemctl disable firewalld&关闭selinux[root@ansible ~]# setenforce 0[root@server ~]# setenforce 0&DNS服务配置[root@ansible ~]# vim etc/hosts****************************************************192.168.159.141 server.cn192.168.159.145 ansible.cn****************************************************[root@server ~]# vim etc/hosts****************************************************192.168.159.141 server.cn192.168.159.145 ansible.cn****************************************************&安装epel源[root@ansible ~]# yum install -y epel-release[root@server ~]# yum install -y epel-release&安装ansible[root@ansible ~]# yum install -y ansible&查看版本 [root@ansible ~]# ansible --versionansible 2.8.0 config file = etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = usr/lib/python2.7/site-packages/ansible executable location = usr/bin/ansible python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
Ansible命令参数
& anisble命令语法: ansible [-i 主机文件] [-f 批次] [组名] [-m 模块名称] [-a 模块参数] & ansible详细参数: -v,–verbose #详细模式,如果命令执行成功,输出详细的结果 (-vv –vvv -vvvv) -i PATH, -inventory=PATH #指定 host 文件的路径,默认是在 /etc/ansible/hosts -f NUM,-forks=NUM #NUM 是指定一个整数,默认是 5 ,指定 fork 开启同步进程的个数。 -m NAME,-module-name=NAME #指定使用的 module 名称,默认使用 command模块 -a,MODULE_ARGS #指定 module 模块的参数 -k,-ask-pass #提示输入 ssh 的密码,而不是使用基于 ssh 的密钥认证 -sudo #指定使用 sudo 获得 root 权限 -K,-ask-sudo-pass #提示输入 sudo 密码,与 -sudo 一起使用 -u USERNAME,-user=USERNAME # 指定移动端的执行用户 -C,–check #测试此命令执行会改变什么内容,不会真正的去执行& ansible-doc详细参数: ansible-doc -l #列出所有的模块列表 ansible-doc -s 模块名 #查看指定模块的参数 -s, --snippet # [ˈsnɪpɪt] 片断
定义主机清单(基于端口用户密码定义主机清单)
&基于|端口|用户|密码|定义主机清单ansible基于ssh连接-i (inventory)参数后指定的远程主机时,也可以写端口,用户,密码。格式:ansible_ssh_port:指定ssh端口 ansible_ssh_user:指定 ssh 用户 ansible_ssh_pass:指定 ssh 用户登录是认证密码(明文密码不安全) ansible_sudo_pass:指明 sudo 时候的密码&例[root@ansible ~]# vim etc/ansible/hosts #文件 /etc/ansible/hosts 维护着Ansible中服务器的清单。在文件最后追加以下内容****************************************************[web-servers] #主机组名192.168.159.145 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=你的密码192.168.159.141 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=你的密码****************************************************&简单测试[root@ansible ~]# ansible -i etc/ansible/hosts web-servers -m ping &报错![DEPRECATION WARNING]: The TRANSFORM_INVALID_GROUP_CHARS settings is set to allow bad characters in group names by default, this will change, but still be user configurable on deprecation. This feature will be removed in version 2.10. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg. [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details192.168.159.145 | FAILED! => { "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."}192.168.159.141 | FAILED! => { "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."}&解决!手动连接一下/etc/ansible/hosts主机清单中的主机,这样就可以在ansible服务器上保存目标主机的fingerprint指纹。后期可以正常连接了[root@ansible ~]# ssh root@192.168.159.141[root@server ~]# ssh root@192.168.159.145&继续测试 [root@ansible ~]# ansible -i etc/ansible/hosts web-servers -m ping192.168.159.141 | SUCCESS => { #表示成测试。通信成功。 "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, #因为ping命令不会改变被管理的服务器的状态。所以是false正常 "ping": "pong"}192.168.159.145 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong"}
定义主机清单(基于ssh密钥来访问定义主机清单)
&基于ssh密钥来访问定义主机清单一般来说,使用明文密码不安全,所以增加主机无密码访问在Ansible服务端生成密钥,并且复制公钥到节点中[root@ansible ~]# ssh-keygen -t rsa #一路回车Generating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): --------------[root@ansible ~]# ls ~/.ssh/id_rsa id_rsa.pub known_hosts&使用ssh-copy-id命令来复制Ansible公钥到节点:ansible.cn server.cn[root@ansible ~]# ssh-copy-id root@192.168.159.145[root@ansible ~]# ssh-copy-id root@192.168.159.141&修改主机清单[root@ansible ~]# vim etc/ansible/hosts****************************************************[web-servers] #主机组名192.168.159.145 # ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=8760346709192.168.159.141 # ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=8760346709****************************************************&测试[root@ansible ~]# ansible -i etc/ansible/hosts web-servers -m ping192.168.159.141 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong"}192.168.159.145 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong"}
Anisable模块高级用法
1.ansible常用的支撑模块
(1)、command模块: 为ansible默认模块,不指定-m参数时,使用的就是command模块; comand模块比较简单,常见的命令都可以使用,但其命令的执行不是通过shell执行的,所以,像这些 "<", ">", "|", and "&"操作都不可以,当然,也就不支持管道; 缺点:不支持管道,没法批量执行命令;(2)、shell模块: 使用shell模块,在远程命令通过/bin/sh来执行; 所以,我们在终端输入的各种命令方式,都可以使用。 &例:运行free-m命令 [root@ansible ~]# ansible -i etc/ansible/hosts web-servers -m shell -a 'free -m' 192.168.159.145 | CHANGED | rc=0 >> total used free shared buff/cache available Mem: 972 284 136 7 551 470 Swap: 2047 0 2047 192.168.159.141 | CHANGED | rc=0 >> total used free shared buff/cache available Mem: 972 198 466 7 307 584 Swap: 2047 0 2047 &注:但是我们自己定义在~/.bashrc或~/.bash_profile中的环境变量shell模块由于没有加载,所以无法识别;如果需要使用自定义的环境变量,就需要在最开始,执行加载自定义脚本的语句; [root@ansible ~]# ansible -i /etc/ansible/hosts web-servers -m shell -a "source ~/.bash_profile && df -h | grep sda1" 192.168.159.141 | CHANGED | rc=0 >> dev/sda1 1014M 133M 882M 14% boot 192.168.159.145 | CHANGED | rc=0 >> /dev/sda1 1014M 133M 882M 14% /boot(3)、scripts模块 使用scripts模块可以在本地写一个脚本,在远程服务器上执行: [root@ansible ~]# vim etc/ansible/test.sh **************************************************** #!/bin/bash date hostname uname -r **************************************************** [root@ansible ~]# ansible -i etc/ansible/hosts web-servers -m script -a etc/ansible/test.sh 192.168.159.145 | CHANGED => { "changed": true, "rc": 0, "stderr": "Shared connection to 192.168.159.145 closed.\r\n", "stderr_lines": [ "Shared connection to 192.168.159.145 closed." ], "stdout": "2019年 05月 28日 星期二 17:20:09 CST\r\nansible\r\n3.10.0-957.el7.x86_64\r\n", "stdout_lines": [ "2019年 05月 28日 星期二 17:20:09 CST", "ansible", "3.10.0-957.el7.x86_64" ]}192.168.159.141 | CHANGED => { "changed": true, "rc": 0, "stderr": "Shared connection to 192.168.159.141 closed.\r\n", "stderr_lines": [ "Shared connection to 192.168.159.141 closed." ], "stdout": "2019年 05月 28日 星期二 17:20:09 CST\r\nserver\r\n3.10.0-957.el7.x86_64\r\n", "stdout_lines": [ "2019年 05月 28日 星期二 17:20:09 CST", "server", "3.10.0-957.el7.x86_64" ]}
2.copy模块
&实现主控端向目标主机拷贝文件,类似scp功能例1:把ansible主机上的/etc/hosts文件复制到主机组中机器的/tmp目录下[root@ansible ~]# ansible web-servers -m copy -a 'src=/etc/hosts dest=/tmp/ owner=root group=root mode=755'192.168.159.141 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "9d66ad21244f99ec70c3ef64857a022aff35fd64", "dest": "/tmp/hosts", "gid": 0, "group": "root", "md5sum": "0f4e992d425578550e77c7ef8db235a5", "mode": "0755", "owner": "root", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 213, "src": "/root/.ansible/tmp/ansible-tmp-1559035562.88-256738484716180/source", "state": "file", "uid": 0}192.168.159.145 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "9d66ad21244f99ec70c3ef64857a022aff35fd64", "dest": "/tmp/hosts", "gid": 0, "group": "root", "md5sum": "0f4e992d425578550e77c7ef8db235a5", "mode": "0755", "owner": "root", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 213, "src": "/root/.ansible/tmp/ansible-tmp-1559035562.87-171973789106857/source", "state": "file", "uid": 0}&验证结果[root@ansible ~]# ll tmp/hosts -rwxr-xr-x. 1 root root 213 5月 28 17:26 tmp/hosts[root@server ~]# ll tmp/hosts -rwxr-xr-x. 1 root root 213 5月 28 17:26 /tmp/hosts
3.file模块
&设置文件属性[root@ansible ~]# ansible web-servers -m file -a "path=/tmp/hosts mode=0777" 192.168.159.145 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "gid": 0, "group": "root", "mode": "0777", "owner": "root", "path": "/tmp/hosts", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 213, "state": "file", "uid": 0}192.168.159.141 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "gid": 0, "group": "root", "mode": "0777", "owner": "root", "path": "/tmp/hosts", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 213, "state": "file", "uid": 0} &查验[root@ansible ~]# ll tmp/hosts -rwxrwxrwx. 1 root root 213 5月 28 17:26 tmp/hosts[root@server ~]# ll tmp/hosts-rwxrwxrwx. 1 root root 213 5月 28 17:26 /tmp/hosts
4.stat模块
&获取远程文件信息[root@ansible ~]# ansible web-servers -m stat -a 'path=/tmp/hosts'192.168.159.145 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "stat": { "atime": 1559035565.2709994, "attr_flags": "", "attributes": [], "block_size": 4096, "blocks": 8, "charset": "us-ascii", "checksum": "9d66ad21244f99ec70c3ef64857a022aff35fd64", "ctime": 1559036480.5205803, "dev": 2051, "device_type": 0, "executable": true, "exists": true, "gid": 0, "gr_name": "root", "inode": 68193329, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mimetype": "text/plain", "mode": "0777", "mtime": 1559035564.5179918, "nlink": 1, "path": "/tmp/hosts", "pw_name": "root", "readable": true, "rgrp": true, "roth": true, "rusr": true, "size": 213, "uid": 0, "version": "775015968", "wgrp": true, "woth": true, "writeable": true, "wusr": true, "xgrp": true, "xoth": true, "xusr": true }}
5.get_url模块
&模块实现远程主机下载指定url到本地,支持sha256sum文件校验例如:下载epel-release-latest-7.noarch.rpm到主机清单中的/tmp/目录下 ansible -i etc/ansible/hosts web-servers -m get_url -a "url=https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm dest=/tmp/ mode=0440 force=yes" 注:url=https://xxx 的等号=前后不能有空格&扩展:查看force=yes的作用 [root@ansible ~]# ansible-doc -s get_url force: # If `yes' and `dest' is not a directory, will download the file every time and replace the file if the contents change. If `no', the file will only be downloaded if the destination does not exist. Generally should be `yes' only for small local files. Prior to 0.6, this module behaved as if `yes' was the default.如果force=yes,当下载文件时,如果所下的内容和原目录下的文件内容不一样,则替换原文件,如果一样,就不下载了。如果为“否”,则仅在目标不存在时才下载文件。 一般来说,只有小型本地文件才应该为“是”。 在0.6之前,该模块表现为默认为“是”。
6.yum模块
&linux平台软件包管理yum模块可以提供的status状态: latest ,present,installed #这3个代表安装;removed, absent #后面2个是卸载&例:安装httpd软件[root@ansible ~]# ansible web-servers -m yum -a 'name=httpd state=latest'192.168.159.145 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "changes": { "installed": [], "updated": [] }, "msg": "", "rc": 0, "results": [ "All packages providing httpd are up to date", "" ]}192.168.159.141 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "changes": { "installed": [], "updated": [] }, "msg": "", "rc": 0, "results": [ "All packages providing httpd are up to date", "" ]}
7.cron模块
&远程主机crontab配置例如:增加每30分钟执行ls /tmp[root@ansible ~]# ansible web-servers -m cron -a "name=list_dir minute='*/30' job='ls /tmp' "192.168.159.141 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "envs": [], "jobs": [ "list_dir" ]}192.168.159.145 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "envs": [], "jobs": [ "list_dir" ]}&验证[root@ansible ~]# crontab -l#Ansible: list_dir*/30 * * * * ls /tmp[root@server ~]# crontab -l#Ansible: list_dir*/30 * * * * ls /tmp
8.service模块
&远程主机系统服务管理service模块常用参数:(1)、name参数:此参数用于指定需要操作的服务名称,比如 nginx,httpd。 (2)、state参数:此参数用于指定服务的状态,比如,我们想要启动远程主机中的httpd,则可以将 state 的值设置为 started;如果想要停止远程主机中的服务,则可以将 state 的值设置为 stopped。此参数的可用值有 started、stopped、restarted(重启)、reloaded。 enabled参数:此参数用于指定是否将服务设置为开机 启动项,设置为 yes 表示将对应服务设置为开机启动,设置为 no 表示不会开机启动。注:想使用service模块启动服务,被启动的服务,必须可以使用service 命令启动或关闭&例远程启动apache[root@ansible ~]# ansible web-servers -m service -a "name=httpd state=started enabled=yes"192.168.159.145 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "enabled": true, "name": "httpd", "state": "started", "status": { "ActiveEnterTimestamp": "二 2019-05-28 14:44:39 CST", "ActiveEnterTimestampMonotonic": "17795521", "ActiveExitTimestampMonotonic": "0", "ActiveState": "active", "After": "systemd-journald.socket remote-fs.target system.slice basic.target nss-lookup.target network.target tmp.mount -.mount", "AllowIsolate": "no", "AmbientCapabilities": "0", ... }}
9.sysctl模块
&远程主机sysctl配置例:开启路由转发功能[root@ansible ~]# ansible web-servers -m sysctl -a "name=net.ipv4.ip_forward value=1 reload=yes"192.168.159.141 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true}192.168.159.145 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true} &验证[root@ansible ~]# cat /proc/sys/net/ipv4/ip_forward1[root@server ~]# cat /proc/sys/net/ipv4/ip_forward1
10.user模块
&远程主机用户管理例[root@ansible ~]# ansible web-servers -m user -a "name=kawayi state=present"192.168.159.145 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 1003, "home": "/home/kawayi", "name": "kawayi", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1002}192.168.159.141 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 1002, "home": "/home/kawayi", "name": "kawayi", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1001}&验证[root@ansible ~]# id kawayiuid=1002(kawayi) gid=1003(kawayi) 组=1003(kawayi)
明天继续playbook模块!
明天继续playbook模块!
明天继续playbook模块!
明天继续playbook模块!
明天继续playbook模块!
明天继续playbook模块!
发表评论
暂时没有评论,来抢沙发吧~