首页 > 其他分享 >Ansible roles 动态变更

Ansible roles 动态变更

时间:2024-08-21 16:48:38浏览次数:8  
标签:httpd tasks roles yaml Ansible file apache 变更

目录

role 动态变更

通过一个功能完整的Roles来整体了解Vars,Files,Handlers,Meta,Templates,然后逐步深入Roles Handlers用法

示例example.yaml

site.yaml  
   webservers.yaml  
   fooservers.yaml  
   roles/  
      common/  
        files/  
        templates/  
        tasks/  
        handlers/  
        vars/  
        defaults/  
        meta/  
      webservers/  
        files/  
        templates/  
        tasks/  
        handlers/  
        vars/  
        defaults/  
        meta/

Playbooks中的调用方式如下

---
  - hosts: webservers
    roles:
     - common
     - webservers

roles对应功能的含义

roles/x/tasks/main.yaml 主函数,在其中任务都会执行
roles/x/handlers/main.yaml 所有包含其中的handlers都会执行
roles/x/vars/main.yaml 所有包含在其中的变量都会生效
roles/x/meta/main.yaml roles所有依赖都会写明
roles/x/{files,templates,tasks} 所有文件,模板都可放在这里

当Apache的配置文件发生变化时重启Apache进程

编排roles目录结构

  
roles/apache/  
├── handlers  
│ └── main.yaml  
├── files  
│ └── httpd.conf  
└── tasks  
 ├── restart.yaml
 └── main.yaml  

目录结构需按要求编排,不得随意变更名称

mkdir roles/apache/{handlers,tasks,files}

编辑roles/apache/handlers/main.yaml

重启Apache进程

---
- name: restart apache
  service: 
    name: apache2 
    state: restarted

编辑roles/apache/files/httpd.conf

apache2 配置文件,这里略过

....

编辑roles/apache/tasks/restart.yaml

更新Apache配置文件,如配置文件有变化则重启Apache

---
- name: transfer apache config
  copy: 
    src: httpd.conf 
    dest: /opt/apache/httpd.conf
  notify:
   - restart apache

编辑roles/apache/tasks/main.yaml

---
    - import_tasks: restart.yaml

编辑roles/apache.yaml

该yaml为总调度文件,完成Apache配置文件的变更和Apache的重启工作

---
  - hosts: ubuntu
    remote_user: root
    roles:
     - role: apache
ansible-playbook apache.yaml 

PLAY [ubuntu] *****

TASK [Gathering Facts] *****
ok: [192.168.255.110]

TASK [apache : transfer apache config] *****
changed: [192.168.255.110]

RUNNING HANDLER [apache : restart apache] *****
changed: [192.168.255.110]

PLAY RECAP *****
192.168.255.110            : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

roles 文件传输

files和templates均用于Ansible文件处理,files(不是file模块)目录下的文件无需写绝对路径即可将文件传输至远程主机,templates目录下的文件以Jinja2渲染,且传输文件至远程主机的同时支持预定义变量替换

编排目录结构如下

file.yaml
roles/file/
├── files
│ ├── MAGEDU.PPT
│ └── STANLEY.PPT
├── tasks
│ ├── file.yaml
│ └── main.yaml
mkdir -p roles/file/{files,tasks}

依次编辑 roles/file/files/MAGEDU.PPT,roles/file/files/STANLEY.PPT
roles/file/files/MAGEDU.PPT

This is magedu.ppt file.

roles/file/files/STANLEY.PPT

This is stanley.ppt file.

依次编辑role/file.yaml,roles/file/tasks/file.yaml,roles/file/tasks/main.yaml

file.yaml

---
    # 该playbook是整个项目的调度入口
  - hosts: ubuntu
    gather_facts: yes
    roles:
     - role: file

roles/file/tasks/file.yaml

