Ansible 学习笔记

Ansible 基础


第一部分:


1,Ansible是先出现的自动化运维工具,Ansible是一个配置管理和应用部署工具,基于python开发,聚合了众多的运维工具(puppet,cfengine,chef,func,fabric SaltStack)的优点,实现批量系统部署,批量运行命令,Ansible是基于模块工作的,本身没有部署能力,真正有部署能力的是Ansible所运行的模块,Ansible只是提供一种框架。
2,应用:
自动化部署应用
自动化管理配置
自动化持续交付(dovops自动化交付)
自动化AWS云服务器管理
3,优点:
1,结合众多的运维工具优点,基本上pubbet和SaltStack基本有的都有。
2,轻量级,无需在客户端上部署agent,只需要在操作及上进行一次更新即可
3,Ansible是是一个工具,不需要启动服务,仅仅是一个工具,可以轻松实现分布式扩展
4,批量任务可以写成脚本,高可靠,安全性设计的轻量级自动化工具与
5,Ansible是一致性,高可靠性,安全性设计的轻量级自动化工具
6,使用python编写,维护简单,ruby语法过于复杂。
4,特点:
1,no agents 不需要再被监控的主机上安装任何的客户端
2,no server 无服务端,使用时直接运行命令即可
3,modules in any languages,基于模块操作,可以用任意的语言
4,yaml,not code 使用yaml语言定制剧本playbook
5,ssh by default 基于ssh的工作
6,strong multi-tier solution:可以实现多级指挥
5,ansible的基本架构
1.连接插件(connectior plugins) 用于连接主机 用来连接被管理端
2.核心模块(core modules) 连接主机实现操作, 它依赖于具体的模块来做具体的事情
3.自定义模块(custom modules) 根据自己的需求编写具体的模块
4.插件(plugins) 完成模块功能的补充
5.playbooks(剧本) ansible的配置文件,将多个任务定义在剧本中,由ansible自动执行
6.host inventory(主机清单)定义ansible需要操作主机的范围
最重要的一点是 ansible是模块化的 它所有的操作都依赖于模块
https://www.processon.com/mindmap/58d6713be4b0359bbccc00aa 架构图


第二部分:
Ansible安装
1,ansible只是一个进程,胥添加数据库,也不需要启动和运行守护进程,他只是一个进程,你可以轻松使用它安装在任何一个主机上,都不能安装到windows上
2,版本选择:选择2.0以上的版本
3,ansible是python写的,一般运行环境是主控python2.6以上,管理节点是2.5以上


安装有3个方式
yum -y install ansible wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo(下载yum源)
pip install ansible
从github下载
$ git clone git://github.com/ansible/ansible.git --recursive
$ cd ./ansible
$ make rpm
$ sudo rpm -Uvh ./rpm-build/ansible-*.noarch.rpm


第三部分:


ansible -h 帮助文档
ansible 127.0.0.1 -a 'data' 显示本机的时间  单条命令
vim /etc/ansible/hosts 编辑ansible的组文件
添加如下
[ceshi]
192.168.43.20
启动命令。查看所有主机的时间
ansible all -a 'date'
如果正常,则:
192.168.43.20 | SUCCESS | rc=0 >>
2018年 05月 20日 星期日 16:46:51 CST


ansible all -l ceshi -m ping  指定cehsi的zu来执行命令


ansible all -m file -a "src=/etc/fstab dest=/tmp/fstab state=link" 


ansible all -m file -a "path=/home/haoxiaoyu state=touch" 远程创建一个文件


ansible all -m file -a "path=/home/haoxiaoyu state=absent" 远程删除一个文件


ansible all -m copy -a "src=/home/hello.txt dest=/home/ owner=root mode=0644" 拷贝文件


ansible all -m copy -a "src=/home/hello.txt dest=/home/ owner=root mode=0644 backup=yes"


ansible all -m service -a "name=nginx state=restarted sleep=10"


nginx 重启,10秒后重启


ansible all -m get_url -a 'url=http://baidu.com/1.txt dest=/tmp/jesse/ mode=0644' 


文件下载模块get_url














作者:知了啊文
链接:https://www.jianshu.com/p/575ced3a08fa
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

猜你喜欢

转载自blog.csdn.net/weixin_36171533/article/details/80601703