Ansible基于Python开发,运维工程师对其二次开发相对比较容易;python
Ansible丰富的内置模块,几乎能够知足一切要求;mysql
管理模式很是简单,一条命令能够影响上千台主机;程序员
无客户端模式,底层经过SSH通讯。web
使用者:如何使用Ansible实现自动化运维?面试
Ansible工具集:Ansible能够实现的功能?sql
做用对象:Ansible能够影响哪些主机?shell
CMDB:CMDB存储和管理者企业IT架构中的各项配置信息,是构建ITIL项目的核心工具,运维人员能够组合CMDB和Ansible,经过CMDB直接下发指令调用Ansible工具集完成操做者所但愿达到的目标;数据库
PUBLIC/PRIVATE方式:Ansible除了丰富的内置模块外,同时还提供丰富的API语言接口,如PHP、Python、PERL等多种流行语言,基于PUBLIC/PRIVATE,Ansible以API调用的方式运行;编程
Ad-Hoc命令集:Users直接经过Ad-Hoc命令集调用Ansible工具集来完成任务;json
Playbooks:Users预先编写好Ansible Playbooks,经过执行Playbooks中预先编排好的任务集,按序执行任务。
Ansible Playbooks:任务脚本,编排定义Ansible任务集的配置文件,由Ansible按序依次执行,一般是JSON格式的YML文件;
Inventory:Ansible管理主机清单;
Modules:Ansible执行命令功能模块,多数为内置的核心模块,也可自定义;
Plugins:模块功能的补充,如链接类型插件、循环插件、变量插件、过滤插件等,该功能不太经常使用;
API:供第三方程序调用的应用程序编程接口;
Ansible:该部分图中表现得不太明显,组合Inventory、API、Modules、Plugins能够理解为是Ansible命令工具,其为核心执行工具。
[root@centos01 ~]# cd /mnt/ansiblerepo/ansiblerepo/repodata/
[root@centos01 ansiblerepo]# vim /etc/yum.repos.d/local.repo
[local]
name=centos
baseurl=file:///mnt/ansiblerepo/ansiblerepo <!--修改yum路径-->
enabled=1
gpgcheck=0
[root@centos01 ~]# yum -y install ansible
<!--安装Ansible自动化运维工具-->
[root@centos01 ~]# ansible --version
<!--若是命令能够正常执行,则表示Ansible工具安装成功-->
ansible 2.3.1.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
python version = 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
[root@centos01 ~]# ssh-keygen -t rsa <!--生成密钥对-->
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):<!--密钥对存放路径-->
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
<!--输入私钥保护密码,直接按Enter键表示无密码-->
Enter same passphrase again: <!--再次输入-->
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:cJz6NRTrvMDxX+Jpce6LRnWI3vVEl/zvARL7D10q9WY root@centos01
The key's randomart image is:
+---[RSA 2048]----+
| . . .|
| . . + oo|
| . = o o. oo|
| = * o..+ *|
| . S *.=+=*+|
| . o =+XooE|
| . ..=.++.|
| ..o ..|
| .. o. |
+----[SHA256]-----+
[root@centos01 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.100.20 <!--复制公钥到远端192.168.100.20-->
[root@centos01 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.100.30 <!--复制公钥到远端192.168.100.30-->
[root@centos01 ~]# ansible -i /etc/ansible/hosts web -m ping
[root@centos01 ~]# ansible web -m ping
[root@centos01 ~]# vim /etc/ansible/hosts
............ <!--此处省略部份内容-->
[web]
192.168.100.20
192.168.100.30
[test]
www.benet.com:222 <!--经过222端口管理设备-->
[mail]
yj1.kgc.cn
yj[2:5].kgc.cn
<!--[2:5]表示2~5之间的全部数字,即表示yj2.kgc.cn、yj3.kgc.cn……的全部主机-->
[root@centos01 ~]# ansible web -m command -a "systemctl status httpd" --limit "192.168.100.20"
192.168.100.20 | SUCCESS | rc=0 >>
<!--看到SUCCESS就知道成功了,因此如下内容-->
<!--若是测试httpd服务,被测试主机必然已经安装并启动了httpd服务-->
[root@centos01 ~]# ansible 192.168.100.20 -m command -a "systemctl status httpd"
192.168.100.20 | SUCCESS | rc=0 >>
[root@centos01 ~]# ansible 192.168.1.* -m command -a "systemctl status httpd"
192.168.100.20 | SUCCESS | rc=0 >>
....... <!--此处省略部份内容-->
192.168.100.30 | SUCCESS | rc=0 >>
....... <!--此处省略部份内容-->
<!--实验环境,效果同样,这里就很少说了-->
[root@centos01 ~]# ansible <!--连续按Tab键-->
ansible ansible-console-2 ansible-galaxy ansible-playbook-2.7 ansible-vault-2
ansible-2 ansible-console-2.7 ansible-galaxy-2 ansible-pull ansible-vault-2.7
ansible-2.7 ansible-doc ansible-galaxy-2.7 ansible-pull-2
ansible-connection ansible-doc-2 ansible-playbook ansible-pull-2.7
ansible-console ansible-doc-2.7 ansible-playbook-2 ansible-vault
非固化需求;
临时一次性操做;
二次开发接口调用。
Ansible <host-pattern> [options]
-v(--verbose):输出详细的执行过程信息,能够获得执行过程全部信息;
-i PATH(--inventory=PATH):指定inventory信息,默认为/etc/ansible/hosts;
-f NUM(--forks=NUM):并发线程数,默认为5个线程;
--private-key=PRIVATE_KEY_FILE:指定密钥文件;
-m NAME,--module-name=NAME:指定执行使用的模块;
-M DIRECTORY(--module-path=DIRECTORY) :指定模块存放路径,默认为/usr/share/ansible;
-a ARGUMENTS(--args=ARGUMENTS):指定模块参数;
-u USERNAME(--user=USERNAME):指定远程主机以USERNAME运行命令;
-l subset(--limit=SUBSET):限制运行主机。
[root@centos01 ~]# ansible all -f 5 -m ping
<!--调用ping模块,all表示/etc/ansible/hosts文件中的全部主机,不用建立all分组(默认存在)-->
192.168.100.20 | SUCCESS => { <!--表示执行成功-->
"changed": false, <!--没有对主机作出更改-->
"ping": "pong" <!--表示执行ping命令的返回结果-->
}
192.168.100.30 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@centos01 ~]# ansible web --list <!-- --list:表示列出主机列表信息-->
hosts (2):
192.168.100.20
192.168.100.30
[root@centos01 ~]# ansible web -m command -a "df -hT"
192.168.100.30 | SUCCESS | rc=0 >>
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/cl-root xfs 17G 4.4G 13G 26% /
devtmpfs devtmpfs 897M 0 897M 0% /dev
tmpfs tmpfs 912M 84K 912M 1% /dev/shm
tmpfs tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 173M 842M 18% /boot
tmpfs tmpfs 183M 16K 183M 1% /run/user/42
tmpfs tmpfs 183M 0 183M 0% /run/user/0
192.168.100.20 | SUCCESS | rc=0 >>
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/cl-root xfs 17G 4.3G 13G 26% /
devtmpfs devtmpfs 897M 0 897M 0% /dev
tmpfs tmpfs 912M 84K 912M 1% /dev/shm
tmpfs tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 173M 842M 18% /boot
tmpfs tmpfs 183M 16K 183M 1% /run/user/42
tmpfs tmpfs 183M 0 183M 0% /run/user/0
/dev/sr0 iso9660 4.1G 4.1G 0 100% /mnt
红色:表示执行过程出现异常;
橘黄颜色:表示命令执行后目标有状态变化;
绿色:表示执行成功且没有目标机器作修改。
ansible-doc [options] [module……]
[root@centos01 ~]#ansible-doc -l
[root@centos01 ~]# ansible-doc ping
> PING (/usr/lib/python2.7/site-packages/ansible/modules/system/ping.py)
A trivial test module, this module always returns `pong' on successful contact. It
does not make sense in playbooks, but it is useful from `/usr/bin/ansible' to verify
the ability to login and that a usable python is configured. This is NOT ICMP ping,
this is just a trivial test module.
EXAMPLES:
# Test we can logon to 'webservers' and execute python with json lib.
ansible webservers -m ping
MAINTAINERS: Ansible Core Team, Michael DeHaan
METADATA:
Status: ['stableinterface']
Supported_by: core
Ansible-playbook playbook.yml
<!--playbook.yml文件要提早编写好,建议使用绝对路径-->
[root@centos01 ~]# ansible-console
Welcome to the ansible console.
Type help or ? to list commands.
<!--输入help或?获取帮助-->
root@all (2)[f:5]$ cd web <!--使用cd命令切换主机或分组-->
root@web (2)[f:5]$ list <!--列出当前的设备-->
192.168.100.20
192.168.100.30
<!--支持Tab键补全,快捷键Ctrl+D或Ctrl+C便可退出当前的虚拟终端-->
chdir:在远程主机上运行命令前要提早进入的目录;
creates:在命令运行时建立一个文件,若是文件已存在,则不会执行建立任务;
removes:在命令运行时移除一个文件,若是文件不存在,则不会执行移除任务;
executeable:指明运行命令的shell程序。
[root@centos01 ~]# ansible web -m command -a "chdir=/ ls ./"
[root@centos01 ~]# ansible web -m shell -a "echo hello world " <!--输出到屏幕-->
192.168.100.20 | SUCCESS | rc=0 >>
hello world
192.168.100.30 | SUCCESS | rc=0 >>
hello world
[root@centos01 ~]# ansible web -m shell -a "echo hello world > /1.txt" <!--输出到1.txt文件中-->
192.168.100.20 | SUCCESS | rc=0 >>
192.168.100.30 | SUCCESS | rc=0 >>
dest:指出复制文件的目标目录位置,使用绝对路径。若是源是目录,则目标也要是目录,若是目标文件已存在,会覆盖原有内容;
src:指出源文件的路径,可使用相对路径和绝对路径,支持直接指定目录。若是源是目录,则目标也要是目录;
mode:指出复制时,目标文件的权限,可选;
owner:指出复制时,目标文件的属主,可选;
group:指出复制时目标文件的属组,可选;
content:指出复制到目标主机上的内容,不能和src一块儿使用,至关于复制content指明的数据到目标文件中。
[root@centos01 ~]# ansible web -m copy -a "src=/etc/hosts
dest=/root/a1.hosts mode=777 owner=root group=root"
<!--/将本机的hosts文件复制到Web组中的全部主机上存放在家目录下的a1.hosts目录,
权限是777,属主是root,属组是root-->
[root@centos01 ~]# ansible 192.168.100.20 -m hostname -a "name=test"
<!--将192.168.100.20的主机名改成test,
可是192.168.100.20须要敲一下bash才生效-->
name:程序包名称,能够带上版本号。若不指明版本,则默认为最新版本;
state=present|atest|absent:指明对程序包执行的操做:present代表安装程序包,latest表示安装最新版本的程序包,absent表示卸载程序包;
disablerepo:在用yum安装时,临时禁用某个仓库的ID;
enablerepo:在用yum安装时,临时启用某个仓库的ID;
conf_file:yum运行时的配置文件,而不是使用默认的配置文件;
disable_gpg_check=yes|no:是否启用完整性校验功能。
[root@centos01 ~]# ansible web -m shell -a "/usr/bin/rm -rf
/etc/yum.repos.d/CentOS-*"
<!--批量化删除web组主机的yum源-->
[root@centos01 ~]# ansible web -m shell -a "/usr/bin/mount
/dev/cdrom /mnt" <!--批量化挂载光盘-->
[WARNING]: Consider using mount module rather than running mount
192.168.100.20 | SUCCESS | rc=0 >>
mount: /dev/sr0 写保护,将以只读方式挂载
192.168.100.30 | SUCCESS | rc=0 >>
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# ansible web -m yum -a "name=httpd
state=present" <!--批量化安装httpd程序-->
[root@centos01 ~]# ansible web -m shell -a "rpm -qa | grep httpd"
<!--批量化查看安装的httpd程序包-->
[WARNING]: Consider using yum, dnf or zypper module rather than running rpm
192.168.100.20 | SUCCESS | rc=0 >>
httpd-2.4.6-67.el7.centos.x86_64
httpd-tools-2.4.6-67.el7.centos.x86_64
192.168.100.30 | SUCCESS | rc=0 >>
httpd-2.4.6-67.el7.centos.x86_64
httpd-tools-2.4.6-67.el7.centos.x86_64
[root@centos01 ~]# ansible web -m shell -a "systemctl start httpd" <!--批量启动服务-->
[root@centos01 ~]# ansible web -m shell -a "netstat -anptu | grep httpd" <!--批量化监听httpd服务是否启动成功-->
192.168.100.20 | SUCCESS | rc=0 >>
tcp6 0 0 :::80 :::* LISTEN 2072/httpd
192.168.100.30 | SUCCESS | rc=0 >>
tcp6 0 0 :::80 :::* LISTEN 3098/httpd
name:被管理的服务名称;
state=started|stopped|restarted:动做包含启动,关闭或重启;
enable=yes|no:表示是否设置该服务开机自启动;
runlevel:若是设定了enabled开机自启动,则要定义在哪些运行目标下自动启动。
[root@centos01 ~]# ansible web -m service -a "name=httpd
enabled=yes state=restarted"
<!--设置httpd服务从新启动和开机自动启动-->
name:必选参数,帐号名称;
state=present|absent:建立帐号或者删除帐号,present表示建立,absent表示删除;
system=yes|no:是否为系统帐户;
uid:用户UID;
group:用户的基本组;
groups:用户的附加组;
shell:默认使用的shell;
home:用户的家目录;
mve_home=yes|no:若是设置的家目录已经存在,是否将已存在的家目录进行移动;
pssword:用户的密码,建议使用加密后的字符串;
comment:用户的注释信息;
remore=yes|no:当state=absent时,是否要删除用户的家目录。
[root@centos01 ~]# ansible web -m user -a "name=user01
system=yes uid=502 group=root groups=root shell=/etc/nologin
home=/home/user01 password=pwd@123"
<!--在web组的全部主机上新建一个系统用户,UID为502,
属组是root,名字是user01,密码是pwd@123-->
[root@centos01 ~]# grep -v ^# /etc/ansible/hosts | grep -v ^$ <!--查看hosts中的分组信息-->
[web1]
192.168.100.20
[web2]
192.168.100.30
[root@centos01 ~]# vim /etc/ansible/a.yml
<!--建立a.yml文件,写入如下内容-->
---
- hosts: web1 <!--针对web1组中的操做-->
remote_user: root <!--远端执行用户身份为root-->
tasks: <!--任务列表-->
- name: adduser <!--任务名称-->
user: name=user1 state=present <!--执行user模块,建立用户-->
tags: <!--建立tag标签-->
- aaa <!--tag标签为aaa-->
- name: addgroup <!--任务名称-->
group: name=root system=yes <!--执行group模块,建立组-->
tags: <!--建立tag标签-->
- bbb <!--tag标签为bbb-->
- hosts: web2 <!--针对web2组中的操做-->
remote_user: root <!--远端执行用户身份为root-->
tasks: <!--任务列表-->
- name: copy file to web <!--任务名称-->
copy: src=/etc/passwd dest=/home <!--执行copy模块,复制文件-->
tags: <!--建立tag标签-->
- ccc <!--tag标签为ccc-->
...
hosts:任务的目标主机,多个主机用冒号分隔,通常调用/etc/ansible/hosts中的分组信息;
remote_user:远程主机上,运行此任务的默认身份为root;
tasks:任务,即定义的具体任务,由模块定义的操做列表;
handlers:触发器,相似tasks,只是在特定的条件下才会触发的任务。某任务的状态在运行后为changed时,可经过“notify”通知给相应的handlers进行触发执行;
roles:角色,将hosts剥离出去,由tasks、handlers等所组成的一种特定的结构集合。
ansible-playbook [option] /PATH/TO/PLAYBOOK.yaml
--syntax-check:检测yaml文件的语法;
-C(--check):预测试,不会改变目标主机的任何设置;
--list-hosts:列出yaml文件影响的主机列表;
--list-tasks:列出yaml文件的任务列表;
--list-tags:列出yaml文件中的标签;
-t TAGS(--tags=TAGS):表示只执行指定标签的任务;
--skip-tags=SKIP_TAGS:表示除了指定标签的任务,执行其余任务;
--start-at-task=START_AT:从指定的任务开始往下运行;
[root@centos01 ~]# ansible-playbook --syntax-check /etc/ansible/a.yml <!--语法检测-->
playbook: /etc/ansible/a.yml <!--表示没有报错-->
[root@centos01 ~]# ansible-playbook -C /etc/ansible/a.yml
<!--对a.yml进行预测试-->
.................<!--省略部份内容-->
192.168.100.20 : ok=3 changed=1 unreachable=0 failed=0
192.168.100.30 : ok=2 changed=1 unreachable=0 failed=0
<!--返回结果表示没有错误,所有能够执行成功。-->
[root@centos01 ~]# ansible-playbook --list-hosts /etc/ansible/a.yml
<!--列出a.yml文件中的主机-->
[root@centos01 ~]# ansible-playbook --list-tasks /etc/ansible/a.yml
<!--列出任务-->
[root@centos01 ~]# ansible-playbook --list-tags /etc/ansible/a.yml <!--列出标签-->
[root@centos01 ~]# ansible-playbook /etc/ansible/a.yml <!--执行任务-->
[root@centos01 ~]# ssh 192.168.100.20 tail -1 /etc/passwd <!--确认执行结果-->
user1:x:1001:1001::/home/user1:/bin/bash
[root@centos01 ~]# ssh 192.168.100.30 ls -ld /home/passwd
-rw-r--r--. 1 root root 2342 7月 23 16:06 /home/passwd
<!--通常状况先执行“-C”命令进行预测试,没有问题后再执行.yml文件。-->
handlers是Ansible提供的条件机制之一。handlers和task很相似,可是它只在被task通知的时候才会触发执行。
handlers只会在全部任务执行完成后执行。并且即便被通知了不少次,它也只会执行一次。handlers按照定义的顺序依次执行。
[root@centos01 ~]# ssh 192.168.100.20 netstat -anpt | grep 80 <!--查询100.20主机监听的端口-->
tcp6 0 0 :::80 :::* LISTEN 94858/httpd
<!--能够看到是监听80端口,如今经过脚本改成8080端口,并使其生效。-->
[root@centos01 ~]# vim /etc/ansible/httpd.yml
<!--编辑httpd.yml文件,写入如下内容-->
---
- hosts: web1
remote_user: root
tasks:
- name: change port
command: sed -i 's/Listen\ 80/Listen\ 8080/g' /etc/httpd/conf/httpd.conf
notify: <!--配置触发条件-->
- restart httpd server <!--完成该任务后调用名为“restart httpd server”的触发器-->
handlers: <!--配置触发器-->
- name: restart httpd server <!--指定触发器名字,要和上面“notify”指定的触发器名字同样-->
service: name=httpd state=restarted<!--触发任务为重启httpd服务-->
...
<!--编写完成后,保存退出便可-->
[root@centos01 ~]# ansible-playbook -C /etc/ansible/httpd.yml <!--进行预测试-->
[root@centos01 ~]# ansible-playbook /etc/ansible/httpd.yml <!--执行脚本-->
[root@centos01 ~]# ssh 192.168.100.20 netstat -anpt | grep 8080 <!--远端主机已经运行8080端口-->
tcp6 0 0 :::8080 :::* LISTEN 103594/httpd
MariaDB:MySQL角色;
Apache:httpd角色;
Nginx:Nginx角色。
files:存放由copy或script等模块调用的文件;
templates:存放template模块查找所须要的模板文件的目录,如MySQL配置文件模板;
tasks:任务存放的目录;
handlers:存放相关触发执行的目录;
vars:变量存放的目录;
meta:用于存放此角色元数据;
default:默认变量存放的目录,文件中定义了此角色使用的默认变量。
- hosts: web
remote_user: root
roles:
- mysql <!--调用角色名-->
- httpd <!--调用角色名-->
要求被管理主机上自动安装MariaDB,安装完成后上传提早准备好的配置文件至远端主机,重启服务,而后新建testdb数据库,并容许test用户对其拥有全部权限。
被管理主机配置yum仓库,自行配置,若被管理端能够链接互联网,那么直接将yum仓库指向互联网便可。
[root@centos01 /]# mkdir -pv /etc/ansible/roles/mariadb/{files,tasks,handlers}
mkdir: 已建立目录 "/etc/ansible/roles/mariadb"
mkdir: 已建立目录 "/etc/ansible/roles/mariadb/files"
mkdir: 已建立目录 "/etc/ansible/roles/mariadb/tasks"
mkdir: 已建立目录 "/etc/ansible/roles/mariadb/handlers"
[root@ansible /]# cd /etc/ansible/roles/mariadb/tasks/ <!--切换至指定目录-->
[root@centos01 tasks]# ls
[root@centos01 tasks]# vim main.yml <!--编写main.yml文件-->
---
- name: install mariadb
yum: name=mariadb-server state=present
- name: move config file
shell: "[ -e /etc/my.cnf ] && mv /etc/my.cnf /etc/my.cnf.bak"
- name: provide a new config file
copy: src=my.cnf dest=/etc/my.cnf
- name: reload mariadb
shell: systemctl restart mariadb
- name: create database testdb
shell: mysql -u root -e "create database testdb;grant all on testdb.* to 'test'@'192.168.100.%' identified by 'test123';flush privileges;"
notify:
- restart mariadb
...
<!--编写完毕,保存退出便可-->
[root@centos01 tasks]# cd ../handlers/ <!--切换至触发器目录-->
[root@centos01 handlers]# vim main.yml <!--编写main.yml文件,写入如下内容-->
---
- name: restart mariadb
service: name=mariadb state=restarted
...
<!--编写完毕,保存退出便可-->
[root@centos01 handlers]# cd ../files <!--进入mariadb角色文件夹的files-->
[root@centos01 files]# pwd
/etc/ansible/roles/mariadb/files
[root@centos01 files]# ls <!--准备好配置好的mysql数据库配置文件,须要分发到远程主机的-->
my.cnf
[root@centos01 files]# cd /etc/ansible/
[root@centos01 ansible]# vim mariadb.yml <!--编写.yml文件-->
---
- hosts: web
remote_user: root
roles:
- mariadb
...
<!--编写完毕,保存退出便可-->
[root@centos01 ansible]# ansible-playbook -C mariadb.yml <!--进行预检测-->
........................ <!--省略部份内容-->
PLAY RECAP ***************************************************************************
192.168.100.20 : ok=3 changed=1 unreachable=0 failed=0
<!--返回结果表示没问题-->
[root@centos01 ansible]# ansible-playbook mariadb.yml <!--执行安装-->
原文连接:
https://blog.51cto.com/14156658/2461907
欢迎你们关注个站:damon8.cn。
往期回顾
ArrayList、LinkedList 你真的了解吗?
浅谈 Java 集合 | 底层源码解析
Spring Cloud Kubernetes之实战一配置管理
Spring Cloud Kubernetes之实战二服务注册与发现
Spring Cloud Kubernetes之实战三网关Gateway
关注公众号,回复入群,获取更多惊喜!公众号(程序猿Damon)里回复 ES、Flink、Java、Kafka、MQ、ML、监控、大数据、k8s 等关键字能够查看更多关键字对应的文章。
若有收获,点个在看,谢谢
本文分享自微信公众号 - 程序猿Damon(Damon4X)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。