ansible-play中role的基本用法


#role應用
#roles跟調用角色的劇本文件應該與roles同級關系,即放在ansible目錄下
#makir /root/ansible/roles/{nginx,http,ftp,mysql,redis}
palybook.yml
roles/
    project/
        tasks/   定義task,role的基本元素,至少包含一個main.yml文件
        files/   存放由copy或script模塊等調用的文件
        vars/     定義變量文件
        templates/  template模塊查找所需要末班文件的目錄
        handlers/
        default/  設定默認變量
#以nginx為例
思路:
1.group:創建用戶組nginx
2.user:創建用戶nginx
3.yum:安裝nginx
4.template:配置文件更新nginx.conf
5.service:啟動nginx
####################################################################
cd /root/ansible/roles/nginx
mkdir tasks templates
cd task

touch group.yml
- name: create group nginx
    group: name=nginx gid=80

touch user.yml
-name: create user nginx
    user: name=nginx uid=80 group=nginx system=yes shell=/sbi/nologin
 
touch install.yml
- name: install package
    yum: name=nginx
   
touch start.yml
- name: start service
    service: name=nginx state=started enabled=yes
    
touch restart.yml
- name: restart service
    service: name=nginx state=restarted
    
touch templ.yml
- name: copy conf
    template: src=nginx.conf.j2 dest=/etc/nginx/conf/nginx.conf
    
touch main.yml
- include: group.yml
- include: user.yml
- include: install.yml
- include: templ.yml
- include: start.yml
      
cd ../templates  && ll
nginx.conf.j2

cd /root/ansible
touch nginx_role.yml
- hosts: websrvs
  remote_user: root
  roles:
    - role: nginx

執行命令:ansible-playbook nginx_role.yml

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM