设为首页 收藏本站
查看: 2230|回复: 0

[经验分享] ansible常用的一些模块

[复制链接]

尚未签到

发表于 2018-1-2 19:30:49 | 显示全部楼层 |阅读模式
  ansible简介绍
  1.ansible是什么?
  l  ansible是一个基于Python开发的自动化运维工具 !
  l  其功能实现基于SSH远程连接服务!
  l  ansible可以实现批量系统配置、批量软件部署、批量文件拷贝、批量运行命令等功能
  2.ansible软件特性
  l  不需要单独安装客户端(no agents),基于系统自带的sshd服务,sshd就相当于ansible的客户端。
  l  不需要服务端(no servers)。
  l  需要依靠大量的模块实现批量管理。?
  l  配置文件/etc/ansible/ansible.cfg,不用配置
  3.ansible软件执行结果
  l  输出内容显示绿色:表示执行成功,当没有任何改变
  l  输出内容显示黄色:表示执行成功,蛋对被管理主机进行了改变
  l  输出内容显示红色:表示执行失败!!!
  4.ansible的官方文档
  http://docs.ansible.com/
  ansible的部署:
  1.因为ansible基于ssh所以需要密钥对可参考http://www.cnblogs.com/ExzaiTin/p/7687410.html
  2.被管理端需要安装libselinux-python
  

yum install libselinux-python -y  

  3.管理端安装ansible软件配置ansible的hosts文件(管理机和被管理机可以基于ssh免密钥,也可以在ansible的hosts文件中写入被管理机的密码)配置格式如下(仅供参考,可以不写密码和用户,[]里表示自己定义的主机组,如果域名可以解析的话ip也可以写为域名)

  4.ansible命令格式
  

ansible <host-pattern> [-m module_name] [-a args] [options]  

  

<host-pattern> 自己定义的主机组  

[-m module_name] 指定模块
[-a args] 指定要执行的动作

  
  ansible常用模块:
  1.ping-测试模块
  

  

ansible test -m ping  

172.16.1.8 | SUCCESS => {"changed": false,"ping": "pong"  
}
  
##以上为可以正常链接字体会显示为绿色
  

172.16.1.41 | UNREACHABLE! => {"changed": false,"msg": "Failed to connect to the host via ssh: ssh: connect to host 172.16.1.41 port 22: Connection timed out\r\n","unreachable": true  
}
  
##以上为无法链接字体会显示为红色
  

  2.setup-远程查看主机的配置信息
  

ansible test -m setup  

  3.command-远程执行命令

  4.shell-远程节点执行模块
  

[iyunv@m01 ~]# ansible test -m shell -a "date"  
172.16.1.222 | SUCCESS | rc=0 >>
  
Sat Oct 21 12:00:38 CST 2017
  

  5.script-在远程执行本地的脚本
  

[iyunv@m01 ~]# ansible test -m script -a "/sh/test.sh"  
172.16.1.222 | SUCCESS => {
  "changed": true,
  "rc": 0,
  "stderr": "Shared connection to 172.16.1.222 closed.\r\n",
  "stdout": "hello word\r\n",
  "stdout_lines": [
  "hello word"
  ]
  
}
  


  6.copy-复制模块

  

##管理主机数据--远程主机  
[iyunv@m01
~]# ansible test -m copy -a "src=/sh/test.sh dest=/server/scripts mode=0755"  
172.16.1.222 | SUCCESS => {
  "changed": true,
  "checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128",
  "dest": "/server/scripts/test.sh",
  "gid": 0,
  "group": "root",
  "md5sum": "e87e71d1c410c5863aadc5b9a3fe209b",
  "mode": "0755",
  "owner": "root",
  "size": 30,
  "src": "/root/.ansible/tmp/ansible-tmp-1508560437.19-268270019190063/source",
  "state": "file",
  "uid": 0
  
}
  
##远程主机数据进行复制
  
[iyunv@m01 ~]# ansible test -m copy -a "remote_src=true src=/server/scripts/test.sh dest=/tmp/"
  
172.16.1.222 | SUCCESS => {
  "changed": true,
  "checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128",
  "dest": "/tmp/test.sh",
  "gid": 0,
  "group": "root",
  "md5sum": "e87e71d1c410c5863aadc5b9a3fe209b",
  "mode": "0644",
  "owner": "root",
  "size": 30,
  "src": "/server/scripts/test.sh",
  "state": "file",
  "uid": 0
  
}
  
##复制到目标后修改名称
  
[iyunv@m01 ~]# ansible test -m copy -a "src=/etc/hosts dest=/tmp/hello.txt"
  
