• 05Ansible_YAML


    YAML语法

    列表

    fruits:
      - Apple
      - Orange
      - Strawberry
      - Mango
    

    字典

    martin:
      name: Martin D'vloper
      job: Developer
      skill: Elite
    

    Ansible YAML入门

    需求:通过YAML编写一个简单的剧本,完成web的部署,配置,启动的全过程。

    准备工作

    清理环境

    [root@ansible-server ~]# ansible all -m yum -a 'name=httpd state=removed' -o
    

    安装测试httpd

    用于获取配置文件

    [root@ansible-server ~]# yum install -y httpd
    [root@ansible-server ~]# cp -rf /etc/httpd/conf/httpd.conf /tmp/
    

    修改配置文件

    [root@ansible-server ~]# vim /tmp/httpd.conf
    Listen 8080
    

    编写剧本

    [root@ansible-server ~]# vim /tmp/apache.yaml 
    - hosts: test
      tasks:
      - name: install latest apache packages
        yum: name=httpd state=latest
      - name: copy apache config file
        copy: src=/tmp/httpd.conf dest=/etc/httpd/conf/httpd.conf
      - name: ensure apache httpd service is running
        service: name=httpd state=started enabled=yes
    

    测试

    检验语法

    [root@ansible-server ~]# ansible-playbook /tmp/apache.yaml --syntax-check
    

    列出任务

    [root@ansible-server ~]# ansible-playbook /tmp/apache.yaml --list-tasks
    

    列出主机

    [root@ansible-server ~]# ansible-playbook /tmp/apache.yaml --list-hosts
    

    执行

    [root@ansible-server ~]# ansible-playbook /tmp/apache.yaml
    

    访问网页

    网址:http://172.22.69.216:8080

    如果配置文件发生改变,要求服务进行重启,此时需要借助handlers的帮助:

    修改配置文件

    [root@ansible-server ~]# vim /tmp/httpd.conf
    Listen 9080
    

    修改剧本文件

    [root@ansible-server ~]# vim /tmp/apache.yaml 
    - hosts: test
      tasks:
      - name: install latest apache packages
        yum: name=httpd state=latest
      - name: copy apache config file
        copy: src=/tmp/httpd.conf dest=/etc/httpd/conf/httpd.conf
        notify: restart apache service
      - name: ensure apache httpd service is running
        service: name=httpd state=started enabled=yes
      handlers:
      - name: restart apache service
        service: name=httpd state=restart
    

    再次执行ansible-playbook /tmp/apache.yaml,触发handlers,重启httpd服务

  • 相关阅读:
    前端学习数据库之子元素
    最详细win7下手动搭建PHP环境:apache2.4.23+php7.0.11
    Javascript实现页面跳转的几种方式
    读书笔记:《HTML5开发手册》Web表单
    C#开发微信门户及应用(26)-公众号微信素材管理
    APP开发基础知识
    C#学习路线
    ASP.NET机制详细的管道事件流程
    SQL Serve中的6种事务隔离级别简单总结
    Asp.net设计模式笔记之一:理解设计模式
  • 原文地址:https://www.cnblogs.com/ElegantSmile/p/12588806.html
Copyright © 2020-2023  润新知