ansible 是什么Ansible 是一种 IT 自动化工具。它可以配置系统部署软件以及协调更高级的 IT 任务例如持续部署滚动更新。Ansible 适用于管理企业 IT 基础设施从具有少数主机的小规模到数千个实例的企业环境。Ansible 也是一种简单的自动化语言可以完美地描述 IT 应用程序基础结构ansible 的好处简单易读基于 YAML 文本编写易于阅读非专业的开发人员也可以编写。功能强大它可以同于管理配置软件安装流程自动化无代理不需要在客户端安装额外的 agent跨平台支持支持 linuxWindowsUnix 和网络设备ansible 架构#ansible架构控制节点(Control Node)控制节点是执行 Ansible 操作的主机。它通常是您的工作站或服务器运行 Ansible 命令和管理配置的主机。主机(Hosts)也称为目标节点是您希望管理和配置的远程服务器或设备。Ansible 通过 SSH 连接到这些主机执行任务。Inventory(清单)清单是一个定义了 Ansible 将要管理的主机的文件。它可以包含主机的 IP 地址、主机名、组等信息。清单文件可以是静态的手动编写或者动态的由脚本或其他工具生成。Playbooks(剧本)Playbooks 是 Ansible 的核心概念之一它们是用 YAML 格式编写的文件用于描述一系列任务的执行。每个任务都描述了在目标主机上应该执行的操作比如安装软件包、配置文件、启动服务等。模块(Modules)模块是 Ansible 的工作单元用于执行特定的任务。Ansible 提供了丰富的内置模块可以用于系统管理、网络管理、云管理等各种场景。例如yum 模块用于在基于 RPM 的系统上安装软件包而 copy 模块用于复制文件到远程主机。ansible 安装主机端(master):192.168.7.42被控端(node)192.168.7.99、192.168.7.217、192.168.7.42在主机端安装yum install -y epel-release yum install -y ansible #ansible配置文件 /etc/ansible/ansible.cfg /etc/ansible/hosts 生成秘钥 ssh-keygen -t rsa #在/root/.ssh/目录下生成秘钥 #被控端(复制主机端公钥到被控端) ssh-copy-id -i /root/.ssh/id_rsa.pub root192.168.7.99或者scp /root/.ssh/id_rsa.pub 192.168.7.99:/root/.ssh/authorized_keys chmod 600 /root/.ssh/authorized_keys 本机也要操作 ssh-copy-id -i /root/.ssh/id_rsa.pub root192.168.7.42 或者 cat /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys 测试 ssh 192.168.7.99 ifconfig ssh 192.168.7.217 ifconfig#测试ansible配置是否正常配置ansible 主机清单cat /etc/ansible/hosts //主机清单 [testhosts] //按组分 192.168.7.42 192.168.7.99 192.168.7.217 [lbtest] 192.168.7.[42-45] 表示42-45内的ip ansible -i host_pattern [-f forks] [-m module_name] [-a args] -i:指定主机清单的路径默认为/etc/ansible/hosts可省略 -m module:默认为command -f forks :默认为5个主机同时执行 例如ansible testhosts -m command -a service salt-minion startansible testhosts -u root -k -m shell -a ps axu|grep salt备注:-u 指定用户名-k 指定密码-m 指定模块-a 指定参数command不支持管道此时可以用shell#ansible command vs ansible shell使用childrencat /etc/ansible/hosts [tests:children] //定义子项 test1 test2 test3 [test1] 192.168.7.42 [test2] 192.168.7.99 [test3] 192.168.7.217#ansible pingansible支持主机列表的正则匹配*/all 全量 : 逻辑或 逻辑与 ! 逻辑非 [] 切片 ~ 以~开头 如 ansible all -m ping #所有默认inventory文件中的机器 ansible * -m ping #同上 ansible 121.28.13.* -m ping #所有122.28.13.X机器 ansible web1:web2 -m ping #所有属于组web1或属于web2的机器 ansible web1:!web2 -m ping #属于组web1但不属于web2的机器 ansible web1web2 -m ping #属于组web1又属于web2的机器 ansible webserver[0] -m ping #属于组webserver的第1台机器 ansible webserver[0:5] -m ping #属于组webserver的第1到4台机器 ansible ~(beta|web)\.example\.(com|org) -m pingansible 常用模块官方模块文档https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html下面是总结的我常用的模块时间长了有时候会忘用的时候忘记了来翻译下file:用于配置文件属性 script:用于在远程机器上执行本地脚本 yum用于安装软件包 cron配置计划任务 copy复制文件到远程主机 command在远程主机上执行命令,不支持管道 raw类似于command模块支持管道 user配置用户 group配置用户组 service用于管理服务 ping用于检测远程主机是否存活 如ansible test1 -m ping setup查看远程主机的基本信息,获取到的主机信息其中的KEY都可以在playbook中被当作变量引用 如 {{ ansible_all_ipv4_addresses }} mount配置挂载点 script 模块用于在远程机器上执行本地脚本 script模块在指定节点上执行/root/a.sh脚本(该脚本是在ansible控制节点上的) fetch从远程主机拉取文件到本地 archive模块用于压缩文件 unarchive模块用于解压文件 selinux:设置selinux防火墙 iptables:设置iptables防火墙 synchronize使用rsync同步文件 get_url:主要用于从http、ftp、https服务器上下载文件类似于wget filesystem:在块设备上创建文件系统file模块group定义文件/目录的属组 owner定义文件/目录的属主 mode定义文件/目录的权限 path/name必选项定义文件/目录的路径 recurse递归设置文件的属性只对目录有效 state:定义文件状态 - directory如果目录不存在创建目录 - touch如果文件不存在创建一个新文件 - absent:删除文件或目录 - link创建软连接 - hard创建硬链接(src指定要被链接的源文件路径dest被链接到的路径,只应用于statelink情况) - file即使文件不存在也不会被创建 示例 ansible test1 -m file -a src/etc/fstab dest/tmp/fstab statelink ansible test1 -m command -a ls /tmp/ -lh ansible test -m file -a name/ane/soft statedirectory ansible test1 -m file -a path/tmp/fstab stateabsent ansible test1 -m file -a path/tmp/test statetouch ansible test1 -m file -a path/tmp/d1 statedirectory ownerroot grouproot mode755 //创建目录 ansible test1 -m command -a path/tmp/ ansible test1 -m file -a path/tmp/d1 stateabsent 删除目录command模块creates一个文件名当该文件存在则该命令不执行 free_form要执行的linux指令 chdir在执行指令之前先切换到该指定的目录 ansible test2 -m command -a chdir/ane tar zcf 666.tar.gz 666.txt ansible test2 -m command -a ls -l /ane removes一个文件名当该文件不存在则该选项不执行 示例 ansible test -a /sbin/rebootyum模块enablerepo:启用某个源 name:要进行操作的软件包的名字也可以传递一个url或者一个本地的rpm包的路径 state:定义软件包状态 present:安装 absent删除 latest安装最新的 示例 ansible test -m yum -a namehttpd statelatest ansible test -m yum -a nameDevelopment tools statepresent ansible test -m yum -a namehttp://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm statepresent enablerepo:启用某个源service模块arguments给命令行提供一些选项 enabled是否开机启动 yes|no name必选项服务名称 runlevel运行级别 sleep如果执行了restarted在则stop和start之间沉睡几秒钟 state对当前服务执行启动停止、重启、重新加载等操作started,stopped,restarted,reloaded 示例 ansible test -m service -a namehttpd statestarted enabledyes ansible test -m service -a namefoo pattern/usr/bin/foo statestarted ansible test -m service -a namenetwork staterestarted argseth0copy模块src:源文件 dest:目标路径 backup:覆盖之前是否备份原文件 owner:设定文件/目录的属主 group:设定文件/目录的属组 mode:设定文件/目录的权限 示例 ansible test -m copy -a src/srv/myfiles/foo.conf dest/etc/foo.conf ownerfoo groupfoo mode0644 ansible test -m copy -a src/mine/ntp.conf dest/etc/ntp.conf ownerroot grouproot mode644 backupyescron模块backup对远程主机上的原任务计划内容修改之前做备份 day日1-31**/2,…… hour小时0-23**/2…… minute分钟0-59**/2…… month月1-12**/2…… weekday周0-7*…… job要执行的任务依赖于statepresent name该任务的描述 special_time指定什么时候执行参数reboot,yearly,annually,monthly,weekly,daily,hourly state确认该任务计划是创建还是删除absent user以哪个用户的身份执行 示例 ansible test -m cron -a namecheck dirs hour5,2 jobls -alh /dev/null ansible test -m cron -a namea job for reboot special_timereboot job/some/job.shuser模块home:指定家目录需要createhome为yes groups:用户组 uid用户UID password:指定用户密码 name:用户名 createhome:是否创建家目录 system:是否创建为系统用户 remove:但stateabsent时删除家目录 state:创建或者删除 shell:指定用户shell环境synchronize模块#使用rsync同步文件其参数如下 archive: 归档相当于同时开启recursive(递归)、links、perms、times、owner、group、-D选项都为yes 默认该项为开启 checksum: 跳过检测sum值默认关闭 compress:是否开启压缩 copy_links复制链接文件默认为no 注意后面还有一个links参数 delete: 删除不存在的文件默认no dest目录路径 dest_port默认目录主机上的端口 默认是22走的ssh协议 dirs传速目录不进行递归默认为no即进行目录递归 rsync_optsrsync参数部分 set_remote_user主要用于/etc/ansible/hosts中定义或默认使用的用户与rsync使用的用户不同的情况 mode: push或pull 模块push模的话一般用于从本机向远程主机上传文件pull 模式用于从远程主机上取文件 使用示例 srcsome/relative/path dest/some/absolute/path rsync_pathsudo rsync srcsome/relative/path dest/some/absolute/path archiveno linksyes srcsome/relative/path dest/some/absolute/path checksumyes timesno src/tmp/helloworld dest/var/www/helloword rsync_opts--no-motd,--exclude.git modepullfilesystem模块在块设备上创建文件系统 选项 dev目标块设备 force在一个已有文件系统 的设备上强制创建 fstype文件系统的类型 opts传递给mkfs命令的选项 示例 ansible test -m filesystem -a fstypeext2 dev/dev/sdb1 forceyes ansible test -m filesystem -a fstypeext4 dev/dev/sdb1 opts-ccmount模块配置挂载点 选项 dump fstype必选项挂载文件的类型 name必选项挂载点 opts传递给mount命令的参数 src必选项要挂载的文件 state必选项 present只处理fstab中的配置 absent删除挂载点 mounted自动创建挂载点并挂载之 umounted卸载 示例 name/mnt/dvd src/dev/sr0 fstypeiso9660 optsro statepresent name/srv/disk srcLABELSOME_LABEL statepresent name/home srcUUIDb3e48f45-f933-4c8e-a700-22a159ec9077 optsnoatime statepresent ansible test -a dd if/dev/zero of/disk.img bs4k count1024 ansible test -a losetup /dev/loop0 /disk.img ansible test -m filesystem fstypeext4 forceyes opts-F dev/dev/loop0 ansible test -m mount name/mnt src/dev/loop0 fstypeext4 statemounted optsrwget_url 模块#该模块主要用于从http、ftp、https服务器上下载文件类似于wget主要有如下选项 sha256sum下载完成后进行sha256 check timeout下载超时时间默认10s url下载的URL url_password、url_username主要用于需要用户名密码进行验证的情况 use_proxy是事使用代理代理需事先在环境变更中定义 示例 get_url: urlhttp://example.com/path/file.conf dest/etc/foo.conf mode0440 get_url: urlhttp://example.com/path/file.conf dest/etc/foo.conf sha256sumb5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944carchive模块用于归档文件 path: /ane/soft下面是多路径的用法 - /ane/soft - /ane/script/* dest: format: bz2、gz、tar、xz、zip removeyes 删除文件默认为no exclude_path 排除特定的目录 - /path/to/foo/bar - /path/to/foo/baz 示例如下 - archive: path/ane/soft/nginx-1.8.1 dest/ane/soft/nginx.tgz formattar #removeyes - archive: path/ane/script dest/ane/scriptunarchive模块用于解压文件模块包含如下选项 copy在解压文件之前是否先将文件复制到远程主机默认为yes。若为no则要求目标主机上压缩包必须存在。 creates指定一个文件名当该文件存在时则解压指令不执行 dest远程主机上的一个路径即文件解压的路径 group解压后的目录或文件的属组 list_files如果为yes则会列出压缩包里的文件默认为no2.0版本新增的选项 mode解决后文件的权限 src如果copy为yes则需要指定压缩文件的源路径 owner解压后文件或目录的属主 示例如下 - unarchive: srcfoo.tgz dest/var/lib/foo - unarchive: src/tmp/foo.zip dest/usr/local/bin copyno - unarchive: srchttps://example.com/example.zip dest/usr/local/bin copynoselinux模块开启selinux selinux: policytargeted stateenforcing 禁用selinux selinux: state:disabled下面是线上机器关闭了selinux