• ansible-playbook-roles基本使用


    1. ansible-角色-roles基本使用
      1.1) 创建roles目录结构

    1 [root@test-1 ansible]# mkdir -p /ansible/roles/{common,nginx,php}/{tasks,handlers,defaults,vars,files,templates,meta}/
    2 [root@test-1 ansible]# touch /ansible/{fooservers.yaml,hosts,site.yaml,webservers.yaml}
    3 [root@test-1 ansible]# touch /ansible/roles/{common,nginx,php}/{tasks,handlers,defaults,vars,files,templates,meta}/main.yaml

      1.2) 验证roles目录结构创建成功没

     1 [root@test-1 ansible]# tree /ansible/
     2 /ansible/
     3 ├── fooservers.yaml
     4 ├── hosts
     5 ├── roles
     6 │   ├── common
     7 │   │   ├── defaults
     8 │   │   │   └── main.yaml
     9 │   │   ├── files
    10 │   │   │   └── main.yaml
    11 │   │   ├── handlers
    12 │   │   │   └── main.yaml
    13 │   │   ├── meta
    14 │   │   │   └── main.yaml
    15 │   │   ├── tasks
    16 │   │   │   └── main.yaml
    17 │   │   ├── templates
    18 │   │   │   └── main.yaml
    19 │   │   └── vars
    20 │   │       └── main.yaml
    21 │   ├── nginx
    22 │   │   ├── defaults
    23 │   │   │   └── main.yaml
    24 │   │   ├── files
    25 │   │   │   └── main.yaml
    26 │   │   ├── handlers
    27 │   │   │   └── main.yaml
    28 │   │   ├── meta
    29 │   │   │   └── main.yaml
    30 │   │   ├── tasks
    31 │   │   │   └── main.yaml
    32 │   │   ├── templates
    33 │   │   │   └── main.yaml
    34 │   │   └── vars
    35 │   │       └── main.yaml
    36 │   └── php
    37 │       ├── defaults
    38 │       │   └── main.yaml
    39 │       ├── files
    40 │       │   └── main.yaml
    41 │       ├── handlers
    42 │       │   └── main.yaml
    43 │       ├── meta
    44 │       │   └── main.yaml
    45 │       ├── tasks
    46 │       │   └── main.yaml
    47 │       ├── templates
    48 │       │   └── main.yaml
    49 │       └── vars
    50 │           └── main.yaml
    51 ├── site.yaml
    52 └── webservers.yaml
    53 
    54 25 directories, 25 files

    2. 案例,roles案例基本使用
     2.1) 编写site.yaml 程序执行文件

     1 [root@test-1 ansible]# cat site.yaml 
     2 ---
     3 - hosts: web1
     4   gather_facts: no
     5 
     6 
     7   roles:
     8     - common
     9     - nginx
    10     - php

      2.2) 编写common公共使用tasks文件main.yaml

    1 [root@test-1 ansible]# vim /ansible/roles/common/tasks/main.yaml 
    2 [root@test-1 ansible]# cat /ansible/roles/common/tasks/main.yaml 
    3 - name: common task test
    4   debug: msg="common task test"

      2.3) 编写nginx的tasks文件main.yaml

    1 [root@test-1 ansible]# cat /ansible/roles/nginx/tasks/main.yaml 
    2 - name: nginx task test
    3   debug: msg="nginx task test"

    2.4) 编写php的tasks文件main.yaml

    1 [root@test-1 ansible]# cat /ansible/roles/php/tasks/main.yaml 
    2 - name: php task test
    3   debug: msg="php task test"

    2.5) 检查site.yaml配置文件是否正确

    1 [root@test-1 /]# cd /ansible/
    2 [root@test-1 ansible]# ls
    3 fooservers.yaml  hosts  roles  site.yaml  webservers.yaml
    4 [root@test-1 ansible]# ansible-playbook --syntax-check site.yaml 
    5 
    6 playbook: site.yaml

    3. 执行site.yaml文件
      3.1) 执行site.yaml文件

     1 [root@test-1 ansible]# ansible-playbook  site.yaml 
     2 
     3 PLAY [web1] *************************************************************************************************************************************************************
     4 
     5 TASK [common : common task test] ****************************************************************************************************************************************
     6 ok: [192.168.200.132] => {
     7     "msg": "common task test"
     8 }
     9 ok: [192.168.200.133] => {
    10     "msg": "common task test"
    11 }
    12 
    13 TASK [nginx : nginx task test] ******************************************************************************************************************************************
    14 ok: [192.168.200.132] => {
    15     "msg": "nginx task test"
    16 }
    17 ok: [192.168.200.133] => {
    18     "msg": "nginx task test"
    19 }
    20 
    21 TASK [php : php task test] **********************************************************************************************************************************************
    22 ok: [192.168.200.132] => {
    23     "msg": "php task test"
    24 }
    25 ok: [192.168.200.133] => {
    26     "msg": "php task test"
    27 }
    28 
    29 PLAY RECAP **************************************************************************************************************************************************************
    30 192.168.200.132            : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    31 192.168.200.133            : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

    提示:
      我们可以看到测试的roles在远程上已经调用配置文件,进行加载

  • 相关阅读:
    Linux企业级项目实践之网络爬虫(6)——将程序设计成为守护进程
    Linux企业级项目实践之网络爬虫(5)——处理配置文件
    Linux企业级项目实践之网络爬虫(3)——设计自己的网络爬虫
    Linux企业级项目实践之网络爬虫(4)——主程序流程
    Linux企业级项目实践之网络爬虫(1)——项目概述及准备工作
    Linux企业级项目实践之网络爬虫(2)——网络爬虫的结构与工作流程
    泛化、依赖、关联、聚合、组合
    日常(停课后的月考)
    日常(停课后的月考)
    打击罪犯
  • 原文地址:https://www.cnblogs.com/scajy/p/11699165.html
Copyright © 2020-2023  润新知