Ansible:playbook实战案例

发布于:2025-03-31 ⋅ 阅读:(28) ⋅ 点赞:(0)

Playbook核心元素

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

hosts 组件

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

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组

案例:

- hosts: webservers:appservers

remote_user 组件

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

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

---
- 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

范例:

---
- 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脚本实现

#!/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实现

---
- 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 命令

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

利用 playbook 创建 mysql 用户

mkdir playbook
 cd playbook/
vim mysql_user.yml
---
- 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
#检查文件是否有错误
ansible-playbook -C mysql_user.yml

在这里插入图片描述
还可以在yaml中增加一行gather_facts: no,禁止收集其他系统信息,增加运行速度:

---
- 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

在这里插入图片描述
执行:

ansible-playbook mysql_user.yml 

在这里插入图片描述

#查询是否新建成功
ansible dbservers -a 'id mysql'

在这里插入图片描述

ansible webservers -a 'ss -ntl'

在这里插入图片描述

playbook安装nginx

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

ansible webservers -m yum -a 'name=httpd state=absent'
  1. 创建playbook脚本
vim install_nginx.yml
---
#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代码

ansible-playbook -C install_nginx.yml

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

---
#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安装
ansible-playbook install_nginx.yml 

在这里插入图片描述
5. 查看nginx网络状态

ansible webservers -a 'netstat -anp|grep nginx'

在这里插入图片描述
6. 如果打开网页无法显示nginx界面,可以尝试关掉远程主机的防火墙:

ansible webservers -a 'systemctl stop firewalld.service'
ansible webservers -a 'systemctl disable firewalld.service'

在这里插入图片描述
7. 可以替换ngnix原始网页,显示成自己的页面:

mkdir files
vim files/index.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>
vim install_nginx.yml
---
#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. 重新运行
ansible-playbook install_nginx.yml 

在这里插入图片描述
在这里插入图片描述

利用 playbook 安装和卸载 httpd

本地先安装httpd

yum install httpd
cp /etc/httpd/conf/httpd.conf files/
vim files/httpd.conf

在这里插入图片描述

vim install_httpd.yml
---
#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
#先把nginx停掉
ansible webservers -m servers -a 'name=nginx state=stopped'
ansible-playbook -C install_httpd.yml
ansible-playbook  install_httpd.yml

在这里插入图片描述

---
- 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
ansible-playbook -C remove_httpd.yml 
ansible-playbook  remove_httpd.yml