本篇主要给大家介绍Ansible-playbook的使用,往期文章中已经介绍了Ansible的基本使用以及配置。
往期参考
Ansible Playbook预先定义好所有需要执行的操作,形成一个脚本文件,执行该脚本文件即可,方便管理和复用。
Playbook,标准翻译过来是剧本的意思,也就是提前准备好演员要演绎的情节。就以演戏为例,之前使用的ansible简单命令相当于导演临时对着演员讲戏,演一场讲一场,可能还要对不同的演员讲同一场戏,而ansible playbook相当于导演提前准备好了所有一系列的场景,情节,演员只要拿到剧本,就基本可以演绎了,这对导演来说省了很多功夫。我猜现在,你大概能体会到ansible-playbook的意义了。
使用场景
我们再次设想一个场景,如果我需要在一台机器上安装nginx、prometheus应用,并且使用nginx代理转发prometheus,这时候我如果使用ansible ad-hoc命令来实现,可能需要输入多条命令,并且不方便再次面对这个场景时的复用。我们考虑将这些操作都写进一个ansible-playbook文件中,封装起来。
项目结构
├── hosts -- 主机清单文件
├── nginx-prometheus.yaml -- ansible-playbook文件,定义任务
├── nginx.conf -- nginx 配置文件
├── prometheus -- promethues 启动配置文件
└── prometheus.conf -- nginx代理转发prometheus配置文件
hosts,主机清单配置文件
[local]
localhost
nginx-prometheus.yaml
---
- hosts: local
name: install nginx and prometheus
become: yes
tasks:
- name: install nginx
apt:
name: nginx
state: latest
update_cache: yes
- name: "install prometheus step 1: import prometheus public GPG Key"
apt_key:
url: https://s3-eu-west-1.amazonaws.com/deb.robustperception.io/41EFC99D.gpg
state: present
- name: "install prometheus step 2: reload local package database"
apt:
update_cache: yes
- name: "install prometheus step 3: install prometheus"
apt:
name: prometheus
update_cache: yes
- name: "install prometheus step 4: Install prometheus-node-exporter"
apt:
name: prometheus-node-exporter
update_cache: yes
- name: "install prometheus step 5: Install prometheus-pushgateway"
apt:
name: prometheus-pushgateway
update_cache: yes
- name: "install prometheus step 6: Install prometheus-alertmanager"
apt:
name: prometheus-alertmanager
update_cache: yes
- name: "copy set prometheus subpath."
copy:
src: prometheus
dest: /etc/default/prometheus
- name: "copy to set for nginx."
copy:
src: nginx.conf
dest: /etc/nginx/nginx.conf
- name: "copy prometheus conf for nginx."
copy:
src: prometheus.conf
dest: /etc/nginx/conf.d/prometheus.conf
- name: "restart prometheus"
service:
name: prometheus
state: restarted
- name: "restart nginx"
service:
name: nginx
state: restarted
以上内容实际包括了大量的细节值得关注,有必要逐一说明下,除此之外还可以下文参考Ansible Modules:
- hosts:ansible主机清单中定义的主机ip、可解析域名或者主机组;
- name:每个ansible-playbook可以包含多个play,上面只写了一个play,每个play可以定义一个name;
- tasks:每个play可以包含多个task
- become:yes/no,默认no,是否使用sudo执行
- tasks.name:task定义的name,类似play定义的name,在ansible-playbook执行时,play或task执行时会按照顺序打印至控制台告知执行进度;
要实现nginx代理转发promethus,除了上面的ansible-playbook yaml文件,我们还需要准备以下文件供ansible-playbook使用。
nginx.conf,已经定制,用于nginx安装完成后,替换nginx配置文件
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
}
prometheus.conf,新增的nginx转发配置文件
server {
location /prometheus {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://localhost:9090/prometheus;
break;
}
}
prometheus,prometheus启动配置文件
ARGS="--web.external-url=prometheus"
使用方式
使用ansible-playbook命令执行,命令格式如下。
ansible-playbook -i <hosts_path> <playbook_path>
- hosts_path:主机清单文件位置
- playbook_path: playbook yaml文件位置
- -C,--check:加在ansible-playbook命令后,用于验证palybook文件是否有误
- -c,--connection:使用的连接类型,默认default=smart,可使用local本地连接使用
现在找一台Ubuntu机器,我们使用上面的项目文件,按照ansible-playbook命令执行,首先将当前目录切至项目下,然后执行以下命令。
# 由于没有服务器资源,就在本机执行 -c local
ansible-playbook -i hosts nginx-prometheus.yaml -c local
执行过程,控制台输出如下:
上面这行命令执行完成后,验证是否成功.
curl localhost/prometheus/
看到如下执行结果,说明我们已经成功的使用ansible-playbook实现这一系列操作了。
ubuntu@ip-xx:~/84579034$ curl localhost/prometheus
<a href="/prometheus/graph">Moved Permanently</a>.
到这里,就演示了一个比较完整的ansible-playbook的使用了。想更多的了解Ansible-playbook,还可以移步官方文档:
Ansible Modules
Ansible功能的单元,一个Ansible功能对应有一个Module,下面列举常用的几种Modules,其他Module可以去官方文档查找。
FILE
远程服务器上的文件进行操作。创建删除文件,修改文件权限等。
- path:执行文件、目录的路径
- recurse:递归设置文件属性,只对目录有效
- group:定义文件、目录的属组
- mode:定义文件、目录的权限
- owner:定义文件、目录的所有者
- src:要被链接的源文件路径,只应用与state为link的情况
- dest:被链接到的路径,只应用于state为link的情况
- force:在两种情况下会强制创建软链接,一种是源文件不存在但之后会建立的情况;一种是目标软链接已经存在,需要先取消之前的软链接,然后创建新的软链接,默认值 no。
ansible servers -m file -a 'path=~/temp state=directory' # 创建目录
ansible servers -m file -a 'path=~/temp/test1.txt state=touch' # 创建文件
COPY
将Control Node上的目录或文件拷贝至Managed Nodes。
- src:指定要复制到远程主机的文件或目录。如果路径以 / 结尾,则只复制目录里的内容,如果没有以 / 结尾,则复制包含目录在内的整个内容
- dest:文件复制的目的地,必须是一个绝对路径,如果源文件是一个目录,那么dest指向的也必须是一个目录
- force:默认取值为yes,表示目标主机包含此文件,但内容不同时,覆盖。
- backup:默认取值为no,如果为yes,在覆盖前将原文件进行备份
- directory_mode:递归设定目录权限,默认为系统默认权限
- others:所有file模块里的选项都可以在这里使用
touch test2.txt
ansible servers -m copy -a 'src=test2.txt dest=~/temp/test2.txt' # 拷贝文件到服务器
COMMAND
直接要求主机清单执行命令。
ansible servers -m command -a "touch ~/temp/test3.txt"
ansible servers -m command -a "sudo apt update"
ansible servers -m command -a "sudo apt install nginx -y"
ansible servers -m command -a "sudo apt autoremove nginx -y"
不支持shell变量,如$NAME, <,>,&,| 等
SHELL
可以先编辑shell脚本,然后要求主机清单执行shell脚本里的命令。
- name: Execute the command in remote shell; stdout goes to the specified file on the remote.
shell: somescript.sh >> somelog.txt
- name: Change the working directory to somedir/ before executing the command.
shell: somescript.sh >> somelog.txt
args:
chdir: somedir/
注意:shell脚本必须有可执行的权限
- name: 'chmod shell'
command: chmod 777 somedir/somescript.sh
APT
ubuntu/debian 包、应用管理。
- update_cache : yes/no,默认no,操作之前是否 apt-get update
- state :
- absent : 移除
- latest :最新
- present: 目前稳定的,默认
- autoremove :yes/no,默认no,移除依赖
- autoclean :yes/no,默认no,移除缓存
ansible servers -m apt -a "name=nginx update_cache=yes" -b
# append to playbook
- name: Install nginx
apt:
name: nginx
state: latest
update_cache: yes
ansible servers -m apt -a "name=nginx state=absent autoremove=yes autoclean=yes" -b
YUM
CentOs管理程序包。
SERVICE
管理服务的模块,用来启动、停止、重启服务。
- enabled :yes/no,默认no.是否开机启动。
- name :服务名
- state :
- reloaded
- restarted
- started
- stopped
- sleep :如果是restarted,需要等待多长秒后再重启 如:10,等待10秒后重启
ansible servers -m service -a "name=nginx state=started"
# playbook
- name: Start service nginx, if not started
service:
name: nginx
state: started
ansible servers -m service -a "name=nginx state=stoped"
GIT
-
Managed需要已经安装git。
-
执行git相关操作。
结束语
Ansible系列本在计划,但却许久未更了,惰性不移,难成良差。当时时自省吾身。
Ansible-palybook是更为推荐的ansible使用方式,操作更容易被记录下来,更容易被复用,甚至可以方便的与其他工具结合起来使用,能极大的提高生产效率。