---
- name: file change file
  copy: 
    src: "{{ item.src }}" 
    dest: "/opt/{{ item.dest }}" 
    owner: root 
    group: root
  with_items:
    - { src: 'MAGEDU.PPT', dest: 'magedu.ppt' }
    - { src: 'STANLEY.PPT', dest: 'stanley.ppt' }

roles/file/tasks/main.yaml

---
    - import_tasks: file.yaml
ansible-playbook file.yaml 

PLAY [ubuntu] *******

TASK [Gathering Facts] *******
ok: [192.168.255.110]

TASK [file : file change file] *******
changed: [192.168.255.110] => (item={'src': 'MAGEDU.PPT', 'dest': 'magedu.ppt'})
changed: [192.168.255.110] => (item={'src': 'STANLEY.PPT', 'dest': 'stanley.ppt'})

PLAY RECAP *******
192.168.255.110            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

role 模板替换

Jinja2是一个Python的功能齐全的模板引擎
Jinja2读取变量的方式,通常两个花括号中间加变量名且花括号和变量名间需有空格分隔,

{{variable}}

更详尽内容请参考 Jinja2官网

将order.j2分发至远程主机/data/{{PROJECT}}/目录下,并改名为order.conf,且替换配置文件中变量为对应的值

编排目录如下

template.yaml
roles/template/
     ├── tasks
     │ ├── main.yaml
     │ └── template.yaml
     ├── templates
     │ └── order.j2
     └── vars
      └── main.yaml
mkdir -p roles/template/{tasks,templates,vars}

tempates.yaml

---
    - hosts: ubuntu
      gather_facts: true
      roles:
       - role: template

roles/template/tasks/main.yaml

---
  - import_tasks: template.yaml

roles/template/tasks/template.yaml

---
  - name: tempalte transfer example
    template: 
      src: order.j2 
      dest: /data/{{ PROJECT }}/order.conf

roles/template/templates/order.j2

project: {{ PROJECT }}
switch: {{ SWITCH }}
dbport: {{ DBPORT }}

roles/template/vars/main.yaml

---
PROJECT: "JAVA"
SWITCH: "ON"
DBPORT: "3306"
ansible-playbook tempates.yaml 

PLAY [ubuntu] *****

TASK [Gathering Facts] *****
ok: [192.168.255.110]

TASK [template : tempalte transfer example] *****
changed: [192.168.255.110]

PLAY RECAP *****
192.168.255.110            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

cat /data/JAVA/order.conf 
project: JAVA
switch: ON
dbport: 3306

跨平台Roles

编辑Inventory文件/etc/ansible/hosts

cat /etc/ansible/hosts 
[ubuntu]
192.168.255.110
[centos]
192.168.255.101

针对不同的系统平台分别编辑httpd_db,httpd_rh的Role

mkdir -p roles/{httpd_db,httpd_rh}/tasks

roles/httpd_db/tasks/httpd.yaml

---
- name: ubuntu install httpd
  apt: 
    name: mini-httpd 
    state: present

roles/httpd_db/tasks/main.yaml

---
- import_tasks: httpd.yaml

roles/httpd_rh/tasks/httpd.yaml

---
- name: centos install httpd
  yum: 
    name: httpd 
    state: latest

roles/httpd_rh/tasks/main.yaml

---
- import_tasks: httpd.yaml

httpd.yaml

---
  - name: all install httpd
    hosts: all
    roles:
     - { role: httpd_db, when: ansible_os_family == 'Debian' }
     - { role: httpd_rh, when: ansible_os_family == 'RedHat' }

ansible-playbook httpd.yaml 

PLAY [all install httpd] ******

TASK [Gathering Facts] ******
ok: [192.168.255.110]
ok: [192.168.255.101]

TASK [httpd_db : ubuntu install httpd] ******
skipping: [192.168.255.101]
ok: [192.168.255.110]

TASK [httpd_rh : centos install httpd] ******
skipping: [192.168.255.110]
changed: [192.168.255.101]

PLAY RECAP ******
192.168.255.101            : ok=2    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
192.168.255.110            : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0 

