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

[经验分享] ansible模块学习

[复制链接]

尚未签到

发表于 2018-1-2 15:17:04 | 显示全部楼层 |阅读模式
  ansible的功能:
  模块化任务,调用特定的模块,完成特定的任务
  基于python语言实现,由paramiko、pyyaml和jinja2三个模块构建
  部署简单,agentless,ansible基于ssh协议实现的  主从模式  支持自定义模块  支持playbook  允许重复执行ansible的安装,ansible提供的rpm包在epel源上  note:epel源安装  cd /etc/yum.repos.d  wget http://mirrors.neusoft.edu.cn/epel/epel-release-latest-6.noarch.rpm  rpm -ivh epel-release-latest-6.noarch.rpm  yum install ansible -y ansible主端:192.168.223.136节点1:192.168.223.1461、ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''2、ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.223.1433、远程连接ssh root@192.168.223.146或者ssh 192.168.223.1464、在节点上执行命令[iyunv@node1 ~]# ssh 192.168.223.146 'date'Sun Jul 30 20:02:58 CST 2017[iyunv@node1 ~]# dateSun Jul 30 12:03:00 CST 2017 ansible基础用法:ansible <host-pattern> [-m module_name] [-a args] [options]ansible-doc -l 显示ansible支持的模块[iyunv@node1 ~]# ansible-doc -s command 显示模块的具体用法- name: Executes a command on a remote node解决:定义host清单1、cp hosts hosts.bak2、修改hoats文件[nodes]192.168.223.146 command模块:  ansible nodes -m command -a 'ifconfig' ping模块:  [iyunv@node1 ansible]# ansible all -m ping  192.168.223.146 | SUCCESS => {  "changed": false,  "ping": "pong"  } user模块:#ansible-doc -s user  新建某个用户:  [iyunv@node1 ansible]# ansible nodes -m user -a "name=wadeson state=present"  192.168.223.146 | SUCCESS => {  "changed": true,  "comment": "",  "createhome": true,  "group": 500,  "home": "/home/wadeson",  "name": "wadeson",  "shell": "/bin/bash",  "state": "present",  "system": false,  "uid": 500  }-a后面接模块的参数:name=:表示需要创建的新用户的名字state:表示用户存在的状态(由于是新建用户,所以状态为present)删除某个用户: absent不在的[iyunv@node1 ansible]# ansible nodes -m user -a "name=wadeson state=absent"192.168.223.146 | SUCCESS => {"changed": true,"force": false,"name": "wadeson","remove": false,"state": "absent"}如果state不填写,默认是present,创建一个新的 cron模块:ansible-doc -s cron[iyunv@node1 ansible]# ansible all -m cron -a 'name="sync time" minute=2 user=root state=present job="/usr/sbin/ntpdate time.nist.gov &> /dev/null"'192.168.223.146 | FAILED! => {"changed": false,"failed": true,"msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"}解决办法:在节点主机上安装libselinux-pythonyum -y install libselinux-python 再次在ansible服务器上执行:[iyunv@node1 ansible]# ansible all -m cron -a 'name="sync time" minute=*/2 user=root state=present job="/usr/sbin/ntpdate time.nist.gov &> /dev/null"'192.168.223.146 | SUCCESS => {"changed": true,"envs": [],"jobs": ["sync time"]}在节点上查看:[iyunv@wadeson ~]# crontab -l#Ansible: sync time2 * * * * /usr/sbin/ntpdate time.nist.gov &> /dev/null删除cron任务:ansible all -m cron -a 'name="sync time" state=absent‘ copy模块:ansible-doc -s copyansible all -m copy -a "src=/etc/fstab dest=/tmp/fatab.bak mode=600"节点上查看:[iyunv@wadeson ~]# ll /tmp/总用量 8-rw-------. 1 root root 899 7月 30 21:55 fatab.bak
[iyunv@node1 ansible]# cat test_var.yaml

  - hosts: nodes
  remote_user: root
  tasks:
  - name: create a new file
  copy: content={{ node_var }} dest=/tmp/ansible_var.txt
file模块:ansible-doc -s file,创建文件,目录,链接文件ansible all -m file -a 'path=/tmp/testdir state=directory'ansible all -m file -a 'src=/tmp/fstab.bak dest=/root/fstab.bak state=link'[iyunv@wadeson ~]# ll总用量 16-rw-------. 1 root root 1104 7月 30 19:54 anaconda-ks.cfglrwxrwxrwx. 1 root root 14 7月 30 22:03 fstab.bak -> /tmp/fstab.bakstate:file|absent|directory|link|hard|touch在远程节点上创建一个新文件:ansible nodes -m file -a 'path=/tmp/ansible_agent.txt state=touch'path这里可以为dest或者name yum模块:ansible-doc -s yumansible all -m yum -a 'name="vim,wget" state=latest'state:present|latest|absentansible all -m yum -a 'name=ntpdate state=latest'按照ntpdate包,然后定义cron任务 service模块:参数:enabled:设置是否开机启动state:started|stopped|restartedansible all -m yum -a 'name=httpd state=latest'ansible all -m service -a 'name=httpd state=started enabled=no'节点查看:[iyunv@wadeson ~]# rpm -qa httpdhttpd-2.2.15-60.el6.centos.4.x86_64[iyunv@wadeson ~]# chkconfig --list|grep httpdhttpd 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭 shell模块:-a “command”,没有额外的参数命令中带有管道,变量等等[iyunv@node1 ~]# ansible nodes -m shell -a 'echo "redhat"|passwd --stdin testuser1'192.168.223.146 | SUCCESS | rc=0 >>更改用户 testuser1 的密码 。passwd: 所有的身份验证令牌已经成功更新。而command模块:[iyunv@node1 ~]# ansible nodes -m command -a 'echo "redhat"|passwd --stdin testuser1'192.168.223.146 | SUCCESS | rc=0 >>redhat|passwd --stdin testuser1可以看出并没有修改用户的密码,command模块只能执行简单的命令 script模块:1、将脚本传到node上2、并在node上执行该脚本ansible服务器:[iyunv@node1 ~]# cat echo.sh#!/bin/bashecho "ansible is better" > /tmp/echo.textansible all -m script -a "/root/echo.sh"节点查看:[iyunv@wadeson ~]# ll /tmp/总用量 16-rw-r--r--. 1 root root 18 7月 30 14:46 echo.text setup模块:查看facts收集node上的系统信息,可以当作变量进行调用ansible all -m setup   

运维网声明 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-430841-1-1.html 上篇帖子: python学习之ansible api 下篇帖子: ansible Developing Plugins
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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