file模块
- 该模块的关键字有 path被控主机端的路径 、state 状态 、 touch创建文件 、 directory 创建目录、 recures 递归 、owner 所属主、 group所属组、 mode 权限。
1.创建文件,并且设定属主,属组,权限
[root@localhost opt]# ansible dbserver -m file -a "path=/var/www/html/xjm.html state=touch owner=root group=root mode=644" -i hosts
2.创建目录,并设定属主,属组,权限
[root@localhost opt]# ansible dbserver -m file -a "path=/var/www/html/dd state=directory owner=root group=root mode=755" -i hosts
3.递归授权目录的方式
[root@localhost opt]# ansible dbserver -m file -a "path=/var/www/html/ owner=apache group=apache recurse=yes" -i hosts
把/var/www/html目录下的所有目录和文件的所属主和所属组都改为apache
yum模块
- 该模块的主要参数有:name:要安装的软件包名,多个软件包以英文逗号隔开, state 对当前指定的软件安装、移除操作 支持的参数有:
present:确认已经安装,但不升级
inistalled:确认已经安装
latest:确保安装,且升级为最新
absent和removed:确认已移除
exclude :排除
1.安装当前最新的apache软件,如果存在就更新
[root@localhost opt]# ansible dbserver -m yum -a "name=httpd state=latest" -i hosts
2.安装当前最新的apache,通过epel仓库安装
[root@localhost opt]# ansible dbserver -m yum -a "name=httpd state=latest enablerepo=epel" -i hosts
3.通过公网url安装rpm软件
[root@localhost opt]# ansible dbserver -m yum -a "name=http://nginx.org/packages/rhel/7/x86_64/RPMS/nginx-1.10.0-1.el7.ngx.x86_64.rpm state=latest" -i hosts
4.更新所有的软件包,但是排除kernel相关的软件包
[root@localhost opt]# ansible dbserver -m yum -a "name=* state=latest exclude=kernel*" -i hosts
要排除多个,用逗号隔开
5.删除apache软件
[root@localhost opt]# ansbile dbserver -m yum -a "name=httpd state=absent" -i hosts
copy模块
- copy模块的参数有:
- src:指定拷贝文件的源地址
dest:指定拷贝文件的目标地址
backup:拷贝文件前,若原始文件发生变化,则对目标文件进行备份
owner:指定新拷贝文件的所有者
group:指定新拷贝文件的所有组
mode:指定新拷贝文件的权限
1.将本地的httpd.conf文件listen端口修改为8080,然后推送到远端服务
[root@localhost opt]# ansible dbserver -m copy -a "src=/opt/xjm/http.conf dest=/opt/xjm/httpd.conf owner=root group=root mode=644" -i hosts
2.将本地的httpd.conf文件listen端口修改为9090。然后推送到远端,检查远端是否存在上一次的备份
[root@localhost opt]# ansible dbserver -m copy