文档章节

2.常用模块

eddy_linux
 eddy_linux
发布于 2017/08/02 18:24
字数 1984
阅读 16
收藏 0
常用模块
1.copy模块
    目的:把主控端/root目录下的a.sh文件拷贝到到指定节点上
[root@localhost ~]# ansible web -m copy -a 'src=/root/ansible-master dest=/tmp/'
192.168.222.129 | SUCCESS => {
    "changed": true, 
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
    "dest": "/tmp/ansible-master", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e", 
    "mode": "0644", 
    "owner": "root", 
    "size": 0, 
    "src": "/root/.ansible/tmp/ansible-tmp-1501666699.41-133742932037676/source", 
    "state": "file", 
    "uid": 0
}
[root@localhost ~]# ansible web -m command -a 'ls -l /tmp/ansible-master'
192.168.222.129 | SUCCESS | rc=0 >>
-rw-r--r-- 1 root root 0 Aug  2 17:38 /tmp/ansible-master

2.file模块:
    目的:更改指定节点上/tmp/t.sh的权限为755,属主和属组为root
[root@localhost ~]# ansible web -m file -a 'dest=/tmp/ansible-master mode=755 owner=root group=root'
192.168.222.129 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/ansible-master", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}
[root@localhost ~]# ansible web -m command -a 'ls -l /tmp/ansible-master'
192.168.222.129 | SUCCESS | rc=0 >>
-rwxr-xr-x 1 root root 0 Aug  2 17:38 /tmp/ansible-master
[root@localhost ~]# ansible web -m file -a 'dest=/tmp/ansible-master mode=755 owner=test group=test'
192.168.222.129 | SUCCESS => {
    "changed": true, 
    "gid": 500, 
    "group": "test", 
    "mode": "0755", 
    "owner": "test", 
    "path": "/tmp/ansible-master", 
    "size": 0, 
    "state": "file", 
    "uid": 500
}
[root@localhost ~]# ansible web -m command -a 'ls -l /tmp/ansible-master'
192.168.222.129 | SUCCESS | rc=0 >>
-rwxr-xr-x 1 test test 0 Aug  2 17:38 /tmp/ansible-master

3.cron模块
 目的:在指定节点上定义一个计划任务,每隔3分钟到windows时间服务器更新一次时间
[root@localhost ~]# ansible web -m cron -a 'name="custom job" minute=*/3 hour=* month=* weekday=* job="/usr/sbin/ntpdate time.windows.com"' 
192.168.222.129 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "custom job"
    ]
}
[root@localhost ~]# ansible web -m command -a 'crontab -l'
192.168.222.129 | SUCCESS | rc=0 >>
#Ansible: custom job
*/3 * * * * /usr/sbin/ntpdate time.windows.com

4.group模块
目的:在所有节点上创建一个组名为linux,gid为2048的组
[root@localhost ~]# ansible web -m group -a 'gid=2048 name=linux' 
192.168.222.129 | SUCCESS => {
    "changed": true, 
    "gid": 2048, 
    "name": "linux", 
    "state": "present", 
    "system": false
}


5.user模块
目的:在指定节点上创建一个用户名为linux,组为linux的用户
[root@localhost ~]# ansible web -m user -a 'name=linux group=linux state=present'
192.168.222.129 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 2048, 
    "home": "/home/linux", 
    "name": "linux", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 501
}
[root@localhost ~]# ansible web -m command -a 'id linux'
192.168.222.129 | SUCCESS | rc=0 >>
uid=501(linux) gid=2048(linux) groups=2048(linux)
删除用户
[root@localhost ~]# ansible web -m user -a 'name=linux state=absent remove=yes'
192.168.222.129 | SUCCESS => {
    "changed": true, 
    "force": false, 
    "name": "linux", 
    "remove": true, 
    "state": "absent"
}
[root@localhost ~]# ansible web -m command -a 'id linux'
192.168.222.129 | FAILED | rc=1 >>
id: linux: No such user

6.yum模块
目的:在指定节点上安装 lrzsz 服务
[root@localhost ~]# ansible web -m yum -a 'state=present name=lrzsz'
192.168.222.129 | SUCCESS => {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "lrzsz-0.12.20-27.1.el6.x86_64 providing lrzsz is already installed"
    ]
}

7.service模块
目的:启动指定节点上的 httpd服务,并让其开机自启动
[root@localhost ~]# ansible web -m service -a 'name=httpd state=restarted enabled=yes'
192.168.222.129 | SUCCESS => {
    "changed": true, 
    "enabled": true, 
    "name": "httpd", 
    "state": "started"
}
[root@localhost ~]# ansible web -m command -a '/etc/init.d/httpd status'
192.168.222.129 | SUCCESS | rc=0 >>
httpd (pid  6680) is running...

