Ansible playbook剧本实战

什么是playbook

playbook:剧本,兵书之意

playbook是由什么组成:

play:定义主机和角色(主角,配角定义)

task:任务(角色的台词和动作)

在playbook中一个play可以由多个task组成

playbook语法:

yaml 语法

  • 缩进:每一个层级,要缩进两个空格
  • 冒号:除了以冒号结尾的内容,冒号后面都要加一个空格
  • 横杠:横杠后面要有空格(Python 列表数据类型)
- hosts: web_group              ## play部分,指定要执行的主机
  remote_user: root             ## 以root身份执行 (默认)
  tasks:                        ## 定义任务
  - name: install httpd and php ## 给任务起名
    yum:                        ## 模块
      - httpd                   ## 动作
      - php
  - name: configure httpd conf
    copy:
      src: /root/web/httpd.conf
      dest: /etc/httpd/conf

# ansible 写playbook后缀 .yml 或者 .yaml
# saltstack 写sls文件 后缀 .sls

## 多任务写法
- 主机s: 指定主机
  任务s:
  - 名字: 给任务起个名字
    模块:
     动作1: 值value
     动作2: 值value
     动作3: 值value
     动作4: 值value

- 主机s: 指定主机
  任务s:
  - 名字: 给任务起个名字
    模块:
      动作1: 值value
      动作2: 值value
      动作3: 值value
      动作4: 值value

playbook小练习

安装httpd

# 1.创建工作目录
[root@m01 ~]# mkdir /root/ansible

# 2.编写httpd剧本
[root@m01 ansible]# vim httpd.yml
- hosts: web_group
  tasks:
  - name: Install httpd
    yum:
      name: httpd
      state: present

# 3.执行剧本
[root@m01 ansible]# ansible-playbook httpd.yml
PLAY [web_group] ***************************************************************

TASK [Gathering Facts] *********************************************************
ok: [web01]
ok: [web02]

TASK [Install httpd] ***********************************************************
changed: [web01]
changed: [web02]

PLAY RECAP *********************************************************************
web01                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
web02                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

## 检测剧本语法
[root@m01 ~]# ansible-playbook --syntax-check httpd.yml

playbook: httpd.yml

## 测试执行
[root@m01 ansible]# ansible-playbook -C httpd.yml

启动httpd并加入开机自启

- hosts: web_group
  tasks:
  - name: Install httpd
    yum:
      name: httpd
      state: present

  - name: Start httpd Service
    service:
      name: httpd
      state: started
      enabled: True

写http前端页面

- hosts: web_group
  tasks:
  - name: Install httpd
    yum:
      name: httpd
      state: present

  - name: Start httpd Service
    service:
      name: httpd
      state: started
      enabled: True

  - name: Ser Wed Index
    copy:
      content: wyk_http_web
      dest: /var/www/html/index.html

不同的主机配置不同的网站

目前来说,想要根据不同主机配置不同的网站,我们可以使用多个play的方式,但是在生产环境中,我们需要写循环,来满足我们的需求,多个play了解即可

- hosts: web_group
  tasks:
  - name: Install httpd
    yum:
      name: httpd
      state: present

  - name: Start httpd Service
    service:
      name: httpd
      state: started
      enabled: True

- hosts: web01
  tasks:
  - name: Ser Wed01 Index
    copy:
      content: http_web01
      dest: /var/www/html/index.html

- hosts: web02
  tasks:
  - name: Ser Wed02 Index
    copy:
      content: http_web02
      dest: /var/www/html/index.html

playbook实战3.使用剧本实现部署wordpress

- hosts: all
  tasks:
  - name: 创建组
    group:
      name: www
      gid: 666
  - name: 创建用户
    user: 
      name: www
      uid: 666
      group: 666
      shell: /sbin/nologin
      create_home: no

- hosts: web
  tasks:
  - name: 创建包目录
    file:
      path: /root/nginx_php
      owner: root
      group: root
      state: directory
  - name: 解压nginx和php安装包至终端
    unarchive:
      src: /root/nginx_php.tgz
      dest: /root/nginx_php/
      remote_src: no
  - name: 安装nginx和php
    shell: yum -y localinstall /root/nginx_php/*.rpm
  - name: 解压wordpress安装包至终端
    file:
      path: /code 
      owner: root 
      group: root 
      state: directory
  - name: 解压wordpress安装包至终端
    unarchive:
      src: /root/wordpress-5.0.3-zh_CN.tar.gz
      dest: /code 
      remote_src: no
  - name: 配置文件
    file: 
      path: /etc/nginx.conf 
      state: absent
  - name: 配置文件
    file: 
      path: /etc/php-fpm.d/www.conf
      state: absent
  - name: 配置文件
    file: 
      path: /etc/nginx/conf.d/default.conf 
      state: absent
  - name: 配置文件
    copy: 
      src: /root/nginx.conf 
      dest: /etc/nginx/
  - name: 配置文件
    copy: 
      src: /root/www.conf 
      dest: /etc/php-fpm.d/
  - name: 配置文件
    copy:
      src: /root/boke.conf 
      dest: /etc/nginx/conf.d/
  - name: 配置文件
    file: 
      path: /coed/wordpress 
      owner: www 
      group: www 
      recurse: yes
  - name: 重启服务
    service:
      name: php-fpm 
      state: restarted 
      enabled: yes
  - name: 重启服务
    service: 
      name: nginx 
      state: restarted 
      enabled: yes
  - name: 安装nfs服务
    yum:
      name: nfs-utils 
      state: present

- hosts: db
  tasks:
  - name: 配置数据库
    shell: yum -y install mariadb-server.x86_64 MySQL-python.x86_64
  - name: 配置数据库
    yum: 
      name: mariadb-server.x86_64,MySQL-python.x86_64 
      state: present
  - name: 配置数据库
    service:
      name: mariadb.service 
      state: started 
      enabled: yes
  - name: 配置数据库
    mysql_user:
      name: wp_user 
      host: "172.16.1.%"
      password: "123" 
      priv: "wp.*:ALL"
  - name: 配置数据库
    mysql_db:
      name: wp

- hosts: nfs
  tasks:
    - name: 配置nfs服务器
      yum:
        name: nfs-utils 
        state: present
    - name: 配置nfs服务器
      copy:
        content: "/data 172.16.1.0/24(rw,sync,anonuid=666,anongid=666,all_squash)" 
        dest: /etc/exports
    - name: 配置nfs服务器
      file:
        path: /data 
        owner: www 
        group: www 
        state: directory
    - name: 配置nfs服务器
      service:
        name: nfs-utils 
        state: restarted 
        enabled: yes

- hosts: web
  tasks:
    - name: 挂载nfs服务器
      file:
        path: /code/wordpress/wp-content/uploads 
        owner: www 
        group: www 
        state: directory
    - name: 挂载nfs服务器
      service:
        name: nfs 
        state: started 
        enabled: yes
    - name: 挂载nfs服务器
      mount:
        src: 172.16.1.31:/data
        path: /code/wordpress/wp-content/uploads 
        fstype: nfs 
        state: mounted