ansible-playbook剧本

ansible-playbook

介绍:

​ playbook剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

​ playbook是ansible用于配置,部署,和管理被控节点的剧本。通过playbook的详细描述,执行其中的一系列tasks,可以让远端主机达到预期的状态。playbook就像Ansible控制器给被控节点列出的的一系列to-do-list,而被控节点必须要完成。
​ playbook 字面意思,即剧本,现实中由演员按照剧本表演,在Ansible中,这次由计算机进行表演,由计算机安装,部署应用,提供对外服务,以及组织计算机处理各种各样的事情。

  • 将多个要执行ansible操作安装顺序整合到一个文件中,这个文件就是yaml
  • 执行yaml文件的方法是通过ansible-playbook执行

yaml文件

yaml文件中的元素

  • 变量
  • 循环
  • 判断

变量

变量命名规则

  • 只能以字母为开头
  • 构成只能有数字、字母、下划线

变量类别

  • 自定义
  • 内置变量

获取内置变量

[root@localhost ~]#  ansible all -m setup

执行a.yaml,同时传递变量name=tom,age=20

[root@localhost ~]#  ansible-playbook a.yaml --extra-vars "name=tom, age=20"

案例:安装httpd,要求设置apache的端口为808,网站跟目录/myweb

- host: all                     # 指定要操作的主机组remoute_user: root            # 指定在远程主机上以哪个用户身份执行tasksvars:                         # 定义变量- port: 808                   # 变量名、变量值- root: /myweb                # 变量名、变量值tasks:                        # 指定要执行的操作列表- name: install httpd                # 设置一个名称,用于给用户提示正在执行操作yum: name=httpd state=latest       # yum是模块名,后面是参数- name: start httpd              service: name=httpd state=started  # service是模块名,后面的参数

yaml文件中主要构成

  • host:
  • tasks:
  • vars:
  • remote_user:

案例:通过一个yaml文件实现如下要求:

在被管理主机上新建一个用户组,组名mygp1,组id是800

再在每个被管理主机上新建用户tom01,将tom加入到这个组中

将当前主机上/etc/inittab分发到所有被管理主机的/tmp下

# 创建yaml文件
[root@localhost ~]#  vim user.yaml
- hosts: allremote_user: roottasks:- name: create gropugroup: name=mygp1 gid=800 state=present- name: create useruser: name=tom group=mygp1 state=present- name: send filecopy: src=/etc/inittab dest=/tmp
# 执行yaml文件
[root@localhost ~]#  ansible-playbook user.yaml

案例1:在所有被管理节点安装httpd,然后启动httpd

- hosts: allremote_user: roottasks:- name: install httpdyum: name=httpd state=present- name: start httpdservice: name=httpd state=started enabled=true

触发器

  • 让一个task在特定的情况下才会被执行

案例2:在所有被管理节点安装httpd,然后启动httpd,要求httpd启动端口是8080

- hosts: allremote_user: roottasks:- name: install httpdyum: name=httpd state=present - name: start httpdservice: name=httpd state=started enabled=true- name: send httpd.confcopy: src=/root/httpd.conf.template dest=/etc/httpd/conf/httpd.confnotify:- restart httpdhandlers:- name: restart httpdservice: name=httpd state=restarted

yaml使用变量

(1)自定义变量

- hosts: allremote_user: rootvars:- package_name: mysql-server     # mariadb-server- service_name: mysqld           # mariadbtasks:- name: install mysql serveryum: name={{ package_name }} state=latest- name: start mysql serverservice: name={{ service_name }} state=started

(2)使用ansible的内置变量

案例:

在每个被管理主机上创建一个用户,用户名和主机名相同

在每个被管理主机上创建一个和主机同名的目录

# 查询内置变量
[root@localhost ~]#  ansible all -m setup | grep fqdn"ansible_fqdn": "centos6-2", 
[root@localhost ~]#  cat c.yaml
- hosts: alltasks:- name: create useruser: name={{ ansible_fqdn }} state=present- name: create filefile: name=/tmp/{{ ansible_fqdn }} state=touch