[root@localhost ~]# ansible web -m raw -a 'chkconfig --list|grep httpd'
192.168.222.129 | SUCCESS | rc=0 >>
httpd          	0:off	1:off	2:on	3:on	4:on	5:on	6:off


8.sctipt模块
 目的:在指定节点上执行/root/a.sh脚本(该脚本是在ansible控制节点上的)
[root@localhost ~]# ansible web -m script -a '/root/a.sh'
192.168.222.129 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.222.129 closed.\r\n", 
    "stdout": "salt\r\n", 
    "stdout_lines": [
        "salt"
    ]
}

9.ping模块
目的:检查指定节点机器是否还能连通
[root@localhost ~]# ansible web -m ping
192.168.222.129 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

10.command模块
目的:在指定节点上运行hostname命令
[root@localhost ~]# ansible web -m command -a 'hostname'
192.168.222.129 | SUCCESS | rc=0 >>
salt
注意不能使用含有|的符号

11.raw模块
目的:在web角色的所有节点上运行hostname命令
[root@localhost ~]# ansible web -m raw -a 'hostname|tee'
192.168.222.129 | SUCCESS | rc=0 >>
salt
Shared connection to 192.168.222.129 closed.
注意可以包含|符号

12.get_url模块
目的:将http://releases.ansible.com/ansible/ansible-latest.tar.gz文件下载到指定节点的/tmp目录下
[root@localhost ~]# ansible web -m get_url -a 'url=http://releases.ansible.com/ansible/ansible-latest.tar.gz dest=/tmp/'
192.168.222.129 | SUCCESS => {
    "changed": true, 
    "checksum_dest": null, 
    "checksum_src": "7c5aa4e8908a0110904f3886000172cbb81c98a4", 
    "dest": "/tmp/ansible-latest.tar.gz", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "b1be8f05864a07c06b8a767dcd48ba1b", 
    "mode": "0644", 
    "msg": "OK (4263357 bytes)", 
    "owner": "root", 
    "size": 4263357, 
    "src": "/tmp/tmpQ5UQe6", 
    "state": "file", 
    "status_code": 200, 
    "uid": 0, 
    "url": "http://releases.ansible.com/ansible/ansible-latest.tar.gz"
}
[root@localhost ~]# ansible web -m command -a 'ls -l /tmp/ansible-latest.tar.gz'
192.168.222.129 | SUCCESS | rc=0 >>
-rw-r--r-- 1 root root 4263357 Aug  2 18:07 /tmp/ansible-latest.tar.gz


