当前位置: 首页 > 科技观察

一篇让你快速熟悉ansible并使用

时间:2023-03-13 20:50:58 科技观察

的文章#基本概述Ansible是一款开源自动化工具,基于pythonparamiko模块开发,分布式,无需客户端官网网址:https://www.ansible.com官方文档:https://docs.ansible.com/ansible/latest/user_guide/become.html#应用场景批量执行分发任务#如:同时在1000台服务器上安装apache服务,安装完成后启动服务#如:一次性复制某个文件到1000台服务器time#特点部署简单,只需要在ansible控制端部署即可。默认是使用SSH协议进行管理。主从集中管理。配置简单,扩展性强支持API和自定义模块,强大的配置可以通过python扩展通过playbooks进行定制#架构图#安装1.搭建epel仓库(ansible仓库默认不在yum仓库中,所以需要启用epel仓库)yuminstallepel-release-y2。使用yum安装yuminstallansible-y3。查看版本Ansible--version#ansible命令参数ansible-Defineandrunasingletask'playbook'againstasetofhosts#Defineandrunasingletask"playbook"againstasetofhostsAnsible命令语法:ansible[-i:hostfile][-f:batch][组名][-m模块名][-a:模块参数]#ansible常用参数-v,--verbose#详细模式,如果命令执行成功,输出详细结果(-vv--vvv-vvvv)-iPATH,-inventory=PATH#指定host文件的路径,默认为/etc/ansible/hostsinventory[??nv?ntri]inventory-fNUM,-forks=NUM#NUM指定一个整数,默认为5,指定个数fork启动的同步进程的数量。-mNAME,-module-name=NAM#指定使用的模块名,默认使用commandmodule-a,MODULE_ARGS#指定模块module的参数-k,-ask-pass#改为提示输入ssh密码ofusingusingssh-basedKeyauthentication-sudo#指定使用sudo获取root权限-K,-ask-sudo-pass#提示输入sudo密码,配合-sudo-uUSERNAME,-user=USERNAME使用#指定移动端的执行用户-C,--check#测试什么内容会执行该命令时会发生变化,不会真正执行#ansibleviewhelpansible-doc-l#列出所有模块列表ansible-doc-smodulename#查看指定模块的参数ansible-docmodulename#查看详细参数指定模块#示例:ansible-docyum|ansible-dcofirewalld#基本使用#实验拓扑图1:定义主机列表(有两种方式:一种基于端口、用户名和密码,一种基于SSH密钥)1.基于端口、用户名和密码定义列表格式:ansible_ssh_port:指定ssh端口ansible_ssh_user:指定用户ansible_ssh_pass:指定密码ssh_sudo_pass:指定sudo的密码2.根据SSHkey访问定义列表生成秘钥ansible服务器并复制公钥到节点#ssh-keygenalwayspressenter#一键生成ssh-keygen-f~/.ssh/id_rsa-P''-q使用ssh-copy-id命令复制ansible公钥到节点ssh-copy-idroot@192.168.186.142#使用sshpass批量分发秘钥到服务器[root@gamescripts]#catscp_isa.sh#!/bin/baship=(130131132133)foriin${ip[@]}dosshpass-p"skills39"ssh-copy-id-i/root/.ssh/id_rsa.pub"-oStrictHostKeyChecking=no"root@192.168.228.$idone#服务器查看效果[root@game~]#cat/root/.ssh/known_hosts192.168.228.130ecdsa-sha2-nistp256AAAAE2VjZHNhL.....AuHi+n9iVPC0Of9D/MKK8lWuGHg=192.168.228.131ecdsa-sha2-nistp256AAAAE2VjZHNh...d8FX4mqoYCp43E2knM9t8YAZGg/k08=192.168.228.132ecdsa-sha2-nistp256AAAAE2VjZHNhL....y6U2tCs+SU/Sg+rGR8E76lqC316Q=192.168.228.133ecdsa-sha2-nistp256AAAAE2....iTuiJ+0VNOYrhh2gqW36XJVlW1+p6pj2dbo=?#服务端Testsurfacekeyloginssh192.168.228.130?#Configurationlist#/etc/ansible/hosts[db-server]192.168.186.142192.168.186.135#Networksegmentrange[all]192.168.228.[130:133]#Ifyouwritethehostname,youneedtowrite[a]k8s-masterin/etc/hosts#/etc/hosts[root@game~]#cat/etc/hosts192.168.228.130k8s-masterTwo:runontheansibleserver命令#提示:#首次运行如果不进行什么修改的话会提出警告,可以按照将deprecation改为False[DEPRECATIONWARNING]:TheTRANSFORM_INVALID_GROUP_CHARSsettingsissettoallowbadcharactersingroupnamesbydefault,thiswillchange,butstillbeuserconfigurableondeprecation.Thisfeaturewillberemovedinversion2.10.Deprecationwarningscanbedisabledbysettingdeprecation_warnings=Falseinansible.cfg.[WARNING]:Invalidcharacterswerefoundingroupnamesbutnotreplaced,use-vvvvtoseedetails#[root@k8s-master~]#vim/etc/ansible/ansible.cfgdeprecation_warnings=False1,usepingtocheckthek8s-nodesection点击本机的连接[root@k8s-master~]#ansiblek8s-node-mping#Tips:也可以指定hosts文件,但是默认是读取那个文件,所以不指定也是可以的#ansible-i/etc/ansible/hostsk8s-node-mping192.168.86.133|SUCCESS=>{"ansible_facts":{"discovered_interpreter_python":"/usr/bin/python"},"changed":false,"ping":"pong"}192.168。86.132|SUCCESS=>{"ansible_facts":{"discovered_interpreter_python":"/usr/bin/python"},"changed":false,"ping":"pong"}2.检查ansible节点的运行时间[root@k8s-master~]#ansiblek8s-node-mcommand-a'uptime'192.168.86.133|CHANGED|rc=0>>23:16:34up5min,2users,loadaverage:0.00,0.03,0.03192.168.86.132|CHANGED|rc=0>>23:16:34up31min,2users,loadaverage:0.00,0.01,0.043,查看节点内核版本[root@k8s-master~]#ansiblek8s-node-a'uname-r'192.168。86.132|已更改|rc=0>>3.10.0-1062.el7.x86_64192.168.86.133|已更改|rc=0>>3.10.0-1062.el7.x86_64