Ansible 之Playbook

时间:2023-03-09 03:30:29
Ansible 之Playbook

ansbile playbook是一系列ansible命令的集合,利用yaml 语言编写,playbook命令根据自上而下的顺序依次执行。同时,playbook开创了很多特性,它可以允许你传输某个命令的状态到后面的指令,如你可以从一台机器的文件中抓取内容并附为变量,然后在另一台机器中使用,这使得你可以实现一些复杂的部署机制,这是ansible命令无法实现的.

Playbook是Ansible的配置,部署,编排语言。他们可以被描述为一个需要希望远程主机执行命令的方案,或者一组IT程序运行的命令集合。

playbook的构成

playbook是由一个或多个"play"组成的列表。play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task定义好的角色。从根本上来讲所谓task无非是调用ansible的一个module。将多个play组织在一个playbook中即可以让它们联同起来按事先编排的机制同唱一台大戏。其主要有以下四部分构成:

Playbooks
Variables #变量元素,可传递给Tasks/Templates使用;
Tasks #任务元素,即调用模块完成任务;
Templates #模板元素,可根据变量动态生成配置文件;
Hadlers #处理器元素,通常指在某事件满足时触发的操作;
Roles #角色元素

使用Playbook时通过ansible-playbook命令使用,它的参数和ansible命令类似,如参数-k(–ask-pass) 和-K (–ask-sudo) 来询问ssh密码和sudo密码,-u指定用户,这些指令也可以通过规定的单元写在playbook里。

ansible-playbook的简单使用方法:

ansible-playbook /etc/ansible/site.yml 

以下是简单的playbook例子(play可以有多个):

1.使用playbook 实现新增一个用户的功能:

#vim create_user.yml

---
- hosts: test
gather_facts: false
user: root
vars:
user: "user1"
tasks:
- name: create user
user: name="{{ user }}" createhome=yes

各行参数详解:

hosts参数指定了对哪些主机进行参作,test为定义的主机组.
user参数指定了使用什么用户登录远程主机操作;
gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息.
vars参数,指定了变量,这里指定一个user变量,其值为user1,需要注意的是,变量值一定要用引号引住;
task指定了一个任务,其下面的name参数是对任务的描述,在执行过程中会打印出来
user提定了调用user模块,name是user模块里的一个参数,而增加的用户名字调用了上面user变量的值,createhome=yes表示创建用户家目录,也是user模块的参数对象. 同样,如果想实现把这个新增的用户删除,只需将该playbook文件的最后一行替换为如下行再执行相应的playbook即可:
user: name="{{ user }}" state=absent remove=yes

2.使用playbook更改一组主机主机名的方法:

---
- hosts : test
remote_user : root
tasks :
- name : show hostname
shell : hostname
- name: show ipaddress
command : ip addr
- hostname : name=benet.develop.com

说明:在tasks任务中,调用了shell和command模块来执行查询命令,- hostname是使用hostname模块的name方法来更改主机名.

3.使用playbook安装apache服务实例:

---
- hosts: test
remote_user: root
tasks:
- name: Install the latest version of apache.
yum: name=httpd state=latest
- name: write the apache config file
copy: src=/tmp/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd service
- name: Start the apache service
service: name=httpd state=started
handlers:
- name: restart httpd service
service: name=httpd state=restarted #注意:在notify中定义内容一定要和tasks中定义的 - name 内容一样,这样才能达到触发的效果,否则会不生效。

4.使用playbook安装Nginx实例:

---
- hosts: test
tasks:
- name: Install nginx Depend on the package
yum: pkg={{ item }} state=latest
with_items:
- openssl-devel
- gcc
- pcre-devel
- zlib-devel
- name: Installing NGINX repo rpm
yum: name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm - name: Installing NGINX
yum: name=nginx state=latest - name: write the NGINX config file
copy: src=/tmp/nginx.conf dest=/etc/nginx/nginx.conf
notify:
- restart NGINX service - name: Start the NGINX service
service: name=nginx state=started
handlers:
- name: restart NGINX service
service: name=nginx state=restarted

ansible-playbook - script module

使用script模块可以实现,ansible本机到对象节点上执行本机脚本。有点类似copy+shell+删除copy的脚本的这样一个综合的功能.

这样的好处是ansible所在服务器部署脚本就可以了,无需在所有远程的服务器上都部署一份,避免改动脚本时候要在所有远程服务器上都重新部署一遍.

/usr/bin/ansible-playbook -i /data/devops/etc/hosts.zt_prod /data/devops/etc/config_os_policy.yml -v

config_os_policy.yml

- hosts: all-hosts
remote_user: root
gather_facts: false tasks:
- name: set the system timezone
shell: "timedatectl set-local-rtc 0 && timedatectl set-timezone Asia/Shanghai" - name: modify os parameters
#shell: "/bin/bash /data/devops/bin/modify_os_parameters.sh"
script: "/data/devops/bin/modify_os_parameters.sh" //不需要使用/bin/bash指令.

注意点是,script模块后面是不需要用sh指令的了.