172.16.1.222 | SUCCESS => {
  "changed": true,
  "checksum": "545f587595b5c60d66243fca48e052ed34eed782",
  "dest": "/tmp/hello.txt",
  "gid": 0,
  "group": "root",
  "md5sum": "fe08440ffebed54cab7a9b4cb3c3beda",
  "mode": "0644",
  "owner": "root",
  "size": 371,
  "src": "/root/.ansible/tmp/ansible-tmp-1508560939.57-196389366869591/source",
  "state": "file",
  "uid": 0
  
}
  
##复制时会创建目录,包括多级目录
  
[iyunv@m01 ~]# ansible test -m copy -a "src=/etc/hosts dest=/tmp/a/b/c/"
  
172.16.1.222 | SUCCESS => {
  "changed": true,
  "checksum": "545f587595b5c60d66243fca48e052ed34eed782",
  "dest": "/tmp/a/b/c/hosts",
  "gid": 0,
  "group": "root",
  "md5sum": "fe08440ffebed54cab7a9b4cb3c3beda",
  "mode": "0644",
  "owner": "root",
  "size": 371,
  "src": "/root/.ansible/tmp/ansible-tmp-1508561086.8-245628104704955/source",
  "state": "file",
  "uid": 0
  
}
  

  7.file-设置文件属性

  

创建目录(支持创建多级目录)  
[iyunv@m01
~]# ansible test -m file -a "dest=/tmp/dir state=directory"  
172.16.1.222 | SUCCESS => {
  "changed": true,
  "gid": 0,
  "group": "root",
  "mode": "0755",
  "owner": "root",
  "path": "/tmp/dir",
  "size": 4096,
  "state": "directory",
  "uid": 0
  
}
  
创建文件
  
[iyunv@m01 ~]# ansible test -m file -a "dest=/tmp/hello state=touch"
  
172.16.1.222 | SUCCESS => {
  "changed": true,
  "dest": "/tmp/hello",
  "gid": 0,
  "group": "root",
  "mode": "0644",
  "owner": "root",
  "size": 0,
  "state": "file",
  "uid": 0
  
}
  
创建软连接
  
[iyunv@m01 ~]# ansible test -m file -a "src=/tmp/test.sh dest=/root/test.link state=link "
  
172.16.1.222 | SUCCESS => {
  "changed": true,
  "dest": "/root/test.link",
  "gid": 0,
  "group": "root",
  "mode": "0777",
  "owner": "root",
  "size": 12,
  "src": "/tmp/test.sh",
  "state": "link",
  "uid": 0
  
}
  
删除文件目录
  
[iyunv@m01 ~]# ansible test -m file -a "dest=/root/test.link state=absent"
  
172.16.1.222 | SUCCESS => {
  "changed": true,
  "path": "/root/test.link",
  "state": "absent"
  
}
  

  8.fetch-远程获取

  

##从远程主机拉取文件  
[iyunv@m01
~]# ansible test -m fetch -a "src=/tmp/test.sh dest=/tmp"  
172.16.1.222 | SUCCESS => {
  "changed": true,
  "checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128",
  "dest": "/tmp/172.16.1.222/tmp/test.sh",
  "md5sum": "e87e71d1c410c5863aadc5b9a3fe209b",
  "remote_checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128",
  "remote_md5sum": null
  
}
  
[iyunv@m01 ~]# tree /tmp
  
/tmp
  
└── 172.16.1.222
  └── tmp
  └── test.sh
  

  
2 directories, 1 file
  
##拉取时仅拉取目标文件或目录
  
[iyunv@m01 tmp]# ansible test -m fetch -a "src=/tmp/test.sh dest=/tmp/ flat=yes"
  
172.16.1.222 | SUCCESS => {
  "changed": true,
  "checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128",
  "dest": "/tmp/test.sh",
  "md5sum": "e87e71d1c410c5863aadc5b9a3fe209b",
  "remote_checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128",
  "remote_md5sum": null
  
}
  
[iyunv@m01 tmp]# tree /tmp
  
/tmp
  
└── test.sh
  

  
0 directories, 1 file
  

  9.mount-挂载相关模块

  10.cron-定时任务模块

  11.yum-软件安装模块

  12.service-服务启动或管理模块

  13.filesystem-文件系统模块

  14.user-用户管理

  15.synchronize-rsync相关

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-430912-1-1.html 上篇帖子: Ansible 系列之 Ad-Hoc介绍及使用 下篇帖子: 3.2、Ansible单命令测试
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表