Ansible的功能过于强大,在以往的运维中,基本都是通过shell脚本来实现 ,但是ansible实现起来很容易,几个脚本配合实现的功能就需要一条ansible命令。从今天开始准备多做几期专题来实践,这次测试了八个较为实用的模块,里面涉及了一些知识点,也解决了自己的误区。
一、ansible 命令执行过程
1、加载 ansible 配置文件,默认/etc/ansible/ansible.cfg;
2、查找对应的主机配置文件,找到要执行的主机或者组,默认inventory = /etc/ansible/hosts;
3、加载对应的模块文件,如 command、fetch等
4、通过ansible将模块或命令生成对应的临时python脚本, 并将该文件传输至远程服务器的对应执行用户home目录的.ansible/tmp/XXX/XXX.PY文件;
5、给文件 +x 执行权限,执行并返回结果;
6、删除临时python脚本,sleep 0退出;
二、command 模块
ansible-doc -s command :获取command模块的使用帮助。

需要注意的是,command模块不会通过shell进行处理。比如下面的命令切换目录需要用到chdir命令才能成功。

ansible tcloud -m command -a "ps -f" ,有命令在调用/home/lighthouse/.ansible/tmp/目录下的python执行,进程里面有两条相关的命令,这是因为是/bin/sh -c将后面的字符串作为整个命令来执行,真实的命令执行时会在有一个进程。

笔者对于生成的临时python脚本感兴趣,想去看一看,无奈运行完成后就删除了。本想利用的画面的ps -f 命令把python脚本的路径提出来,然后打印出来,想一想觉得复杂了,于是换成cp命令:ansible tcloud -m command -a "cp -r /home/lighthouse/.ansible/tmp/ /home/lighthouse/" ,那么在目标服务器tcloud上就有cp下面的临时python脚本。

三、shell模块
shell模块可以在远程主机上调用shell解释器运行命令,支持shell的各种功能。

从下面测试可以查看,command模块不支持的;,在shell命令是支持的。

ansible tcloud -m shell -a "ps -ef | grep mysql" ,可以看到在云服务器tcloud上,有两条进程在使用sh执行ps -ef | grep mysql 。

四、copy模块
Ansible运维自动化工具(1)中讲解了fetch模块,从远程某主机获取(复制)文件到本地。
copy模块就是相反,用于将文件复制到远程主机,同时支持给定内容生成文件和修改权限等。
使用ansible-doc -s copy查看模块的使用信息,鉴于条目过多,笔者摘抄几条。
本地目录src:Local path to a file to copy to the remote server. This can be absolute or relative. If path is a directory, it iscopied recursively.In this case, if path ends with "/", only inside contents of that directory are copied to destination. Otherwise, if it does not end with "/", the directory itself with all contents is copied. This behavior is similar to the `rsync' command line tool.
文件内容content:When used instead of `src', sets the contents of a file directly to the specified value. Works only when `dest' is a file. Creates the file if it does not exist. For advanced formatting or if `content' 。
目的地址dest:(required) Remote absolute path where the file should be copied to. If `src' is a directory, this must be a directory too. If `dest' is a non-existent path and if either `dest' ends with "/" or `src' is a directory, `dest' is created. If `dest' is a relative path, the starting directory is determined by the remote host. If `src' and `dest' are files, the parentdirectory of `dest' is not created and the task fails if it does not already exist.
原文件备份backup:Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly.
1、下面有三条利用copy模块的命令。第一条时直接复制文件,第二条时复制文件夹下面的文件,不包含文件夹,第三条时直接复制文件夹。

2、第一条是给定内容生成文件,权限是默认的;第二条是给定内容生成文件并制定权限。

3、带有backup选项,在复制时会保留备份同名的原文件,原文件会重命名为带有时间戳的文件名。

五、file模块
该模块主要用于设置文件的属性,比如创建文件、创建链接文件、删除文件等。
ansible-doc -s file 查看模块的使用帮助信息。
创建目录:ansible tcloud -m file -a 'path=/home/lighthouse/myfile state=directory'
删除目录:ansible tcloud -m file -a 'path=/home/lighthouse/myfile state=absent'

创建文件并设置权限:ansible tcloud -m file -a 'path=/home/lighthouse/myfile state=touch mode=666'
删除文件:ansible tcloud -m file -a 'path=/home/lighthouse/myfile state=absent'

六、cron 模块
该模块用于管理cron计划任务,使用的语法跟crontab文件中的语法一致。
ansible-doc -s cron查看cron模块的使用方法。

新建任务计划:ansible tcloud -m cron -a 'name="ECHO every 5 min" minute=*/5 job="/usr/bin/echo $(date) >>/home/lighthouse/cron.log"'
删除任务计划: ansible tcloud -m cron -a 'name="ECHO every 5 min" minute=*/5 job="/usr/bin/echo $(date) >>/home/lighthouse/cron.log" state=absent'

七、yum 模块
该模块主要用于软件的安装。
ansible-doc -s yum 查看yum模块的使用帮助信息。
name表示安装的软件,state=的present安装, latest安装最新的, absent卸载软件。
ansible web -m yum -a 'name=*** state=present'
ansible web -m yum -a 'name=*** state=absent'
八、service 模块
该模块用于服务程序的管理。
ansible-doc -s service查看service模块的使用帮助信息。
state 有四种状态,分别为:started启动服务, stopped停止服务, restarted重启服务, reloaded重载配置。enabled 设置开机启动。
现在我们来关闭mairiadb数据库,发现连接超时,估计是权限问题。
ansible tcloud -m service -a 'name=mariadb state=stopped'

在hosts文件里面添加ansible_become_pass,注意2.9版本变量做了变化,不是ansible_sudo_pass 。使用vim好像会检查格式,笔者打开后就在sudo上有阴影显示,不知是否?
再次使用sudo权限来操作成功。
ansible tcloud -m service -a 'name=mariadb state=stopped' -become=true

关闭后,可以使用ansible tcloud -m shell -a 'systemctl status mariadb' 来查看你服务状态。
九、setup模块
该模块主要用于收集信息,是通过调用facts组件来实现的。
ansible-doc -s setup查看setup模块的使用帮助信息。
查看内存:ansible web -m setup -a 'filter="*mem*"'

保存筛选的信息至本主机上。
ansible tcloud -m setup -a 'filter="*mem*"' --tree /tmp/myfree

网友评论