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

[经验分享] 自动化运维工具ansible基础应用

[复制链接]

尚未签到

发表于 2018-7-30 07:00:21 | 显示全部楼层 |阅读模式
  ansible是一款自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置,批量程序部署,批量运行命令等功能。
  ansible是基于模块工作的,本事没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
  1、连接插件connection plugins:负责和被监控端进行通信
  2、host inventory:指定操作的主机,是一个配置文件里卖弄定义监控的主机
  3、各种核心模块、command模块、自定义模块
  4、借助于插件完成记录日志邮件等功能
  5、playbook:可让slave节点一次执行多个任务
  ansible的特性:
  1、模块化:调用特定的模块,完成特定任务
  2、基于python语言实现,有Paramiko,PyYAML和jinja2三个关键模块
  3、部署简单
  4、支持自定义模块
  5、支持playbook
  准备环境
主机名             IP          localhost(ansiblemaster)          10.10.86.56           node1(ansibleslave)          10.10.73.148           bogon(ansibleslave)          10.10.73.149  一、ansible的安装
[root@localhost ~]# yum install ansible  
##配置文件:/etc/ansible/ansible.cfg
  
##主机清单:/etc/ansible/hosts
  
##主程序:ansible、ansible-doc、ansible-playbook
  二、ansible免秘钥ssh登陆
root@localhost ~]# ssh-keygen -t rsa -P ''  
[root@localhost ~]# ssh-copy-id -i root@10.10.73.148
  
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  

  
/usr/bin/ssh-copy-id: WARNING: All keys were skipped because they already exist on the remote system.
  

  
[root@localhost ~]# ssh-copy-id -i root@10.10.73.149
  
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  

  
/usr/bin/ssh-copy-id: WARNING: All keys were skipped because they already exist on the remote system.
  
##将生产的秘钥推送给slave节点,第一次交互需要(yes/no)
  三、主机组自定义
[root@localhost ~]# vim /etc/ansible/hosts  
[ansible_agent]
  

  
10.10.73.148  ##port
  

  
10.10.73.149  ##port
  
##如果slave的ssh端口不是默认的22,则可在主机后面相应的端口
  四、简单测试
  获取ansible的常用模块列表
[root@localhost ~]# ansible-doc -l  获取某个模块的用法
[root@localhost ~]# ansible-doc -s COMMAND  
##COMMAND:yum、cron、shell、setup、copy......
  1、ping
[root@localhost ~]# ansible ansible_agent -m ping    ##用来测试远程主机的运行状态  
10.10.73.149 | SUCCESS => {
  
    "changed": false,
  
    "ping": "pong"
  
}
  
10.10.73.148 | SUCCESS => {
  
    "changed": false,
  
    "ping": "pong"
  
}
  2、setup模块
[root@localhost ~]# ansible ansible_agent -m "setup"   ##获取远程主机的详情信息  3、command模块
[root@localhost ~]# ansible ansible_agent -m shell -a "ls /tmp"  
10.10.73.149 | SUCCESS | rc=0 >>
  
ansible_Z1xG26
  
wtc
  

  
10.10.73.148 | SUCCESS | rc=0 >>
  
ansible_6SQ3_D
  
report.sh
  
wtc.txt
  4、shell模块
[root@localhost ~]# ansible ansible_agent -m shell -a "echo wxpp | passwd --stdin wtc"  
10.10.73.148 | SUCCESS | rc=0 >>
  
Changing password for user wtc.
  
passwd: all authentication tokens updated successfully.
  

  
10.10.73.149 | SUCCESS | rc=0 >>
  
更改用户 wtc 的密码 。
  
passwd: 所有的身份验证令牌已经成功更新。
  

  
###ansible是要支持管道命令,必须要使用shell模块;同时想要支持shell特性,必须要使用shell模块
  5、copy模块
  (1)src=  dest=
  (2)content=  dest=
  (3)owner:指明属主
  (4)group:指明数组
  (5)mode:指明权限