标签:httpd,tasks,roles,yaml,Ansible,file,apache,变更
From: https://www.cnblogs.com/anyux/p/18372007

相关文章

  • ansible roles 示例
    目录role构建role编辑roles目录变量文件调用roletasksmain文件taskscreate_dir.yaml,git_checkout.yaml,static_git_pull.yaml文件roleAd-Hoc适用于临时命令的执行,Playbook合适中小项目,Roles适合大项目构建roleRoles主要依赖于目录的命名和摆放,默认tasks/main.yaml是......
  • 2024.8.20(playbook剧本安装nginx、roles)
    一、playbook 剧本安装nginx[root@m0~]#mkdir/etc/ansible/playbook[root@m0~]#vim/etc/ansible/playbook/nginx.yml----hosts:group02remote_user:roottasks:-name:卸载httpdyum:......
  • ansible include_tasks示例
    目录include_tasks导入任务include_tasks拉取ansible代码示例include_tasks导入任务任务文件只包含任务,作用主机范围由playbook.yaml决定include_tasks.yaml----name:restartmemcachedservice:name:memcachedstate:restartedplaybook.yaml----h......
  • STP(角色选举、状态、定时器、拓扑变更机制、PVST、PVST+增强特性)
    文章目录一、什么是STP定义特点工作原理专业术语二、STP角色选举1、配置命令:2、端口角色:三、STP的状态四、STP的定时器①HelloTime:2s②MaxAge:20s③ForwardDelay:15s④AgingTime:300s五、STP拓扑变化机制六、PVST七、PVST+增强特性......
  • ansible自动化之playbook剧本【nginx安装为例】
    一、简介roles则是ansible中,playbooks的目录组织结构。将代码或者文件进行模块化,成为roles的文件目录组织结构,易管理,易理解,代码可重用,层次清晰。二、准备目录结构创建所需目录:mkdir-proles/nginx/{files,handlers,tasks,templates,vars}创建所需文件:touchroles/......
  • ansible block模块
    目录示例:使用block、rescue和always解释:示例输出:实际应用场景:Ansible中的block功能允许你将多个任务组合在一起,作为一个整体来处理。block提供了一些额外的功能,比如rescue和always,这些可以用来处理错误和执行清理任务。以下是一个简单的block功能的示例:示例:使用blo......
  • 云计算实训31——playbook(剧本)基本应用、playbook常见语法、playbook和ansible操作
    playbook(剧本):是ansible⽤于配置,部署,和管理被控节点的剧本。⽤于ansible操作的编排。使⽤的格式为yaml格式一、YMAL格式以.yaml或.yml结尾⽂件的第⼀⾏以"---"开始,表明YMAL⽂件的开始(可选的)以#号开头为注释列表中的所有成员都开始于相同的缩进级别,并且使⽤⼀......
  • ansible相关模块
    copy模块(重点)copy模块⽤于对⽂件的远程拷⻉操作(如把本地的⽂件拷⻉到远程的机器上)https://docs.ansible.com/ansible/latest/modules/copy_module.html#copy-module在master上准备⼀个⽂件,拷⻉此⽂件到group1的所有机器上使⽤content参数直接往远程⽂件⾥写内容(会覆......
  • ansible Tags 标签
    目录Tags标签使用场景通过指定tags执行指定任务Tags标签Ansible的标签(Tags)功能可以给role,file,task,playbook,然后利用这些标签来指定要运行Playbook中的个别任务,或不执行指定的任务,并且它的语法非常简单使用场景选择性任务执行:当您不需要运行整个Playbook时,可以选择......
  • ansible 流程控制
    目录ansibleif流程控制变量控制结构过滤器注释Jinja2api示例test语句ansiblewhen流程控制简单示例多条件示例ansibleif流程控制大部分的Ansible任务,需要对用户的输入内容或任务的运行结果进行判断,这中间体现了流程控制的作用像ansible的模板文件,以.j2结尾的都是Jinja2......