随笔 - 58  文章 - 0  评论 - 1  阅读 - 4861

剧本(playbook)

1、剧本(playbook)

PlayBook即"剧本""兵书"之意,PlayBook是由以下部分组成的
 
play(host): 定义的是主机的角色。(主角还是配角)
Book(task): 定义的是具体执行的任务。(角色的台词和动作)
playbook: 由一个或多个play(角色)组成,一个play(角色)可以包含多个task(台词,动作)。

剧本的格式是一个YAML文件格式。

2、组成

1、host	: 指定的需要操作的主机
2、vars	: 指定的是变量(非必须)
3、tasks	: 具体要执行的任务
4、remote_user : 执行时使用的用户

- hosts: web01
  tasks:
    - name: 测试剧本
      shell: 'ls'
    - name: 测试剧本2
      shell: 'touch 1.txt'
    - name: 安装Nginx
      yum:
        name: nginx
        state: present

3、剧本的变量

1、主机清单

2、变量

	2.1、全局变量
		- hosts: web01
           vars:
             package_name: nginx

    2.2、局部变量
    	    - name: 安装Nginx
              vars:
                package_name: mariadb
              yum:
                name: "{{ package_name }}"
                state: present


    2.3、配置清单中的变量
    	[web01:vars]
         package_name=redis


    2.4、在命令行中设置变量
    	[root@localhost ansible]# ansible-playbook test.yaml -e 'package_name=mamecache'


    2.5、变量的优先级
    	在命令行中设置变量 > 局部变量 > 全局变量 > 配置清单

案例:使用剧本部署超级玛丽

- hosts: web01
  vars:
    package_name: nginx
  tasks:
    - name: 上传代码
      unarchive:
        src: /root/mario.tar.gz
        dest: /usr/share/nginx/html
        remote_src: no
    - name: 安装Nginx
      yum:
        name: "{{ package_name }}"
        state: present
    - name: 启动Nginx
      service:
        name: "{{ package_name }}"
        state: restarted

4、流程控制

4.1、条件判断(when)

案例:在CentOS系统中,安装Nginx
- hosts: web01
  tasks:
    - name: 安装Nginx
      yum:
        name: nginx
        state: present
      when: ansible_distribution == "CentOS"

and : 并且
or  : 或者


案例:判断Nginx服务是否启动
- hosts: web01
  tasks:
    - name: nginx服务监测
      script: './1.sh'
      register: check_nginx
    - name: 查看check_nginx变量
      debug: var=check_nginx
    - name: 启动Nginx
      service:
        name: nginx
        state: started
      when: check_nginx.rc == 0
      
[root@localhost ansible]# cat 1.sh 
#!/bin/bash
function main() {
    RES=`systemctl is-active nginx`
    if [ $RES == 'unknown' ];then
      return 1
    else
      return 0
    fi
}
main
案例
- hosts: web02
  tasks:
   - name: 安装Nginx
     yum:
       name: nginx
       state: present
     when:
       - ansible_distribution == "CentOS"
       - ansible_distribution_major_version == "7"
   - name: 安装HTTPD
     yum:
       name: httpd
       state: present
     when:
       - ansible_distribution == "CentOS"
       - ansible_distribution_major_version == "6"

4.2、循环流程(with_item、items)

案例:要求安装nginx、mariadb等软件
- hosts: web01
  tasks:
    - name: 安装Nginx
      yum:
        name: "{{ item }}"
        state: present
      with_items:
        - nginx
        - mariadb

案例:要求安装Nginx、卸载mariaDB
- hosts: web01
  tasks:
    - name: 安装Nginx
      yum:
        name: "{{ item.name }}"
        state: "{{ item.state }}"
      with_items:
        - {"name":"nginx","state":"present"}
        - {"name":"mariadb","state":"absent"}
        
        
安装:nginx、mariadb
卸载:redis

5、在某种条件下执行任务

案例:要求在安装完成之后再启动Nginx
- hosts: web01
  tasks:
    - name: 安装Nginx
      yum:
        name: "{{ item.name }}"
        state: "{{ item.state }}"
      with_items:
        - {"name":"nginx","state":"present"}
      notify: 启动Nginx
  handlers:
    - name: 启动Nginx
      service:
        name: nginx
        state: restarted

6、标签

- hosts: web01
  tasks:
    - name: 安装Nginx
      yum:
        name: "{{ item.name }}"
        state: "{{ item.state }}"
      with_items:
        - {"name":"nginx","state":"present"}
      tags: nginx

    - name: 安装mariadb
      yum:
        name: mariadb
        state: present
      tags: mairadb


[root@localhost ansible]# ansible-playbook test.yaml -t mairadb,nginx
[root@localhost ansible]# ansible-playbook test.yaml -t mairadb
posted on   婷婷妮子  阅读(520)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示