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

简介: 1.copy模块语法格式

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"
目录
相关文章
|
6月前
|
运维 Shell Linux
Ansible自动化运维工具之常用模块使用实战(5)
Ansible自动化运维工具之常用模块使用实战(5)
|
8月前
|
网络协议 网络安全
Ansible模块介绍——防火墙模块
Ansible模块介绍——防火墙模块
144 0
|
3天前
|
算法 安全 Linux
Ansible自动化工具copy复制用法
Ansible 中的 copy 模块用于将文件或目录从本地计算机或远程主机复制到远程主机上的特定位置。它是一个功能强大的模块,可用于各种文件传输任务. ### 作用 将配置文件复制到远程服务器 将应用程序部署到远程服务器 将日志文件从远程服务器复制到本地计算机 备份和恢复文件和目录
Ansible自动化工具copy复制用法
|
6月前
|
运维 Linux
Ansible自动化运维工具之常用模块使用实战(6)
Ansible自动化运维工具之常用模块使用实战(6)
|
9月前
|
Shell
ansible模块大全上【建议收藏】
ansible模块大全上【建议收藏】
104 0
ansible模块大全上【建议收藏】
|
5月前
|
网络安全 数据安全/隐私保护
ansible的get_url模块
ansible的get_url模块
|
5月前
|
存储 Linux Python
ansible手动添加模块
ansible手动添加模块
47 0
|
8月前
Ansible模块管理——磁盘管理模块、mount模块
Ansible模块管理——磁盘管理模块、mount模块
250 0
|
8月前
|
网络协议 网络安全 数据安全/隐私保护
Ansible模块介绍——配置网络模块、上传下载文件模块
Ansible模块介绍——配置网络模块、上传下载文件模块
272 0
|
8月前
|
应用服务中间件 nginx
Ansible模块——软件包管理模块
Ansible模块——软件包管理模块