目录html
Ansible是一个简单的自动化引擎,可完成配置管理,应用部署,服务编排以及其余各类IT需求。Ansible也是一款使用Python语言开发实现的开源软件,其依赖Jinja2,Paramiko和PyYAML这几个库linux
Ansbile的优势:web
Ansible做为自动化系统运维的一大利器,在构建整个体系过程当中有着举足轻重的地位。其简单易用,易于安装,功能强大,便于分享,内含大量模版等都是它的魅力所在,再加上易封装,接口调用方便,Ansible正在被愈来愈多的大公司采用。shell
Ansilbe
管理员节点和远程主机节点经过ssh协议进行通讯。apache
Ansible配置的时候只须要与被控端作免密便可。json
# Redhat/CentOS Linux上,Ansible目前放在的epel源中 # Fedora默认源中包含ansible,直接安装包既可 [root@master ~]# yum install epel-release [root@master ~]# yum install ansible -y
配置Linux不一样机器间免密 略...vim
什么是Host Invenory(主机目录,主机清单)?架构
Host Inventory是配置文件,用来告诉Ansible须要管理那些主机。而且把这些主机根据需分类。运维
默认的配置文件是:/etc/ansible/hosts
ssh
最简单的host文件:
192.168.32.130
Ansible 提供了一个命令行工具
ansible命令的格式是:
ansible <host-pattern> [options]
检查ansible安装环境
[root@192.168.32.130 /etc/ansible]$ ansible all -m ping -u root 192.168.32.130 | SUCCESS => { "changed": false, "ping": "pong" }
执行命令
[root@192.168.32.130 /etc/ansible]$ ansible all -a "ls /etc/ansible" 192.168.32.130 | SUCCESS | rc=0 >> ansible.cfg hosts hosts.py roles
拷贝文件
[root@192.168.32.130 /tmp]$ ansible all -m copy -a "src=/etc/ansible/hosts dest=/tmp/" 192.168.32.130 | SUCCESS => { "changed": true, "checksum": "2e304266c75c95987fb111c5482443bb41408cd7", "dest": "/tmp/hosts", "gid": 0, "group": "root", "md5sum": "827317b4e0cd727bf245f2044319d31d", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 15, "src": "/root/.ansible/tmp/ansible-tmp-1572083478.93-155762973748871/source", "state": "file", "uid": 0 }
安装包
[root@192.168.32.130 /tmp]$ ansible all -m shell -a "yum -y install nc" 192.168.32.130 | SUCCESS | rc=0 >> Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Install Process Loading mirror speeds from cached hostfile * base: mirrors.cn99.com * extras: ftp.sjtu.edu.cn * remi-safe: ftp.riken.jp * updates: mirrors.163.com Package nc-1.84-24.el6.x86_64 already installed and latest version Nothing to do
添加用户
[root@192.168.32.130 /tmp]$ ansible all -m shell -a "useradd jack" 192.168.32.130 | SUCCESS | rc=0 >>
并行执行
# 开启10个线程执行 [root@192.168.32.130 /tmp]$ ansible all -a "ip addr" -f 10 192.168.32.130 | SUCCESS | rc=0 >> 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:b8:5c:ad brd ff:ff:ff:ff:ff:ff inet 192.168.32.130/24 brd 192.168.32.255 scope global eth0 inet6 fe80::20c:29ff:feb8:5cad/64 scope link valid_lft forever preferred_lft forever 3: pan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN link/ether 16:86:d7:59:08:da brd ff:ff:ff:ff:ff:ff # 查看远程主机的所有系统信息 [root@192.168.32.130 /tmp]$ ansible all -m setup
只有脚本才能够重用,避免总敲重复的代码
Ansible脚本的名字叫 Playbook,使用YAML的格式,文件以yml结尾
YAML 和 JSON 相似,是一种表示数据的格式
执行脚本的方法:[root@192.168.32.130 /tmp]$ ansible-playbook xxx.yml
yml文件的功能能够写一些部署、启停服务逻辑,例如:安装Apache,步骤以下:
一、 安装Apache包
二、 拷贝配置文件httpd,并保证拷贝文件后,apache服务会被重启
三、 拷贝默认的网页index.html
四、 启动Apache服务
yml文件包含如下几个关键字:
示例:
- hosts: web vars: http_port: 80 max_clients: 200 remote_user: root tasks: - name: ensure apache is at the latest version yum: pkg=httpd state=latest - name: Write the configuration file template: src=templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf notify: - restart apache - name: Write the default index.html file template: src=templates/index.html.j2 dest=/var/www/html/index.html - name: ensure apache is running service: name=httpd state=started handlers: - name: restart apache service: name=httpd state=restarted
不懂yml没有关系,上面的yml格式能够转化为json格式:
[ { "hosts": "web", "vars": { "http_port": 80, "max_clients": 200 }, "remote_user": "root", "tasks": [ { "name": "ensure apache is at the latest version", "yum": "pkg=httpd state=latest" }, { "name": "Write the configuration file", "template": "src=templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf", "notify": [ "restart apache" ] }, { "name": "Write the default index.html file", "template": "src=templates/index.html.j2 dest=/var/www/html/index.html" }, { "name": "ensure apache is running", "service": "name=httpd state=started" } ], "handlers": [ { "name": "restart apache", "service": "name=httpd state=restarted" } ] } ]
playbook 是指一个能够被ansible执行的yml文件
module就是Ansible的“命令”,即执行任务的方式,常见的module有yum、copy、shell
例如使用mudule copy拷贝文件
主机目录的文件,远程机的临时文件存储位置,管理机的临时文件存储文件
[root@192.168.32.130 /etc/ansible]$ vim ansible.cfg
inventory = /etc/ansible/hosts library = /usr/share/my_modules/ remote_tmp = ~/.ansible/tmp local_tmp = ~/.ansible/tmp
链接端口号"accelerate_port",超时时间等。
accelerate_port = 5099 accelerate_timeout = 30 accelerate_connect_timeout = 5.0 accelerate_daemon_timeout = 30 accelerate_multi_key = yes
主机目录管理,告诉ansible须要管理那些server,和server的分类和分组信息
# 默认文件 /etc/ansible/hosts # 修改主机目录的配置文件 ... inventory = /etc/ansible/hosts ... # 命令行中传递主机目录配置文件 $ ansible-playbook -i hosts site.yml 或者参数—inventory-file $ ansible-playbook --inventory-file hosts site.yml
远程主机的分组([]内是组名):
[webservers] foo.example.com [databases] db-[a:f].example.com
指定Server的链接参数,包括链接方法、用户等
执行playbook的语法
ansible-playbook deploy.yml
查看输出的细节
ansblie-playbook playbook.yml --verbose
查看该脚本影响哪些hosts
ansible-playbook playbook.yml --list-hosts
并行执行脚本
ansible-playbook playbook.yml -f 10
最基本的playbook脚本分为三个部分:
在什么机器上以什么身份执行