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

  • 相关阅读:
    Codeforces Round #706 (Div. 2)
    Caddi Programming Contest 2021(AtCoder Beginner Contest 193)
    [ARC116] Deque Game
    Codeforces Round #721 (Div. 2)
    Codeforces Round #618 (Div. 1)
    Educational Codeforces Round 109 (Rated for Div. 2)
    [ABC201F] Insertion Sort
    AtCoder Regular Contest 119
    Codeforces Global Round 13
    Codeforces Round #673 (Div. 1)
  • 原文地址:https://www.cnblogs.com/ElegantSmile/p/12588806.html
Copyright © 2011-2022 走看看