zoukankan      html  css  js  c++  java
  • 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服务

  • 相关阅读:
    DNS放大攻击
    Java并发编程(四):并发容器(转)
    关注商业价值
    样式小记
    应用程序优化
    查看当前的连接和锁
    重命名你的数据库
    转:对XML插入操作
    对数据的分页再一次思考
    不浪费自己的时间,同时也不浪费别人的时间
  • 原文地址:https://www.cnblogs.com/ElegantSmile/p/12588806.html
Copyright © 2011-2022 走看看