0
点赞
收藏
分享

微信扫一扫

五月学习之Ansible playbook

一、playbook介绍

1、什么是playbook

playbook 是一个 由 yml 语法编写的文本文件,它由play 和 task 两部分组成。
play: 主要定义要操作主机或者主机组
task:主要定义对主机或主机组具体执行的任务,可以是一个任务,也可以是多个任务(模块)

总结: playbook 是由一个或多个 play 组成,一个play 可以包含多个 task任务。
可以理解为: 使用多个不同的模块来共同完成一件事情。

五月学习之Ansible playbook_Server

2、Playbook与Ad-Hoc

1) playbook 是对 AD-Hoc 的一种编排方式。
2) playbook 可以持久运行,而 Ad-Hoc 只能临时运行。
3) playbook 适合复杂的任务,而 Ad-Hoc 适合做快速简单的任务。
4) playbook 能控制任务执行的先后顺序。

3、Playbook书写格式

playbook 是由 yml 语法书写,结构清晰,可读性强,所以必须掌握 yml 语法
语法                         描述
缩进                         YAML使用固定的缩进风格表示层级结构,每个缩进由两个空格组成, 不能使用tabs
冒号                         以冒号结尾的除外,其他所有冒号后面所有必须有空格。
短横线                       表示列表项,使用一个短横杠加一个空格。多个项使用同样的缩进级别作为同一列表。

4、示例

1.下面我们一起来编写一个playbook文件,playbook起
步
host: 对哪些主机进行操作
remote_user: 我要使用什么用户执行
tasks: 具体执行什么任务
[root@manager ~]# cat f1.yml
---
- hosts: all
  remote_user: root
  vars:
    file_name: xuliangwei
  tasks:
    - name: Create New File
      file: name=/tmp/{{ file_name }} state=touch
2.执行playbook,注意观察执行返回的状态颜色:
红色:表示有task执行失败,通常都会提示错误信息。
黄色:表示远程主机按照编排的任务执行且进行了改变。
绿色:表示该主机已经是描述后的状态,无需在次运行。

二、playbook案例实战

1、Ansible部署NFS示例

[root@manager project]# cat nfs_server.yaml 
- hosts: webservers
  tasks:
    - name: Installed NFS Server
      yum:
        name: nfs-utils
        state: present

    - name: COnfigure NFS Server
      copy:
        src: ./exports.j2
        dest: /etc/exports
        owner: root
        group: root
        mode: '0644'
      notify: Restart NFS Server

    - name: Create SHare DIrectory 
      file:
        path: /ansible
        state: directory
        owner: www
        group: www
        recurse: yes

    - name: Systemd Start NFS Server
      systemd:
        name: nfs
        state: started

  handlers:
    - name: Restart NFS Server
      systemd:
        name: nfs
        state: restarted


- hosts: 172.16.1.8
  tasks:
    - name: Mount NFS data
      mount:
        src: 172.16.1.7:/ansible
        path: /bb
        fstype: nfs
        opts: defaults
        state: mounted

echo "/data 172.16.1.0/24(rw,sync)" > exports.j2

检查语法
ansible-playbook nfs.yml --syntax-check

2、Ansible部署Httpd示例

cat web.yml
- hosts: web
  tasks:
    - name: Installed Httpd Server
      yum: name=httpd state=latest
    - name: Started Httpd Server
      service: name=httpd state=started enabled=yes
    - name: Started Firewalld Server
      service: name=firewalld state=started enabled=yes
    - name: Copy Httpd Web Page
      copy: content='This is Web Page' dest=/var/www/html/index.html
    - name: Configure Firewalld Permit Http
      firewalld: service=http immediate=yes permanent=yes state=enabled

3、Ansible部署Rsync示例

cat rsyncd.conf 
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = "backup dir by oldboy"
path =/backup

cat rsync.yaml 
- hosts: rsync_server
  tasks:
    - name: 01-install rsync
      yum: name=rsync state=installed
    - name: 02-create rsyncd.conf
      copy: src=/etc/ansible/file/rsyncd.conf dest=/etc
    - name: 03-create user rsync
      user: name=rsync create_home=no shell=/sbin/nologin
    - name: 04-create /backup directory
      file: path=/backup state=directory owner=rsync group=rsync
    - name: 05-create password file
      copy: content=rsync_backup:oldboy123 dest=/etc/rsync.password mode=600
    - name: 06-start rsync service
      service: name=rsyncd state=started enabled=yes

- hosts: rsync_client
  tasks:
    - name: 01-installl rsync
      yum: name=rsync state=installed
    - name: 02-create password file
      copy: content=oldboy123 dest=/etc/rsync.password mode=600
    - name: 03-create test file
      file: dest=/tmp/test.txt state=touch
    - name: 04-test rsync sending
      shell: rsync -avz /tmp/test.txt rsync_backup@172.16.1.7::backup --password-file=/etc/rsync.password

4、Ansible部署LAMP示例

使用 AnsiblePlaybook 方式构建 LAMP 架构,具体
操作步骤如下:
1.使用yum安装 httpd、php、php-mysql、mariadb、firewalld 等
2.启动 httpd、firewalld、mariadb 等服务
3.添加防火墙规则,放行 http 的流量,并永久生效
6.使用 get_url 下载http://fj.xuliangwei.com/public/index.php文件

1.针对主机进行分组管理,分组名称定义为 web
[root@m01 ~]# cat /etc/ansible/hosts
[web]
172.16.1.7
172.16.1.8
2.编写 LAMP 架构对应的 playbook 文件
cd /etc/ansible/playbook/
cat lamp.yml
---
- hosts: web
  tasks:
    - name: Installed LAMP Server
      yum: name=httpd,php,php-mysql,mariadb state=latest
    - name: Started Httpd Server
      service: name=httpd state=started enable=yes
    - name: Started Firewalld Server
      service: name=httpd state=started enable=yes
    - name: Get Url Index.php File
      get_url: url=http://fj.xuliangwei.com/public/index.php dest=/var/www/html/index.php
    - name: Configure Firewalld Permit Http
      firewalld: service=http immediate=yes permanent=yes state=enable

ansible-playbook --syntax-check lamp.yml

5、Playbook部署集群架构

1.使用多台节点部署 kodcloud 网盘
2.使用 Nginx 作为负载均衡统一调度
3.使用 Redis 实现多台节点会话保持

cat /etc/ansible/hosts
[dbservers]
172.16.1.5
[lbservers]
172.16.1.6
[webservers]
172.16.1.7
172.16.1.8

4、部署Redis
cat install_redis.yml
- hosts: dbservers
  tasks:
    - name: Installed Redis Server
      yum:
        name: redis
        state: present
    - name: Configure Redis Server
      template:
        src: conf/redis.j2
        dest: /etc/redis.conf
        owner: redis
        group: root
        mode: 0640
      notify: Restart Redis Server
    - name: Systemctl Redis Server
      systemd:
        name: redis
        state: started
        enabled: yes
  handlers:
    - name: Restart Redis Server
      systemd:
        name: redis
        state: restarted

5、部署PHP环境

6、部署负载均衡

举报

相关推荐

0 条评论