栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

ansible中role角色的应用

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ansible中role角色的应用


[root@foundation50 .ansible]# vim ansible.cfg
[root@foundation50 .ansible]# mkdir /root/.ansible/roles 建立指定的目录
[root@foundation50 .ansible]# ansible-galaxy list 列出roles
[root@foundation50 .ansible]# cd /root/.ansible/roles/ 进入roles目录
[root@foundation50 roles]# ansible-galaxy init apache


[root@foundation50 apache]# cd vars/ 进入变量目录
[root@foundation50 vars]# vim main.yml 编辑文件

[root@foundation50 apache]# vim tasks/  进入tasks目录
[root@foundation50 tasks]# vim main.yml  编辑文件 ,不需要缩进
  1 ---
  2 # tasks file for apache
  3 - name: install apache
  4   dnf:
  5    name: httpd
  6    state: latest
  7 - name: config apache   都顶格写
  8   lineinfile:                                                               
  9     path: /etc/httpd/conf/httpd.conf
 10     regexp: "^Listen"
 11     line: "listen{{PORT}}"
 12   notify: restart apache
 13   changed_when: true
 14 
 15 - name: start apache  定格
 16   service:
 17     name: httpd
 18     state: started
 19     enabled: yes                                                          
 
 
 [root@foundation50 apache]# cd handlers/   进入触发器目录
[root@foundation50 handlers]# vim main.yml  编辑文件         
---
# handlers file for apache
- name: restart apache
  service:
    name: httpd
    state: restarted


[root@foundation50 ~]# cd .ansible/  
[root@foundation50 .ansible]# vim roles.yml   
---
- name: test roles
  hosts: westos
  roles: 
    - apache    如果有多个角色,依次在后面加
[root@foundation50 .ansible]# ansible-playbook  roles.yml  运行

  角色的作用:把playbook片段全部拆开,放到该方的位置,不会因为playbook过长而导致缩进出现问题 ,不需要缩进全是定格
                  





[root@foundation50 .ansible]# ansible-galaxy collection install nginxinc.nginx_core  下载
[root@foundation50 .ansible]# cd collections/  
[root@foundation50 collections]# ls
ansible_collections
[root@foundation50 collections]# cd ansible_collections/
[root@foundation50 ansible_collections]# ls
nginxinc
[root@foundation50 ansible_collections]# cd nginxinc/
[root@foundation50 nginxinc]# ls
nginx_core
[root@foundation50 nginxinc]# cd nginx_core/
[root@foundation50 nginx_core]# ls
CHANGELOG.md        docs        MANIFEST.json  plugins
CODE_OF_CONDUCT.md  FILES.json  meta           README.md
CONTRIBUTING.md     LICENSE     playbooks      roles
[root@foundation50 nginx_core]# cd roles/
[root@foundation50 roles]# ls
nginx  nginx_app_protect  nginx_config    nginx所有资源
[root@foundation50 roles]# cp -r * /root/.ansible/roles   将nginx资源复制到 /root/.ansible/roles 指定的角色目录里面
[root@foundation50 roles]# ls /root/.ansible/roles
apache  nginx  nginx_app_protect  nginx_config
[root@foundation50 roles]# ansible-galaxy list  列出角色
# /root/.ansible/roles
- apache, (unknown version)      nginx角色已经存在了
- nginx, (unknown version)
- nginx_app_protect, (unknown version)
- nginx_config, (unknown version)
# /usr/share/ansible/roles
# /etc/ansible/roles

安装自己的包

[root@foundation50 roles]# ls
apache  nginx  nginx_app_protect  nginx_config  
[root@foundation50 roles]# tar zcf apache.tar.gz apache   打包apache
[root@foundation50 roles]# rm -fr apache
[root@foundation50 roles]# ls
apache.tar.gz  nginx  nginx_app_protect  nginx_config 
[root@foundation50 ~]# cd .ansible/
[root@foundation50 .ansible]# ansible-galaxy list  没有apache角色
# /root/.ansible/roles
- nginx, (unknown version)
- nginx_app_protect, (unknown version)
- nginx_config, (unknown version)
[root@foundation50 .ansible]# mv apache.yml /mnt/   移动到/mnt里
[root@foundation50 .ansible]# vim install_role.yml  
---
- src: file:///mnt/apache.tar.gz    压缩包的位置  ,此处源也可以是互联网地址
  name: westos   安装解压后的名字
[root@foundation50 .ansible]# ansible-galaxy install -r install_role.yml   安装
- downloading role from file:///mnt/apache.tar.gz
- extracting westos to /root/.ansible/roles/westos
- westos was installed successfully
[root@foundation50 roles]# ls
nginx  nginx_app_protect  nginx_config  westos  角色westos已经下载成功
[root@foundation50 roles]# 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/613615.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号