(3)主机清单变量

# 定义主机变量
[webservers]
11.11.11.12 userid=1050
11.11.11.13 userid=1060# 定义主机组变量
[webservers:vars]
username=jerry
- hosts: alltasks:- name: create useruser: name={{ username }} uid={{ userid }} state=present

template

  • template称为模板
  • 模板的用处是用来动态生成服务的配置文件

判断

  • 格式:when 条件
  • 做用:判断满足条件,才执行操作

案例:用playbook实现如下功能

1:在所有被管理主机上安装zsh

2:在主机名为centos7-2的主机上创建用户tom3,其他主机不创建

- hosts: alltasks:- name: install zshyum: name=zsh state=present- name: create user tom3user: name=tom3 state=presentwhen: ansible_fqdn == "centos7-5"

循环

格式:

  • 内置变量:item
  • 循环格式:with_items 列表

注意:

  • playbook中的循环一般用于循环次数少,而且简单的情况
  • 循环次数多,而且复杂,肯定是在本地创建shell脚本,然后使用script模块执行

案例:在所有被管理主机上创建5个用户u1 u2 u3 u4 u5

- hosts: alltasks:- name: create useruser: name={{ item }} state=presentwith_items:- u1- u2- u3- u4- u5

tags

作用:给某个task设置一个表情,用于仅仅执行某个task

案例:

- hosts: alltasks:- name: isntallshell: yum install httpd -y- name: send filecopy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.confnotify:- restart httpdtags:- senfile- name: start httpdshell: systemctl start httpdhandlers:- name: restart httpdshell: systemctl restart httpd
[root@localhost ~]# ansible-playbook 4.yaml --tags="senfile"

amsbile-playbool应用1

安装ansible并配置远程主机免密连接

[root@master ~]# yum install epel-release -y
[root@master ~]# yum install ansible -y
[root@master ~]# ssh-keygen -t rsa
[root@master ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.31.64
[root@master ~]# vi /etc/ansible/hosts
[server]
192.168.31.64

2)(编写lnmp.yml,以下所有操作必须写在playbook中)

准备php测试页面

[root@master ~]# vi /roo/a.php 
<?php
phpinfo();
?>

准备nginx配置文件

[root@master ~]# yum install epel-release -y 
[root@master ~]# yum install nginx -y
[root@master ~]# cp /etc/nginx/nginx.conf.default /root/nginx.conf

修改三个位置

  location / {root   /var/www/html;index  index.html index.htm;}location ~ \.php$ {root           /var/www/html;fastcgi_pass   127.0.0.1:9000;fastcgi_index  index.php;fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;include        fastcgi_params;}

编写yaml文件

- hosts: alltasks:- name: create dirshell: mkdir -pv /var/www/html- name: install basic softshell: yum install make gcc gcc-c++ zlib-devel pcre-devel ncurses-devel -y- name: send nginx packagecopy: src=/root/nginx-1.17.10.tar.gz dest=/usr/local/- name: install nginxshell: cd /usr/local/ && tar xf nginx-1.17.10.tar.gz && cd nginx-1.17.10 && ./configure --prefix=/usr/local/nginx && make && make install - name: send nginx.confcopy: src=/root/nginx.conf dest=/usr/local/nginx/conf/nginx.conf- name: start nginxshell: /usr/local/nginx/sbin/nginx- name: install php and mysqlshell: yum install php-fpm mariadb-server -y- name: start php and mysqlshell: systemctl start php-fpm && systemctl start mariadb- name: send test pagecopy: src=/root/a.php dest=/var/www/html

使用ansible模块分发源码包、安装包

3)使用ansible模块安装nginx依赖环境

4)编译安装nginx

5)推送配置文件并启动nginx

6)安装mysql和php相关服务

7)配置nginx和php整合

8)重启nginx并配置php测试页面为hello world

9)成功运行playbook

[root@master ~]# ansible-playbook a.yaml


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部