ansible-playbook案例

本文介绍如何使用Ansible自动化工具来安装Apache服务器,并进行配置修改,包括更改监听端口至8080,设置ServerName,定义默认主页,以及实现服务自启动。同时,通过示例展示了如何使用Ansible的模块如yum、lineinfile、replace和service。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本案例要求:

  • 安装Apache并修改监听端口为8080
  • 修改ServerName配置,执行apachectl -t命令不报错
  • 设置默认主页hello world
  • 启动服务并设开机自启

   

---
- hosts: cache
  remote_user: root
  tasks:
    - name: install one specific version of Apache
      yum:
        name: httpd        //安装Apache
        state: installed
    - lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: '^Listen '
        line: 'Listen 8080'        //修改端口为8080
    - replace:
        path: /etc/httpd/conf/httpd.conf
        regexp: '^#(ServerName).*'        //配置ServerName
        replace: '\1 localhost'
    - service:
        name: httpd
        enabled: yes        //开机自启
        state: restarted
    - copy:
        src: /root/index.html        //修改主页,可以自己写个页面
        dest: /var/www/html/index.html

 

扩展的一个tags和notify以及handlers

---
- hosts: web1
  remote_user: root
  tasks:
    - name: install the latest version of Apache
      yum:
        name: httpd
        state: latest
    - replace:
        path: /etc/httpd/conf/httpd.conf
        regexp: '(^Listen).*'
        replace: '\1 8080'
        backup: yes
    - replace:
        path: /etc/httpd/conf/httpd.conf
        regexp: '(^#ServerName ).*'
        replace: '\1 localhost'
        backup: yes
    - copy:
        src: test1.yml
        #src: /root/index.html
        dest: /root/user13.yml
        #dest: /var/www/html
        owner: apache
        group: apache
        mode: 0644
      tags: haha
      notify:
        - restart httpd
        - restart vsftpd
        - modify port
  handlers:
    - name: modify port
      replace:
        path: /etc/httpd/conf/httpd.conf
        regexp: '(^Listen ).*'
        replace: '\1 80'
        backup: yes
    - name: restart httpd
      service: name=httpd state=restarted
    - name: restart vsftpd
      service: name=vsftpd state=restarted
    - service:
        name: httpd
        state: restarted
        enabled: yes

 

注意格式要求,一般的copy,replace,yum,sevice模块用ansible-doc 模块名  查看帮助就ok

转载于:https://www.cnblogs.com/lsgo/p/10422576.html

### Ansible Playbook 实战案例 #### 使用场景:Web 应用程序部署自动化 为了展示 Ansible Playbook 的实际应用场景,考虑一个常见的 Web 应用程序部署任务。此任务涉及安装和配置 Web 服务器(如 Apache),并确保防火墙设置允许 HTTP 流量通过。 ```yaml --- - hosts: webservers become: yes tasks: - name: Ensure Apache is installed yum: name: httpd state: present notify: Restart Apache - name: Enable and start the Apache service service: name: httpd state: started enabled: yes - name: Allow HTTP traffic through firewall firewalld: service: http permanent: true immediate: true state: enabled notify: Reload Firewalld handlers: - name: Restart Apache service: name: httpd state: restarted - name: Reload Firewalld command: firewall-cmd --reload ``` 上述脚本展示了如何利用 `yum` 模块来管理软件包的状态[^3],以及怎样借助 `service` 和 `firewalld` 模块控制服务运行状况和服务端口开放情况。当任何一项任务触发变更时,相应的处理器 (`handlers`) 将被调用来重启服务或重新加载防火墙规则。 #### 最佳实践要点: 1. **模块化设计**:将复杂的操作分解成多个独立的任务文件,并通过 `include_tasks` 或者 `import_tasks` 来组合它们。 2. **变量管理和重用**:定义全局变量或将特定环境下的参数存储在一个单独的 YAML 文件中以便于维护和调整[^1]。 3. **角色分离**:创建不同的角色以封装不同类型的功能逻辑,比如数据库初始化、应用程序部署等,从而提高可读性和复用率。 4. **测试驱动开发 (TDD)**:编写单元测试验证各个组件的行为是否符合预期;可以采用 Molecule 工具来进行集成测试[^2]。 5. **持续改进文档记录**:保持良好的注释习惯,在适当位置添加说明文字帮助其他开发者理解意图。 6. **安全加固措施**:遵循最小权限原则分配用户权限,定期审查已有的策略,移除不再使用的账户/密钥对等敏感信息。 7. **版本控制系统集成**:把所有的 Playbooks 及其依赖项纳入 Git 等 VCS 中跟踪变化历史,便于协作开发与回滚操作。 8. **静态分析工具的应用**:使用像 `ansible-lint` 这样的工具提前发现潜在错误,保证代码质量。 9. **动态库存支持**:对于大规模集群而言,构建基于云 API 获取最新主机列表的方式更加灵活高效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值