[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]#
|