13.synchronize模块
目的:将主控方/root/a目录推送到指定节点的/tmp目录下
[root@localhost ~]# ansible web -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes'
192.168.222.129 | SUCCESS => {
    "changed": true, 
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh=/usr/bin/ssh -S none -i /root/.ssh/id_rsa -o Port=22 -o StrictHostKeyChecking=no --out-format=<<CHANGED>>%i %n%L /root/a 192.168.222.129:/tmp/", 
    "msg": "cd+++++++++ a/\n<f+++++++++ a/1.png\n<f+++++++++ a/1.txt\n<f+++++++++ a/10.png\n<f+++++++++ a/10.txt\n<f+++++++++ a/2.png\n<f+++++++++ a/2.txt\n<f+++++++++ a/3.png\n<f+++++++++ a/3.txt\n<f+++++++++ a/4.png\n<f+++++++++ a/4.txt\n<f+++++++++ a/5.png\n<f+++++++++ a/5.txt\n<f+++++++++ a/6.png\n<f+++++++++ a/6.txt\n<f+++++++++ a/7.png\n<f+++++++++ a/7.txt\n<f+++++++++ a/8.png\n<f+++++++++ a/8.txt\n<f+++++++++ a/9.png\n<f+++++++++ a/9.txt\n", 
    "rc": 0, 
    "stdout_lines": [
        "cd+++++++++ a/", 
        "<f+++++++++ a/1.png", 
        "<f+++++++++ a/1.txt", 
        "<f+++++++++ a/10.png", 
        "<f+++++++++ a/10.txt", 
        "<f+++++++++ a/2.png", 
        "<f+++++++++ a/2.txt", 
        "<f+++++++++ a/3.png", 
        "<f+++++++++ a/3.txt", 
        "<f+++++++++ a/4.png", 
        "<f+++++++++ a/4.txt", 
        "<f+++++++++ a/5.png", 
        "<f+++++++++ a/5.txt", 
        "<f+++++++++ a/6.png", 
        "<f+++++++++ a/6.txt", 
        "<f+++++++++ a/7.png", 
        "<f+++++++++ a/7.txt", 
        "<f+++++++++ a/8.png", 
        "<f+++++++++ a/8.txt", 
        "<f+++++++++ a/9.png", 
        "<f+++++++++ a/9.txt"
    ]
}
[root@localhost ~]# ansible web -m command -a 'ls -l /tmp/a'
192.168.222.129 | SUCCESS | rc=0 >>
total 0
-rw-r--r-- 1 root root 0 Aug  2 18:09 10.png
-rw-r--r-- 1 root root 0 Aug  2 18:09 10.txt
-rw-r--r-- 1 root root 0 Aug  2 18:09 1.png
-rw-r--r-- 1 root root 0 Aug  2 18:09 1.txt
-rw-r--r-- 1 root root 0 Aug  2 18:09 2.png
-rw-r--r-- 1 root root 0 Aug  2 18:09 2.txt
-rw-r--r-- 1 root root 0 Aug  2 18:09 3.png
-rw-r--r-- 1 root root 0 Aug  2 18:09 3.txt
-rw-r--r-- 1 root root 0 Aug  2 18:09 4.png
-rw-r--r-- 1 root root 0 Aug  2 18:09 4.txt
-rw-r--r-- 1 root root 0 Aug  2 18:09 5.png
-rw-r--r-- 1 root root 0 Aug  2 18:09 5.txt
-rw-r--r-- 1 root root 0 Aug  2 18:09 6.png
-rw-r--r-- 1 root root 0 Aug  2 18:09 6.txt
-rw-r--r-- 1 root root 0 Aug  2 18:09 7.png
-rw-r--r-- 1 root root 0 Aug  2 18:09 7.txt
-rw-r--r-- 1 root root 0 Aug  2 18:09 8.png
-rw-r--r-- 1 root root 0 Aug  2 18:09 8.txt
-rw-r--r-- 1 root root 0 Aug  2 18:09 9.png
-rw-r--r-- 1 root root 0 Aug  2 18:09 9.txt

compress=yes  开启压缩,默认为开启
delete=yes   使两边的内容一样(即以推送方为主)
mode=push 默认推送过去可以修改为pull模式从客户端拉取过来
[root@localhost ~]# ansible web -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes delete=yes'
192.168.222.129 | SUCCESS => {
    "changed": true, 
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --delete-after --archive --rsh=/usr/bin/ssh -S none -i /root/.ssh/id_rsa -o Port=22 -o StrictHostKeyChecking=no --out-format=<<CHANGED>>%i %n%L /root/a 192.168.222.129:/tmp/", 
    "msg": ".d..t...... a/\n*deleting   a/9.png\n*deleting   a/8.png\n*deleting   a/7.png\n*deleting   a/6.png\n*deleting   a/5.png\n*deleting   a/4.png\n*deleting   a/3.png\n*deleting   a/2.png\n*deleting   a/10.png\n*deleting   a/1.png\n", 
    "rc": 0, 
    "stdout_lines": [
        ".d..t...... a/", 
        "*deleting   a/9.png", 
        "*deleting   a/8.png", 
        "*deleting   a/7.png", 
        "*deleting   a/6.png", 
        "*deleting   a/5.png", 
        "*deleting   a/4.png", 
        "*deleting   a/3.png", 
        "*deleting   a/2.png", 
        "*deleting   a/10.png", 
        "*deleting   a/1.png"
    ]
}
目录下的子目录也是可以同步过去的
[root@localhost ~]# ansible web -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes delete=yes'
192.168.222.129 | SUCCESS => {
    "changed": true, 
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --delete-after --archive --rsh=/usr/bin/ssh -S none -i /root/.ssh/id_rsa -o Port=22 -o StrictHostKeyChecking=no --out-format=<<CHANGED>>%i %n%L /root/a 192.168.222.129:/tmp/", 
    "msg": ".d..t...... a/\ncd+++++++++ a/b/\ncd+++++++++ a/b/c/\ncd+++++++++ a/b/c/d/\n", 
    "rc": 0, 
    "stdout_lines": [
        ".d..t...... a/", 
        "cd+++++++++ a/b/", 
        "cd+++++++++ a/b/c/", 
        "cd+++++++++ a/b/c/d/"
    ]
}
从客户端拉取到主控端
[root@localhost ~]# ansible web -m synchronize -a 'src=/tmp/a dest=/tmp/ compress=yes delete=yes mode=pull'
192.168.222.129 | SUCCESS => {
    "changed": true, 
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --delete-after --archive --rsh=/usr/bin/ssh -S none -i /root/.ssh/id_rsa -o Port=22 -o StrictHostKeyChecking=no --out-format=<<CHANGED>>%i %n%L 192.168.222.129:/tmp/a /tmp/", 
    "msg": "cd+++++++++ a/\n>f+++++++++ a/1.txt\n>f+++++++++ a/10.txt\n>f+++++++++ a/2.txt\n>f+++++++++ a/3.txt\n>f+++++++++ a/4.txt\n>f+++++++++ a/5.txt\n>f+++++++++ a/6.txt\n>f+++++++++ a/7.txt\n>f+++++++++ a/8.txt\n>f+++++++++ a/9.txt\ncd+++++++++ a/b/\ncd+++++++++ a/b/c/\ncd+++++++++ a/b/c/d/\n", 
    "rc": 0, 
    "stdout_lines": [
        "cd+++++++++ a/", 
        ">f+++++++++ a/1.txt", 
        ">f+++++++++ a/10.txt", 
        ">f+++++++++ a/2.txt", 
        ">f+++++++++ a/3.txt", 
        ">f+++++++++ a/4.txt", 
        ">f+++++++++ a/5.txt", 
        ">f+++++++++ a/6.txt", 
        ">f+++++++++ a/7.txt", 
        ">f+++++++++ a/8.txt", 
        ">f+++++++++ a/9.txt", 
        "cd+++++++++ a/b/", 
        "cd+++++++++ a/b/c/", 
        "cd+++++++++ a/b/c/d/"
    ]
}




 

