目录
Ansible-Playbook
playbooks介绍
Playbooks 是 Ansible的配置,部署,编排语言.他们可以被描述为一个需要希望远程主机执行命令的方案,或者一组IT程序运行的命令集合.
在基础层面, playbooks 可以被用来管理用于部署到远程主机的配置文件.在更高的层面上,playbooks 可以依次对多层式架构上的服务器执行上线包括滚动更新在内的操作并可以将操作委托给其他主机包括在此过程中发生的与监视服务器,负载均衡服务器的交互操作在内.
Ansible playbook使用场景
- 执行一些简单的任务,使用ad-hoc命令可以方便的解决问题,但是有时一个设施过于复杂,需要大量的操作的时候,执行的ad-hoc命令是不合适的,这时候最好使用playbook。
- 就像执行shell命令与写shell脚本一样,也可以理解为批处理任务,不过playbook有自己的语法格式
- 使用playbook可以方便的重复使用这些代码,可以移植到不同的机器上面,像函数一样,最大化的利用代码。在你使用Ansible的过程中,你也会发现,你所处理的大部分操作都是编写playbook。可以把常见的应用都编写playbook,之后管理服务器会变得很简单。
Ansible playbook格式
- 开始时(---)结束时(...)
- 每次开始写playbook内容时,写上描述
- #注释代码
- 缩进必须统一,不能空格和tab混用
核心元素
- Hosts:主机组
- Tasks:任务列表
- Variables:变量,设置方式有四种
- Templates:包含了模块语法的文本文件
- Handlers:由特定条件触发的任务。
playbook安装Apache
#编写playbook文件
vim apache01.yml
---
- hosts: web
remote_user: root
tasks:
- name: 创建一个apache用户
user: name=apache system=yes shell=nologin
- name: 编写yum仓库
yum_repository: name=local description=local baseurl=file:///media/cdrom enabled=1 gpgcheck=no file=lyh
- name: 挂载镜像
mount: src=/dev/cdrom path=/media/cdrom fstype=iso9660 state=mounted
- name: 安装apache
yum : name=httpd state=installed
- name: 准备Apache页面图片
copy: src=1.png dest=/var/www/html/
- name: 准备Apache页面
copy: content='<img src="1.png" />' dest=/var/www/html/index.html
- name: 开启apache
service: name=httpd state=started
...
playbook触发器
notify
: 触发器当遇到更改时触发handlers
handlers
:触发器触发后执行的动作
tags标签
tags标签的作用
在一个playbook中,如果只想执行某一个任务,那么可以给每个任务集打上标签,这样在执行的时候可以通过-t选择指定标签执行,还可以通过--skip-tags选择除了某个标签外全部执行等。
tags案例
如下图:
给创用户和yum仓库打上标签,执行命令
ansible-playbook -t aaaa apache01.yml
#多个标签之间用,分割
ansible-playbook -t aaaa,bbbb apache01.yml
ansible-playbook --skip-tags aaaa apache01.yml
playbook中template(模板)的使用
什么是jinja2模板
jinjia模板是在ansible中建立的一类模板文件,通常以.j2结尾标识。模板的内容含有多个变量,使原本固定的某个文件配置,通过参数的改变,变得可以复用,提高了使用效率。
{{ }}
:用来装载表达式,比如变量、运算表达式、比较表达式等。{% %}
:用来装载控制语句,比如 if 控制结构,for循环控制结构。{# #}
:用来装载注释,模板文件被渲染后,注释不会包含在最终生成的文件中。
变量操作示例
vim test01.j2
######
test01 jianja2
test {{ aaaa }} test {{#aaaa}}是变量名
######
#aaaa=1是给变量赋值
ansible all -m template -e "aaaa=1" -a "src=/root/test01.j2 dest=/root/test01"
到被管理端查看/root/有一个test01的文件,里面的内容是
上述示例中,"{{ }}“中包含的就是一个变量,当模板被渲染后,变量的值被替换到了最终的配置文件中,当然,既然在模板中定义了变量,那么就要保证在渲染模板时,可以调用到对应的变量,否则就会报错
比较表达式示例
vim test02.j2
####
{{ 5 == 5 }}
{{ 5 != 5 }}
{{ 4 > 2 }}
{{ 3 >= 2 }}
{{ 4 < 1 }}
{{ 0 <= 1 }}
####
ansible all -m template -a "src=/root/test02.j2 dest=/root/test02"
到被管理端查看/root/有一个test02的文件,里面的内容是
逻辑运算符示例
vim /root/test03.j2
####
{{ (3 > 1) or (1 > 3) }}
{{ (3 > 1) and (1 > 3) }}
{{ not true }}
{{ not True }}
{{ not false }}
{{ not False }}
####
ansible all -m template -a "src=/root/test03.j2 dest=/root/test03"
到被管理端查看/root/有一个test03的文件,里面的内容是
算数运算示例
vim /root/test04.j2
####
{{ 4 + 2 }}
{{ 3 - 5 }}
{{ 3 * 5 }}
{{ 6 ** 3 }}
{{ 4 / 5 }}
{{ 5 // 5 }}
{{ 17 % 5 }}
####
ansible all -m template -a "src=/root/test04.j2 dest=/root/test04"
到被管理端查看/root/有一个test04的文件,里面的内容是
template应用示例
vim /root/apache02.yml
####
---
- hosts: all
remote_user: root
vars:
- listen_port: 8888 #变量名:变量值
tasks:
- name: Install Httpd
yum: name=httpd state=installed
- name: peizhimoban
template: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: Restart Httpd
- name: 启动httpd
service: name=httpd state=started
handlers:
- name: Restart Httpd
service: name=httpd state=restarted
####
#将httpd配置文件复制到/root/,并修改
.....
42 Listen {{ listen_port }} #{{ }} 里面的值必须和apache02.yml中vars内的变量名一致
.....
#运行
ansible-playbook apache02.yml
到被管理端查看
template--when讲解
---
- hosts: web
remote_user: root
vars:
- listen_port: 8888
tasks:
- name: Install Httpd
yum: name=httpd state=installed
- name: cetos6 httpd配置文件
template: src=httpd6.conf dest=/etc/httpd/conf/httpd.conf
when: ansible_distribution_major_version == "6" #判断系统版本,如果是6则执行上边的template
notify: Restart Httpd
- name: cetos7 httpd配置文件
template: src=httpd7.conf dest=/etc/httpd/conf/httpd.conf
when: ansible_distribution_major_version == "7" #判断系统版本,如果是7则执行上边的template
notify: Restart Httpd
- name: 启动httpd
service: name=httpd state=started
handlers:
- name: Restart Httpd
service: name=httpd state=restarted
template--with_items讲解
---
- hosts: all
remote_user: root
tasks:
- name: Install Package
yum: name= {{ item }} state=installed #item循环安装下面三个软件
with_items:
- httpd
- vsftpd
- nginx
#with_items可以用于迭代一个列表或字典,通过{{ item }}获取每次迭代的值。
#等同于
---
- hosts: all
remote_user: root
tasks:
- name: Install Httpd
yum: httpd=httpd state=installed
- name: Install Vsftp
yum: httpd=vsftp state=installed
- name: Install Nginx
yum: httpd=nginx state=installed
案例一:创建三个组,创建三个用户分别加入这三个组
vim zu.yml
---
- hosts: web
remote_user: root
tasks:
- name: 创建组
group: name={{ item }} state=present
with_items:
- group1
- group2
- group3
- name: 创建用户
user: name={{ item.name }} group={{ item.group }} state=present
with_items:
- { name: 'user1', group: 'group1' }
- { name: 'user2', group: 'group2' }
- { name: 'user3', group: 'group3' }
ansible-playbook zu.yml
案例二:
vim /root/peizhi.yml
---
- hosts: all
remote_user: root
vars:
nginx_vhost_port:
- 81
- 82
- 83
tasks:
- name: nginx peizhiduankouhao
template: src=nginx.conf.j2 dest=/tmp/nginx_test.conf
vim /root/nginx.conf.j2
{% for port in nginx_vhost_port %}
server{
listen: {{ port }};
server_name: localhost;
}
{% endfor %}
#执行
ansible-playbook peizhi.yml
效果如下:
在被管理端查看
案例三:
vim /root/anli.yml
---
- hosts: all
remote_user: root
vars:
nginx_vhosts:
- web1:
listen: 8081
server_name: "web1.example.com"
root: "/var/www/nginx/web1"
- web2:
listen: 8082
server_name: "web2.example.com"
root: "/var/www/nginx/web2"
- web3:
listen: 8083
server_name: "web3.example.com"
root: "/var/www/nginx/web3"
tasks:
- name: Templage Nginx Config
template: src=nginx.conf.j2 dest=/tmp/nginx_vhost.conf
vim /root/nginx.conf.j2
{% for vhost in nginx_vhosts %}
server{
listen: {{ vhost.listen }};
server_name: {{ vhost.server_name }};
root: {{ vhost.root }};
}
{% endfor %}
#运行
ansible-playbook anli.yml
在被管理端查看
template--for if讲解
vim /root/anli.yml
---
- hosts: all
remote_user: root
vars:
nginx_vhosts:
- web1:
listen: 8081
server_name: "web1.example.com"
root: "/var/www/nginx/web1"
- web2:
listen: 8082
server_name: "web2.example.com"
root: "/var/www/nginx/web2"
- web3:
listen: 8083
server_name: "web3.example.com"
root: "/var/www/nginx/web3"
tasks:
- name: Templage Nginx Config
template: src=nginx.conf.j2 dest=/tmp/nginx_vhost.conf
vim /root/nginx.conf.j2
{% for vhost in nginx_vhosts %}
server{
{% if vhost.listen is defined %} #如果listen已经定义了,则使用定义的,如果没定义,则使用8888
listen: {{ vhost.listen }};
{% else %}
listen: 8888;
{% endif %}
{% if vhost.server_name is defined %}
server_name: {{ vhost.server_name }};
{% endif %}
root: {{ vhost.root }}
}
{% endfor %}
#运行
ansible-playbook anli.yml
在被管理端查看
剧本安装zabbix-agent
vim /root/zabbix_agent.yaml
---
- hosts: web
remote_user: root
vars:
pkgs:
- libxml2-devel
- libcurl-devel
- pcre-devel
tasks:
- name: 安装依赖
yum: name={{ pkgs }} state=installed
- name: 解包
unarchive: src=/root/zabbix-6.0.5.tar.gz dest=/usr/src/
- name: 进入解压路径并安装
shell: ./configure --prefix=/usr/local/zabbix --enable-agent --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2
args:
chdir: /usr/src/zabbix-6.0.5
- name: 编译安装
shell: make && make install
args:
chdir: /usr/src/zabbix-6.0.5
- name: 创建组
group: name=zabbix
- name: 创建用户
user: name=zabbix shell=/sbin/nologin group=zabbix
- name: 修改权限
file: path=/usr/local/zabbix owner=zabbix group=zabbix
- name: 启动zabbixanget
shell: /usr/local/zabbix/sbin/zabbix_agentd
- name: 修改配置文件
copy: src=/root/zabbix_agentd.conf dest=/usr/local/zabbix/etc/zabbix_agentd.conf backup=yes
notify: restart zabbix_agent
handlers:
- name: restart zabbix_agent
shell: killall -9 zabbix_agent
shell: /usr/local/zabbix/sbin/zabbix_agentd
被管理端查看
playbook编译安装nginx
vim /root/nginx.yml
---
- hosts: db
remote_user: root
vars:
yilai:
- pcre-devel
- zlib-devel
tasks:
- name: 创建一个nginx用户
user: name=nginx shell=/bin/nologin system=yes
- name: 安装依赖
yum: name={{ yilai }} state=installed
- name: 解压目录
unarchive: src=/root/nginx-1.23.3.tar.gz dest=/usr/src/
- name: 配置
command: ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
args:
chdir: /usr/src/nginx-1.23.3
- name: 编译安装
shell: make && make install
args:
chdir: /usr/src/nginx-1.23.3
- name: 软连接nginx命令
shell: cp /usr/local/nginx/sbin/nginx /usr/sbin/nginx
- name: 启动nginx
command: nginx
...