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]# 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 好的,下面是一个Ansible的role示例: 文件结构: ``` my-role/ ├── README.md ├── defaults │ └── main.yml ├── handlers │ └── main.yml ├── meta │ └── main.yml ├── tasks │ ├── install.yml │ └── main.yml ├── templates │ └── config.ini.j2 └── vars └── main.yml ``` 说明: - `README.md`:该role的说明文档 - `defaults/main.yml`:该role的默认变量 - `handlers/main.yml`:该role的handlers,用于响应特定事件 - `meta/main.yml`:该role的元数据,包括该role的依赖关系、支持的操作系统等信息 - `tasks/main.yml`:该role的主任务,定义该role的主要行为 - `tasks/install.yml`:该role的安装任务,安装所需的软件包 - `templates/config.ini.j2`:该role使用的Jinja2模板文件 - `vars/main.yml`:该role的变量 其,`tasks/main.yml`可能会包含多个任务,每个任务都会执行一些操作。示例`tasks/main.yml`文件如下: ```yaml --- - include_tasks: install.yml - name: Configure application template: src: config.ini.j2 dest: /etc/my-app/config.ini notify: - restart my-app service ``` 该任务包含两个步骤,第一个步骤会执行`install.yml`文件定义的安装任务,第二个步骤会使用Jinja2模板生成`/etc/my-app/config.ini`配置文件,并在配置文件修改后触发`restart my-app service`这个handler,用于重启应用程序的服务。 ### 回答2: 以下是一个Ansible的role示例: --- - name: 安装和配置Nginx hosts: web_servers become: yes roles: - nginx --- 在上面的示例,我们定义了一个名为“安装和配置Nginx”的任务。我们指定了主机为web_servers,让Ansible在这些主机上运行任务。并且将become设置为yes,以便在主机上提升特权执行任务。 然后,我们在roles部分指定了一个名为nginx的角色Ansible将会按照自定义的角色定义来执行这个任务。 接下来,我们需要创建名为nginx的角色目录,并在目录创建以下目录结构: - roles - nginx - tasks - main.yml - templates - nginx.conf.j2 在tasks/main.yml文件,我们可以编写用于安装和配置Nginx的任务。例如: --- - name: 安装Nginx apt: name: nginx state: latest - name: 配置Nginx template: src: nginx.conf.j2 dest: /etc/nginx/nginx.conf notify: 重启Nginx服务 --- 在templates/nginx.conf.j2文件,我们可以创建Nginx的配置模板。例如: --- user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; server { listen 80; server_name example.com; location / { root /usr/share/nginx/html; index index.html; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } } --- 在这个示例,我们将安装Nginx并配置它的基本设置。然后,我们使用模板文件生成Nginx的配置文件,并将其复制到/etc/nginx/nginx.conf。最后,我们发送一个重启Nginx服务的通知,以应用新的配置。 ### 回答3: Ansible是一种自动化工具,可以帮助系统管理员和开发人员自动完成各种IT任务。其,roleAnsible的一种特定组织方式,可以将任务和配置组织成可重用的模块化组件。 下面是一个简单的Ansible role示例,用来安装和配置Nginx。 首先,在Ansible的roles目录下创建一个名为nginx的目录,在nginx目录下创建以下文件和目录结构: ``` roles/ └── nginx/ ├── tasks/ │ └── main.yml └── templates/ └── nginx.conf.j2 ``` 在`tasks/main.yml`文件,我们定义了一系列的任务,用于安装和配置Nginx: ```yaml --- - name: Install Nginx apt: name: nginx state: present - name: Copy nginx.conf template: src: nginx.conf.j2 dest: /etc/nginx/nginx.conf ``` 在`templates/nginx.conf.j2`文件,我们定义了Nginx的配置文件模板: ```nginx worker_processes auto; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } } } ``` 这个示例role包含了两个主要部分,`tasks`和`templates`。 `tasks`目录下的`main.yml`文件定义了该role的任务,使用Ansible的`apt`模块安装了Nginx,并通过`template`模块将配置文件`nginx.conf.j2`拷贝到目标服务器。 `templates`目录下的`nginx.conf.j2`文件是Nginx的配置文件模板,其使用了Jinja2语法进行变量替换和条件判断。 通过使用这个角色,可以轻松地在其它Ansible Playbook引用并配置Nginx。只需在Playbook使用`include_role`指令,并指定使用的角色即可。 以上就是一个简单的Ansible role示例,通过定义任务和模板,可以实现自动化安装和配置Nginx的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小莫细说linux

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值