[root@localhost tmp]# ansible ansible_agent -m copy -a "src=/tmp/wxpp.txt  dest=/tmp mode=665"  ##复制本地的"wxpp.txt"文件至远程主机上  
10.10.73.148 | SUCCESS => {
  
    "changed": true,
  
    "checksum": "7641dc777dc18a1c2dfa3429aa8009c12c566913",
  
    "dest": "/tmp/wxpp.txt",
  
    "gid": 0,
  
    "group": "root",
  
    "mode": "0665",
  
    "owner": "root",
  
    "path": "/tmp/wxpp.txt",
  
    "size": 36,
  
    "state": "file",
  
    "uid": 0
  
}
  
10.10.73.149 | SUCCESS => {
  
    "changed": true,
  
    "checksum": "7641dc777dc18a1c2dfa3429aa8009c12c566913",
  
    "dest": "/tmp/wxpp.txt",
  
    "gid": 0,
  
    "group": "root",
  
    "mode": "0665",
  
    "owner": "root",
  
    "path": "/tmp/wxpp.txt",
  
    "size": 36,
  
    "state": "file",
  
    "uid": 0
  
}
  
[root@localhost tmp]# ansible ansible_agent -m shell -a "cat /tmp/wxpp.txt"   ##追加指定的文本至远程主机中
  
10.10.73.149 | SUCCESS | rc=0 >>
  
wtc sent a bouquet of roses to wxpp
  

  
10.10.73.148 | SUCCESS | rc=0 >>
  
wtc sent a bouquet of roses to wxpp
  
[root@localhost tmp]# ansible ansible_agent -m copy -a "content='\nHello World' dest=/tmp/wxpp.txt"
  
10.10.73.149 | SUCCESS => {
  
    "changed": true, }
  
10.10.73.148 | SUCCESS => {
  
    "changed": true, }
  
[root@localhost tmp]# ansible ansible_agent -m shell -a "cat /tmp/wxpp.txt"
  
10.10.73.149 | SUCCESS | rc=0 >>
  

  
Hello World
  

  
10.10.73.148 | SUCCESS | rc=0 >>
  

  
Hello World
  
##content是覆盖文件源内容,使用时注意
  6、cron模块
  (1)month=
  (2)day=
  (3)hour=
  (4)weekday=
  (5)minute=
  (6)job=   ##指明运行的命令是什么
  (7)name=  ##指明定时任务描述
  (8)state  ##指定状态,prsent表示添加定时任务,也是默认值。absent表示删除定时任务
  (9)user   ##指明以那个用户的身份执行
[root@localhost ~]# ansible ansible_agent -m shell -a "ls /var/spool/cron/"  
10.10.73.149 | SUCCESS | rc=0 >>
  
root
  

  
10.10.73.148 | SUCCESS | rc=0 >>
  
root
  
[root@localhost ~]# ansible ansible_agent -m cron -a "minute='*/1' job='/usr/sbin/ntpdate 10.10.86.56 &> /dev/null' user=wtc name='Wtc Job'"
  
10.10.73.149 | SUCCESS => {
  
    "changed": true,
  
    "envs": [],
  
    "jobs": [
  
        "Wtc Job"
  
    ]
  
}
  
10.10.73.148 | SUCCESS => {
  
    "changed": true,
  
    "envs": [],
  
    "jobs": [
  
        "Wtc Job"
  
    ]
  
}
  
[root@localhost ~]# ansible ansible_agent -m shell -a "cat /var/spool/cron/wtc"
  
10.10.73.149 | SUCCESS | rc=0 >>
  
#Ansible: Wtc Job
  
*/1 * * * * /usr/sbin/ntpdate 10.10.86.56 &> /dev/null
  

  
10.10.73.148 | SUCCESS | rc=0 >>
  
#Ansible: Wtc Job
  
