Linux架構31 ansible roles角色, ansible galaxy

战斗小人發表於2024-04-19

Ansible Roles角色

1.Ansible Roles基本概述

Roles基於一個已知的檔案結構,去自動地載入某些 var_files, tasks 以及 handlers。

Ansible注意事項: 在編寫roles的時候,最好能將一個task拆分為一個檔案,方便後續複用。(徹底的打散)

2.Ansible Roles目錄結構

roles官方目錄結構,必須按如下定義。在每個目錄中必須有main.yml檔案,這些屬於強制要求。

[root@m01 ~]# cd /etc/ansible/roles
[root@m01 roles]# 
[root@m01 roles]# tree
.
|—— nfs
|   |—— files        #存放檔案,copy呼叫
|   |—— handers        #觸發檔案
|   |—— tasks        #具體任務
|   |—— templates    #模板檔案,template呼叫
|   |—— vars        #定義變數
|   |—— meta        #依賴關係

3.Ansible Roles案例實戰

Roles小技巧:

1.建立roles目錄結構,手動或使用ansible-galaxy init 角色名稱

2.編寫roles的功能,也就是tasks。

3.最後playbook引用roles編寫好的tasks。

#1.先建立一個專案目錄
[root@m01 ~]# mkdir project2
[root@m01 ~]# cd project2/

#初始化一個test的role
# [root@m01 project2]# ansible-galaxy init test    #初始化比較亂,不推薦這種方法。直接自己創
# [root@m01 project2]# rm -rf test

#手動初始化一個memcached的role,資料夾名就是role名
[root@m01 project2]# mkdir memcached/{tasks,handlers,templates,vars,files} -pv
mkdir: created directory ‘memcached’
mkdir: created directory ‘memcached/tasks’
mkdir: created directory ‘memcached/handlers’
mkdir: created directory ‘memcached/templates’
mkdir: created directory ‘memcached/vars’
mkdir: created directory ‘memcached/files’

#2.編寫roles的功能
[root@m01 project2]# vim memcached/tasks/main.yml
- name: Install Memcached Server
  yum:
    name: memcached
    state: present

- name: Configure Memcached Server
  template:
    src: memcached.j2    # 自動在templates中找該檔案
    dest: /etc/sysconfig/memcached
    notify: Restart Memcached Server    #handlers不能寫在這個yml下

- name: Started Memcached Server
  service:
    name: memcached
    state: started
    enabled: yes

[root@m01 project2]# vim /root/project2/memcached/templates/memcached.j2
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="{{ ansible_memtotal_mb // 2 }}"
OPTIONS=""

[root@m01 project2]# vim memcached/handlers/main.yml
- name: Restart Memcached Server
  service:
    name: memcached
    state: restarted
    
#3.編寫playbook引用roles編寫好的tasks
[root@m01 project2]# vim site.yml    #這個名字隨意
- hosts: web01
  roles:
    - memcached    #這個就是role的名字
    
#4.執行
[root@m01 project2]# ansible-playbook site.yml
#也可以把hosts檔案放在該目錄下,指定hosts執行
[root@m01 project2]# ansible-playbook -i hosts site.yml

------------------------------------------------
###第二種寫法,打散
[root@m01 tasks]# vim install.yml
- name: Install Memcached Server
  yum:
    name: memcached
    state: present
    
[root@m01 tasks]# vim config.yml
- name: Configure Memcached Server
  template:
    src: memcached.j2    # 自動在templates中找該檔案
    dest: /etc/sysconfig/memcached
    notify: Restart Memcached Server    #handlers不能寫在這個yml下
    
[root@m01 tasks]# vim start.yml
- name: Started Memcached Server
  service:
    name: memcached
    state: started
    enabled: yes
    
[root@m01 tasks]# vim main.yml
- include_tasks: install.yml
- include_tasks: config.yml
- include_tasks: start.yml

[root@m01 project2]# tree memcached/
memcached/
├── files
├── handlers
│   └── main.yml
├── tasks
│   ├── config.yml
│   ├── install.yml
│   ├── main.yml
│   └── start.yml
├── templates
│   └── memcached.j2
└── vars

###執行
[root@m01 project2]# ansible-playbook site.yml
------------------------------------------------------

#1.再建立nginx和php的role
[root@m01 project2]# mkdir {nginx,php-fpm}/{tasks,handlers,templates} -pv

#2.寫功能
[root@m01 project2]# vim nginx/tasks/main.yml
- name: Installed Nginx Server
  yum: name=nginx state=present
  
- name: Configure Nginx Server
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify: Restart Nginx Server
  
- name: Started Nginx Server
  service: name=nginx state=started enabled=yes
  
[root@m01 project2]# vim nginx/handlers/main.yml
- name: Restart Nginx Server
  service: name=nginx state=restarted
  
[root@m01 project2]# vim nginx/templates/nginx.conf.j2
...

#3.編寫playbook引用roles編寫好的tasks
[root@m01 project2]# vim site.yml    #這個名字隨意
- hosts: web01
  roles:
    - memcached
    - nginx        #對應role的名字
    
#可以透過設定tag,來指定執行哪個role
[root@m01 project2]# vim site.yml
- hosts: web01
  roles:
    - role: memcached
      tages: memcached
    - role: nginx
      tags: nginx
[root@m01 project2]# ansible-playbook site.yml -t nginx     #只跑nginx的role

#4.寫php的task
[root@m01 project2]# vim php-fpm/tasks/main.yml
- name: Installed PHP-FPM Server
  yum: name={{ packages }} state=present
  vars:
    packages:    #下面只寫部分,實際使用還需要其他的
      - php
      - php-cli
      - php-fpm
      - php-pdo
      - php-gd
      - php-mbstring

- name: Configure PHP-FPM Server
  template: src={{ item.src }} dest={{ item.dest }}
  with_items:
    - { src: 'php.ini.j2' ,dest: '/etc/php.ini' }
    - { src: 'php_www.conf.j2' ,dest: '/etc/php-fpm.d/www.conf' }
  notify: Restart PHP-FPM Server
    
- name: Started PHP-FPM Server
  service: name=php-fpm state=started enabled=yes
  
[root@m01 project2]# vim php-fpm/handlers/main.yml
- name: Restart PHP-FPM Server
  service: name=php-fpm state=restarted
  
#5.編寫playbook引用
[root@m01 project2]# vim site.yml    #這個名字隨意
- hosts: web01
  roles:
    - role: memcached
    - role: nginx
    - role: php-fpm

Ansible Galaxy

Galaxy是一個免費網站,類似github網站,網站上基本都是共享的roles角色。從Galaxy下載roles角色是快速啟動自動化專案方式之一。

Galaxy 官網:https://galaxy.ansible.com

搜需要的role

點選複製,執行命令進行下載role

# galaxy下載到預設路徑: /root/.ansible/roles
[root@m01 project2]# ansible-galaxy install geerlingguy.nginx

#寫一個site.yml呼叫執行
- hosts: web01
  roles:
    - role: nginx

相關文章