© 著作权归作者所有

上一篇: 3.playbook基础
下一篇: 1.安装
eddy_linux
粉丝 22
博文 135
码字总数 188789
作品 0
成都
程序员
私信 提问
SaltStack实战之远程执行-Modules

SaltStack实战之远程执行-Modules SaltStack实战之远程执行-Modules 1. 官方模块链接 官网可执行模块文档: https://docs.saltstack.com/en/latest/ref/modules/all/ 2. 常用模块 network模块...

ygqygq2
2017/06/09
0
0
Python第三章模块-张明阳-专题视频课程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a2011480169/article/details/83580545 Python第三章模块—142人已学习 课程介绍 介绍Python在项目当中常用的...

安静的技术控
2018/04/20
0
0
Python的基础知识之时间处理-datetime

时间日历的处理三个模块,在使用之前都需要导入这么模块 1、 time模块 2、 calendar 模块 3、 datetime 模块 tmpdir17961048_05.jpg 一、time模块 1.1 获取时间戳 1.2 获取时间元祖 1.3 获取...

摸着石头过河_崖边树
2017/12/18
0
0
服务器后端开发系列——《实战Nginx高性能Web服务器》

1、高性能Web服务器Nginx的配置与部署研究(1)Nginx简介及入门示例 内容:概述Nginx的背景知识和简单的入门实例。 2、高性能Web服务器Nginx的配置与部署研究(2)Nginx入门级配置与部署及“...

晨曦之光
2012/04/24
295
0
SaltStack实战之配置管理-状态间关系

SaltStack实战之配置管理-状态间关系 SaltStack实战之配置管理-状态间关系 1. 状态相关模块 2. 实际应用 1. 状态相关模块 1.1 pkg 状态模块:pkg 功能:管理软件包状态 常用方法: pkg.inst...

ygqygq2
2017/06/16
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Spring Boot 2 实战:使用 Spring Boot Admin 监控你的应用

1. 前言 生产上对 Web 应用 的监控是十分必要的。我们可以近乎实时来对应用的健康、性能等其他指标进行监控来及时应对一些突发情况。避免一些故障的发生。对于 Spring Boot 应用来说我们可以...

码农小胖哥
今天
6
0
ZetCode 教程翻译计划正式启动 | ApacheCN

原文:ZetCode 协议:CC BY-NC-SA 4.0 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远。 ApacheCN 学习资源 贡献指南 本项目需要校对,欢迎大家提交 Pull Request。 ...

ApacheCN_飞龙
今天
4
0
CSS定位

CSS定位 relative相对定位 absolute绝对定位 fixed和sticky及zIndex relative相对定位 position特性:css position属性用于指定一个元素在文档中的定位方式。top、right、bottom、left属性则...

studywin
今天
7
0
从零基础到拿到网易Java实习offer,我做对了哪些事

作为一个非科班小白,我在读研期间基本是自学Java,从一开始几乎零基础,只有一点点数据结构和Java方面的基础,到最终获得网易游戏的Java实习offer,我大概用了半年左右的时间。本文将会讲到...

Java技术江湖
昨天
7
0
程序性能checklist

程序性能checklist

Moks角木
昨天
7
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部