*/1 * * * * /usr/sbin/ntpdate 10.10.86.56 &> /dev/null
  7、file模块:
  (1)创建链接文件:src=、path=、state=
  (2)修改文件属性:path=、owner=、mode=、group=
  (3)创建目录:path=、state=
  (4)state
  directory:如果目录不存在,则会创建
  link:创建软链接
  hard:创建硬链接
  touch:如果文件不存在,则会创建。如果文件存在,则修改mtime
  absent:删除目录、文件、取消链接
[root@localhost ~]# ansible ansible_agent -m file -a "src=/etc/fstab path=/tmp/fstab.link state=link"  
10.10.73.148 | SUCCESS => {
  
    "changed": true,
  
    "dest": "/tmp/fstab.link",
  
    "gid": 0,
  
    "group": "root",
  
    "mode": "0777",
  
    "owner": "root",
  
    "size": 10,
  
    "src": "/etc/fstab",
  
    "state": "link",
  
    "uid": 0
  
}
  
10.10.73.149 | SUCCESS => {
  
    "changed": true,
  
    "dest": "/tmp/fstab.link",
  
    "gid": 0,
  
    "group": "root",
  
    "mode": "0777",
  
    "owner": "root",
  
    "size": 10,
  
    "src": "/etc/fstab",
  
    "state": "link",
  
    "uid": 0
  
}
  
[root@localhost ~]# ansible ansible_agent -m shell -a "ls /tmp"
  
10.10.73.149 | SUCCESS | rc=0 >>
  
ansible_TdmFaK
  
fstab.link
  
wtc
  
wxpp.txt
  

  
10.10.73.148 | SUCCESS | rc=0 >>
  
ansible_1oUVuk
  
fstab.link
  
report.sh
  
wtc.txt
  
wxpp.txt
  
[root@localhost ~]# ansible ansible_agent -m file -a "path=/tmp/fstab.link state=absent"
  
10.10.73.149 | SUCCESS => {
  
    "changed": true,
  
    "path": "/tmp/fstab.link",
  
    "state": "absent"
  
}
  
10.10.73.148 | SUCCESS => {
  
    "changed": true,
  
    "path": "/tmp/fstab.link",
  
    "state": "absent"
  
}
  
[root@localhost ~]# ansible ansible_agent -m shell -a "ls /tmp"
  
10.10.73.148 | SUCCESS | rc=0 >>
  
ansible_jTgQOI
  
report.sh
  
wtc.txt
  
wxpp.txt
  

  
10.10.73.149 | SUCCESS | rc=0 >>
  
ansible_CuFRGA
  
wtc
  
wxpp.txt
  8、更多模块(ansible-doc -l)
  fetch模块:拉取远程主机的文件到本地(拉取的只能是文件,但是拉取到本地之后是目录,以"hosts"文件中名字命名
  hostname模块:管理主机名
  yum模块:给远程主机安装应用、卸载程序
  service模块:管理远程主机上的服务
  user模块:管理用户的账号、密码
  group模块:管理系统用户组
  总结:
  1、ansible是基于ssh无密钥登陆的,只限于当前用户。如果是"wtc"用户创建ansible秘钥并推送至远程主机,则使用"root"用户推送命令则需使用密码
  2、对于"shell"模块,个人认为它可以实现多数模块的功能,只要能用命令表达式表达出来即可,例如:
ansible ansible_agent -m shell -a "yum install httpd"  
ansible ansible_agent -m shell -a "service httpd start"
  
ansible ansible_agent -m shell -a "echo 'wtc sent a bouquet of roses to wxpp' &>> /tmp/wxpp.txt"
  
ansible ansible_agent -m shell -a "ln -sv /etc/fstab /tmp/fstab.link"

运维网声明 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-543229-1-1.html 上篇帖子: ansible简述 下篇帖子: shell脚本之安装ansible(centos7环境)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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