Ansible:playbook实战案例

文章目录

  • Playbook核心元素
  • [playbook 命令](#playbook 命令)
    • [利用 playbook 创建 mysql 用户](#利用 playbook 创建 mysql 用户)
    • playbook安装nginx
    • [利用 playbook 安装和卸载 httpd](#利用 playbook 安装和卸载 httpd)

Playbook核心元素

  • Hosts 执行的远程主机列表
  • Tasks 任务集
  • Variables 内置变量自定义变量,在playbook中调用
  • Templates 模板,可替换模板文件中的变量并实现一些简单逻辑的文件
    -Handlersnotify 结合使用,由特定条件触发的操作,满足条件方才执行,否则不执行
  • tags 标签 指定某条任务执行,用于选择运行playbook中的部分代码。ansible具有幂等性,因此会自动跳过没有变化的部分,即便如此,有些代码为测试其确实没有发生变化的时间依然会非常地长。此时,如果确信其没有变化,就可以通过tags跳过此些代码片断。

hosts 组件

Hosts:playbook中的每一个play的目的都是为了让特定主机以某个指定的用户身份执行任务。hosts用于指定要执行指定任务的主机,须事先定义在主机清单中:

bash 复制代码
one.example.com
one.example.com:two.example.com
192.168.1.50
192.168.1.*
linux101
Websrvs:dbsrvs      #或者,两个组的并集
Websrvs:&dbsrvs     #与,两个组的交集
webservers:!phoenix  #在websrvs组,但不在dbsrvs组

案例:

yaml 复制代码
- hosts: webservers:appservers

remote_user 组件

remote_user: 可用于Host和task中。也可以通过指定其通过sudo的方式在远程主机上执行任务,其可用于play全局或某任务 ;此外,甚至可以在sudo时使用sudo_user指定sudo时切换的用户

当 Ansible playbook 与远程主机通信时,需要指定一个用户名和密码(或 SSH 私钥),以便连接到目标主机。

通过指定 remote_user,可以让 Ansible playbook 自动使用指定的用户名和密码(或 SSH 私钥)来连接目标主机。

yaml 复制代码
---
- hosts: websrvs
  remote_user: root

  tasks:
    - name: test connection
      ping:
      remote_user: magedu
      sudo: yes                 #默认sudo为root
      sudo_user:wang        #sudo为wang

task列表和action组件

  • play的主体部分是task list,task list中有一个或多个task,各个task 按次序逐个在hosts中指定的所有主机上执行,即在所有主机上完成第一个task后,再开始第二个task.
  • task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量。模块执行是幂等的,这意味着多次执行是安全的,因为其结果均一致
  • 每个task都应该有其name,用于playbook的执行结果输出,建议其内容能清晰地描述任务执行步骤。如果未提供name,则action的结果将用于输出

task两种格式:

  1. action: module arguments
  2. module: arguments 建议使用

注意:shell和command模块后面跟命令,而非key=value

范例:

yaml 复制代码
---
- hosts: webservers
  remote_user: root
  tasks:#两个任务:安装和启动服务
    - name: install httpd
      yum: name=httpd 
    - name: start httpd
      service: name=httpd state=started enabled=yes

其它组件

某任务的状态在运行后为changed时,可通过"notify"通知给相应的handlers

任务可以通过"tags"打标签,可在ansible-playbook命令上使用-t指定进行调用

ShellScripts VS Playbook 案例

SHELL脚本实现

bash 复制代码
#!/bin/bash
# 安装Apache
yum install --quiet -y httpd 
# 复制配置文件
cp /tmp/httpd.conf /etc/httpd/conf/httpd.conf
cp/tmp/vhosts.conf /etc/httpd/conf.d/
# 启动Apache,并设置开机启动
systemctl enable --now httpd 

Playbook实现

yaml 复制代码
---
- hosts: websrvs
  remote_user: root
  tasks:
    - name: "安装Apache"
      yum: name=httpd
    - name: "复制配置文件"
      copy: src=/tmp/httpd.conf dest=/etc/httpd/conf/
    - name: "复制配置文件"
      copy: src=/tmp/vhosts.conf dest=/etc/httpd/conf.d/
    - name: "启动Apache,并设置开机启动"
      service: name=httpd state=started enabled=yes

playbook 命令

  • 格式
bash 复制代码
ansible-playbook <filename.yml> ... [options]
  • 常见选项
    -C --check #只检测可能会发生的改变,但不真正执行操作
    --list-hosts #列出运行任务的主机
    --list-tags #列出tag
    --list-tasks #列出task
    --limit 主机列表 #只针对主机列表中的主机执行
    -v -vv -vvv #显示过程
    范例
bash 复制代码
ansible-playbook  file.yml  --check #只检测
ansible-playbook  file.yml  
ansible-playbook  file.yml  --limit webservers

利用 playbook 创建 mysql 用户

bash 复制代码
mkdir playbook
 cd playbook/
vim mysql_user.yml
yaml 复制代码
---
- hosts: dbservers
  remote_user: root

  tasks:
    - { name: create group,group: name=mysql system=yes gid=306 }
    - name: create user
      user: name=mysql shell=/sbin/nologin system=yes group=mysql uid=306 home=/data/mysql create_home=no
bash 复制代码
#检查文件是否有错误
ansible-playbook -C mysql_user.yml

还可以在yaml中增加一行gather_facts: no,禁止收集其他系统信息,增加运行速度:

yaml 复制代码
---
- hosts: dbservers
  remote_user: root
  gather_facts: no

  tasks:
    - { name: create group,group: name=mysql system=yes gid=306 }
    - name: create user
      user: name=mysql shell=/sbin/nologin system=yes group=mysql uid=306 home=/data/mysql create_home=no

执行:

bash 复制代码
ansible-playbook mysql_user.yml 
bash 复制代码
#查询是否新建成功
ansible dbservers -a 'id mysql'
bash 复制代码
ansible webservers -a 'ss -ntl'

playbook安装nginx

1.卸载httpd,因为httpd和nginx可能会有冲突

bash 复制代码
ansible webservers -m yum -a 'name=httpd state=absent'
  1. 创建playbook脚本
bash 复制代码
vim install_nginx.yml
yaml 复制代码
---
#install nginx 
- hosts: webservers
  remote_user: root
  gather_facts: no
  tasks:
    - name: add group nginx
      group: name=nginx state=present
    - name: add user nginx
      group: name=nginx state=present group=nginx
    - name: Install Nginx
      yum: name=nginx state=present
    - name: Start Nginx
      service: name=nginx state=started enabled=yes

3.检查playbook代码

bash 复制代码
ansible-playbook -C install_nginx.yml

如果显示安装nginx有问题,可以先安装epel-release

yaml 复制代码
---
#install nginx 
- hosts: webservers
  remote_user: root
  gather_facts: no
  tasks:
    - name: add group nginx
      group: name=nginx state=present
    - name: add user nginx
      group: name=nginx state=present group=nginx
    - name: install epel-release
      yum: name=epel-release
    - name: Install Nginx
      yum: name=nginx
    - name: Start Nginx
      service: name=nginx state=started enabled=yes
  1. 执行playbook安装
bash 复制代码
ansible-playbook install_nginx.yml 
  1. 查看nginx网络状态
bash 复制代码
ansible webservers -a 'netstat -anp|grep nginx'
  1. 如果打开网页无法显示nginx界面,可以尝试关掉远程主机的防火墙:
bash 复制代码
ansible webservers -a 'systemctl stop firewalld.service'
ansible webservers -a 'systemctl disable firewalld.service'
  1. 可以替换ngnix原始网页,显示成自己的页面:
bash 复制代码
mkdir files
vim files/index.html
html 复制代码
<!DOCTYPE html>  
<html lang="zh">  

<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>欢迎来到我的网站</title>  
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap" rel="stylesheet">  
    <style>  
        body {  
            margin: 0;  
            padding: 0;  
            font-family: 'Roboto', sans-serif;  
            background-color: #f0f8ff;  
            color: #333;  
            display: flex;  
            justify-content: center;  
            align-items: center;  
            height: 100vh;  
            text-align: center;  
        }  

        h1 {  
            font-size: 48px;  
            margin: 0;  
            color: #4A90E2;  
            transition: color 0.3s;  
        }  

        h1:hover {  
            color: #1C74B7;  
        }  

        p {  
            font-size: 20px;  
            margin: 20px 0;  
            line-height: 1.5;  
        }  

        a {  
            text-decoration: none;  
            color: #fff;  
            background-color: #4A90E2;  
            padding: 10px 20px;  
            border-radius: 5px;  
            font-size: 18px;  
            transition: background-color 0.3s;  
        }  

        a:hover {  
            background-color: #1C74B7;  
        }  

        footer {  
            position: absolute;  
            bottom: 20px;  
            font-size: 14px;  
            color: #777;  
        }  
    </style>  
</head>  

<body>  
    <div>  
        <h1>欢迎来到我的网站!</h1>  
        <p>这是一个简单而美观的欢迎界面。希望你能在这里找到你需要的信息。</p>  
        <a href="#" onclick="alert('感谢您的访问!')">点击我了解更多</a>  
    </div>  
    <footer>  
        &copy; 2023 我的网页版权所有  
    </footer>  
</body>  

</html>
bash 复制代码
vim install_nginx.yml
yaml 复制代码
---
#install nginx 
- hosts: webservers
  remote_user: root
  gather_facts: no
  tasks:
    - name: add group nginx
      group: name=nginx state=present
    - name: add user nginx
      group: name=nginx state=present group=nginx
    - name: install epel-release
      yum: name=epel-release
    - name: Install Nginx
      yum: name=nginx
    - name: web page
      copy: src=files/index.html dest=/usr/share/nginx/html/index.html
    - name: Start Nginx
      service: name=nginx state=started enabled=yes
  1. 重新运行
yaml 复制代码
ansible-playbook install_nginx.yml 


利用 playbook 安装和卸载 httpd

本地先安装httpd

bash 复制代码
yum install httpd
cp /etc/httpd/conf/httpd.conf files/
vim files/httpd.conf
bash 复制代码
vim install_httpd.yml
yaml 复制代码
---
#install httpd 
- hosts: webservers
  remote_user: root
  gather_facts: no

  tasks:
    - name: Install httpd
      yum: name=httpd state=present
    - name: Install configure file
      copy: src=files/httpd.conf dest=/etc/httpd/conf/
    - name: web html
      copy: src=files/index.html  dest=/var/www/html/
    - name: start service
      service: name=httpd state=started enabled=yes
bash 复制代码
#先把nginx停掉
ansible webservers -m servers -a 'name=nginx state=stopped'
ansible-playbook -C install_httpd.yml
ansible-playbook  install_httpd.yml
yaml 复制代码
---
- hosts: webservers
  remote_user: root
  gather_facts: no

  tasks:
    - name: remove httpd package
      yum: name=httpd state=absent
    - name: remove apache user
      user: name=apache state=absent
    - name: remove config file
      file: name=/etc/httpd  state=absent
    - name: remove web html
      file: name=/var/www/html/index.html state=absent
bash 复制代码
ansible-playbook -C remove_httpd.yml 
ansible-playbook  remove_httpd.yml 
相关推荐
七夜zippoe7 小时前
CANN Runtime任务描述序列化与持久化源码深度解码
大数据·运维·服务器·cann
盟接之桥7 小时前
盟接之桥说制造:引流品 × 利润品,全球电商平台高效产品组合策略(供讨论)
大数据·linux·服务器·网络·人工智能·制造
会员源码网8 小时前
理财源码开发:单语言深耕还是多语言融合?看完这篇不踩坑
网络·个人开发
米羊1219 小时前
已有安全措施确认(上)
大数据·网络
Fcy6489 小时前
Linux下 进程(一)(冯诺依曼体系、操作系统、进程基本概念与基本操作)
linux·运维·服务器·进程
袁袁袁袁满9 小时前
Linux怎么查看最新下载的文件
linux·运维·服务器
代码游侠9 小时前
学习笔记——设备树基础
linux·运维·开发语言·单片机·算法
主机哥哥9 小时前
阿里云OpenClaw部署全攻略,五种方案助你快速部署!
服务器·阿里云·负载均衡
Harvey9039 小时前
通过 Helm 部署 Nginx 应用的完整标准化步骤
linux·运维·nginx·k8s
ManThink Technology10 小时前
如何使用EBHelper 简化EdgeBus的代码编写?
java·前端·网络