基于ANSIBLE中的YAML非标记语言&Role角色扮演

YAML-YAML Ain’t Markup Language-非标记语言

  • 语法

    • 列表
      • fruits:
      • ​ - Apple
      • ​ - Orange
      • ​ - Strawberry
      • ​ - Mango
    • 字典
      • martin:
      • ​ name : Martin D’vloper
      • ​ job : Developer
      • ​ skill : Elite
  • 示例1

    • 需求

      • 通过YAML编写一个简单的剧本,完成web的部署,配置,启动的全过程。
    • ansible服务器

      • 准备工作

        • ansible all -m yum -a ‘name=httpd state=removed’ -o
          • 清理一下环境
          • all表示所有配置的主机
        • yum install -y httpd
          • 准备配置文件
        • mkdir apache
        • cd apache
        • pwd
          /root/apache
        • cp -rf /etc/httpd/conf/httpd.conf /root/apache
        • grep -n ‘^Listen’ httpd.conf
          • Listen 8080
            • 修改配置,从80 改为8080 用作推送
      • 编写剧本

        • vim apache.yaml

          /root/apache/apache.yaml

          - hosts: host2
            tasks:
            - name: install apache packages
              yum: name=httpd state=present
            - name: copy apache conf
              copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
            - name: ensure apache is running
              service: name=httpd state=started enabled=yes
          
          

          注释:

          hosts: 要执行该脚本的目标主机
          tasks: 总任务
          name   任务名称
          yum    具体任务
          		name=httpd state=present  模块属性 名称和状态
          
          
      • 测试:

        • ansible-playbook apache.yaml --syntax-check

          • 检验语法
        • ansible-playbook apache.yaml --list-tasks

          • 列出任务
        • ansible-playbook apache.yaml --list-hosts

          • 列出主机
        • ansible-playbook apache.yaml

          • 执行
        • http://192.168.145.142:8080/

  • handlers

  • 如果配置文件发生变化。

    • Listen 9000

      vim /root/apache/httpd.conf
      
      Listen 8090
      
  • ansible-playbook apache.yaml

    • 去host2查看配置文件
      • vim /etc/httpd/conf/httpd.conf
      • 再次执行,命令成功,但配置未生效,所以要增加处理程序。设置触发器
  • 访问查看

    • 会发现访问失败
    • host2中查看httpd端口号

      # netstat -naltp | grep httpd
      tcp6       0      0 :::8080                 :::*                    LISTEN      53570/httpd
      
      

      因为,修改配置后,需要重启服务才能生效

  • vim apache.yaml

