虚拟化容器,大数据,DBA,中间件,监控。

ansible模块之copy模块和service模块(七)

11 11月
作者:admin|分类:系统运维

1.copy模块

语法格式

[root@ansible ~]# ansible web_clust -m copy -a "src=源文件 dest=目标文件"
参数
src			//推送数据的源文件信息
dest		//推送数据的目标路径
backup		//对目标端已有的文件进行备份再传输
content		//直接批量在被管理端文件中添加内容,如果原来有内容直接覆盖
group		//将本地文件推送到远端,指定文件属组信息
owner		//将本地文件推送到远端,指定文件属主信息
mode		//将本地文件推送到远端,指定文件权限信息

案例1:将本地/etc/hosts文件推送到远端按时间信息备份

ansible端
[root@ansible ~]# ansible web_clust -m copy -a "src=/etc/hosts dest=/etc/ backup=yes"
192.168.81.180 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    }, 
    "backup_file": "/etc/hosts.21644.2020-06-10@22:27:31~", 
    "changed": true, 
    "checksum": "ef1e1a00259d1710511577d93041afabd43dc651", 
    "dest": "/etc/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "80dcb22cf7b283e617248324201e390e", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "system_u:object_r:net_conf_t:s0", 
    "size": 241, 
    "src": "/root/.ansible/tmp/ansible-tmp-1591799249.82-11243-223340081540480/source", 
    "state": "file", 
    "uid": 0
}

远程端:
[root@web ~]# ls /etc/*hosts*
/etc/hosts  /etc/hosts.74203.2020-06-03@23:16:11~ 

案例2:添加rsync认证文件和rsync客户端密码文件

添加rsync认证文件

对rsync服务器写入一个文件并设置权限和属组
[root@ansible ~]# ansible  backup -m copy -a "content='rsync_backup:123456' dest=/etc/rsync_passwdfile owner=root group=root mode=600"

测试文件属性和内容
[root@ansible ~]# ansible backup -m shell -a "ls -l /etc/rsync_passwdfile && cat /etc/rsync_passwdfile"

添加客户端密码文件

[root@ansible ~]# ansible web_clust -m copy -a "content=123456 dest=/etc/rsync_pass owner=root group=root mode=600"
192.168.81.240 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "7c4a8d09ca3762af61e59520943dc26494f8941b", 
    "dest": "/etc/rsync_pass", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "e10adc3949ba59abbe56e057f20f883e", 
    "mode": "0600", 
    "owner": "root", 
    "size": 6, 
    "src": "/root/.ansible/tmp/ansible-tmp-1591199068.62-53706-208979644933057/source", 
    "state": "file", 
    "uid": 0
}

[root@ansible ~]# ansible web_clust -m shell -a "ls -l /etc/rsync_pass && cat /etc/rsync_pass"
192.168.81.240 | CHANGED | rc=0 >>
-rw------- 1 root root 6 6月   3 23:44 /etc/rsync_pass
123456


2.service模块

语法格式

ansible 主机组	-m service -a "name=服务 state=状态"
参数
name		//服务的名称
state		//服务的状态
	started			启动
	restarted		重启
	stoped			停止
	reloaded		重载
enabled		//是否开机自启	如果=no则表示disable

案例1:开启web主机组的http服务,并设置端口号,创建首页

1.更改端口
[root@ansible ~]# ansible web -m shell -a "sed -ri '/^Listen/c Listen 80' /etc/httpd/conf/httpd.conf"

2.开启服务
[root@ansible ~]# ansible web -m service -a "name=httpd state=started enabled=yes"

3.开启服务
[root@ansible ~]# ansible web -m copy -a "content='this is a test' dest=/var/www/html/index.html"


浏览413 评论0
返回
目录
返回
首页
ansible常用模块之group模块和user模块(八) ansible模块之yum模块详解(六)