- hosts: host2
  tasks:
  - name: install apache packages
    yum: name=httpd state=present
  - name: copy apache conf
    copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify: restart apache service
  - name: ensure apache is running
    service: name=httpd state=started enabled=yes
  handlers:
  - name: restart apache service
    service: name=httpd state=restarted`
  • 如果配置文件再发生变化。

    • Listen 8091

    • vim /root/apache/httpd.conf
      
      Listen 8091
      
  • ansible-playbook apache.yaml

    • 再次执行,配置生效,触发成功

Role-角色扮演

  • 简介

    • roles则是在ansible中,playbooks的目录组织结构。 将代码或文件进行模块化,成为roles的文件目录组织结构, 易读,代码可重用,层次清晰。
  • 目标

    • 通过role远程部署nginx并配置
  • 1.目录结构
    请添加图片描述

- nginx 角色名

   files  普通文件 

   handlers  触发器程序 

   tasks  主任务 

   templates 金甲模板(有变量的文件) 

   vars 自定义变量
  • 准备目录结构

  • 下面都在ansible服务器下操作:

    • mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p
      
    • touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
      
    • echo 1234 > roles/nginx/files/index.html
      
    • yum install -y nginx && cp /etc/nginx/nginx.conf  有空格roles/nginx/templates/nginx.conf.j2
      
  • 2.编写任务

    • vim roles/nginx/tasks/main.yaml

      • ---
        - name: install epel-release packge
          yum: name=epel-release state=latest
        
        - name: install nginx packge
          yum: name=nginx  state=latest
        
        - name: copy index.html
          copy: src=index.html dest=/usr/share/nginx/html/index.html
        
        - name: copy nginx.conf template
          template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
          notify: restart nginx
        
        - name: make sure nginx service running
          service: name=nginx state=started enabled=yes
        
        
        • 对迭代项的引用,固定变量名为"item”,使用with_item属性给定要迭代的元素;
  • 3.准备配置文件

    • vim roles/nginx/templates/nginx.conf.j2

      • worker_processes {{ ansible_processor_cores }};

        • 调用内部已知变量

          # ansible host1 -m setup -a 'filter=ansible_processor_cores'
          host1 | SUCCESS => {
              "ansible_facts": {
                  "ansible_processor_cores": 1,
                  "discovered_interpreter_python": "/usr/bin/python3"
              },
              "changed": false
          }
          
          
      • worker_connections {{ worker_connections }};

        • 自定义变量
  • 4.编写变量

    • vim roles/nginx/vars/main.yaml
      • worker_connections: 10240
  • 5.编写处理程序

    • vim roles/nginx/handlers/main.yaml

      ---
      - name: restart nginx
        service: name=nginx state=restarted
      
  • 6.编写剧本

    • site.yaml 操作的对象

    • vim roles/site.yaml

      - hosts: host4
        roles:
        - nginx
      
  • 7.实施

    • cd roles

    • ansible-playbook site.yaml --syntax-check

      • 测试
    • ansible-playbook site.yaml

      • 实施剧本
    • 验证host4

    • host4查看nginx配置文件,变量已替换

vim /etc/nginx/nginx.conf

最后可能会形成:

[root@localhost ~]# tree /root/roles/
/root/roles/
├── docker
│   ├── files
│   │   └── index.html
│   ├── handlers
│   │   └── main.yaml
│   ├── tasks
│   │   └── main.yaml
│   ├── templates
│   │   └── nginx.conf.j2
│   └── vars
│       └── main.yaml
├── lvs
│   ├── files
│   │   └── index.html
│   ├── handlers
│   │   └── main.yaml
│   ├── tasks
│   │   └── main.yaml
│   ├── templates
│   │   └── nginx.conf.j2
│   └── vars
│       └── main.yaml
├── mysql
│   ├── files
│   │   └── index.html
│   ├── handlers
│   │   └── main.yaml
│   ├── tasks
│   │   └── main.yaml
│   ├── templates
│   │   └── nginx.conf.j2
│   └── vars
│       └── main.yaml
├── nginx
│   ├── files
│   │   └── index.html
│   ├── handlers
│   │   └── main.yaml
│   ├── tasks
│   │   └── main.yaml
│   ├── templates
│   │   └── nginx.conf.j2
│   └── vars
│       └── main.yaml
└── site.yaml

相关推荐

  1. AnsibleYAML语法

    2024-07-19 07:10:04       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 07:10:04       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 07:10:04       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 07:10:04       57 阅读
  4. Python语言-面向对象

    2024-07-19 07:10:04       68 阅读

热门阅读

  1. ESC(ELectronic Stability Control,电子稳定控制系统)

    2024-07-19 07:10:04       20 阅读
  2. PWM控制技术在电机驱动中的应用(内附资料)

    2024-07-19 07:10:04       21 阅读
  3. Eclipse 内容辅助

    2024-07-19 07:10:04       20 阅读
  4. 01 MySQL

    01 MySQL

    2024-07-19 07:10:04      19 阅读
  5. ZPL Viewer工具网站

    2024-07-19 07:10:04       22 阅读
  6. Selenium - 设置元素等待及加载策略

    2024-07-19 07:10:04       16 阅读
  7. RabbitMq

    RabbitMq

    2024-07-19 07:10:04      16 阅读
  8. vite + vue3 + uniapp 项目从零搭建

    2024-07-19 07:10:04       22 阅读
  9. React中Hooks几个有用的 ref

    2024-07-19 07:10:04       19 阅读
  10. Android RecyclerView实现Item单条滑动,一屏滑动

    2024-07-19 07:10:04       17 阅读
  11. crc、ecc、 uboot和 boot的原理分析及详解

    2024-07-19 07:10:04       22 阅读