[root@pp ~]# mkdir demo5
[root@pp ~]# cp ansible.cfg hosts demo5/
[root@pp ~]# cd demo5/
[root@pp demo5]#
[root@pp demo5]# cat ansible.cfg
[defaults]
inventory = ./hosts
command_warnings=False
roles_path = ./roles
[privilege_escalation]
become=True
become_merhod=sudo
become_user=root
[root@pp demo5]#
[root@pp demo5]# ansible-galaxy init roles/apache
- Role roles/apache was created successfully
[root@pp demo5]# ls roles/
apache
[root@pp demo5]#
[root@pp demo5]# ls roles/apache/
defaults files handlers meta README.md tasks templates tests vars
[root@pp demo5]#
[root@pp demo5]# cat ../demo4/hand-1.yaml
---
- hosts: up
vars:
myport: 808
tasks:
- name: tasks1安装httpd
yum: name=httpd state=installed
- name: tasks拷贝备注文件
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
- name: tasks3启动httpd服务
service: name=httpd state=started
[root@pp demo5]#
[root@pp demo5]# cat roles/apache/tasks/main.yml
---
- name: tasks1安装httpd
yum: name=httpd state=installed
- name: tasks拷贝备注文件
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify: restart httpd1
- name: tasks3启动httpd服务
service: name=httpd state=started
[root@pp demo5]# cat roles/apache/handlers/main.yml
---
- name: restart httpd1
service: name=httpd state=restarted
- name: restart httpd2
service: name=httpd state=restarted
[root@pp demo5]#
[root@pp demo5]# cp ../demo4/httpd.conf.j2 roles/apache/templates/
[root@pp demo5]# ls roles/apache/templates/
httpd.conf.j2
[root@pp demo5]#
[root@pp demo5]# cat roles/apache/vars/main.yml
---
myport: 8080
[root@pp demo5]#
[root@pp demo5]# tree roles/apache/
roles/apache/
├── defaults
│?? └── main.yml
├── files
├── handlers
│?? └── main.yml
├── meta
│?? └── main.yml
├── README.md
├── tasks
│?? └── main.yml
├── templates
│?? └── httpd.conf.j2
├── tests
│?? ├── inventory
│?? └── test.yml
└── vars
└── main.yml
8 directories, 9 files
[root@pp demo5]#
1 roles:
2 ‐ name: 名称1
3 role: rolesname1
4 ‐ name: 名称2
5 role: rolesname2
或
1 roles:
2 ‐ role: rolesname1
3 ‐ role: rolesname2
[root@pp demo5]# cat test-role1.yaml
---
- hosts: up
roles:
- role: apache
[root@pp demo5]#
[root@pp demo5]# ansible-playbook test-role1.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [apache : tasks1安装httpd] ************************************************************
ok: [up]
TASK [apache : tasks拷贝配置文件] **************************************************************
changed: [up]
TASK [apache : tasks3启动httpd服务] **********************************************************
changed: [up]
RUNNING HANDLER [apache : restart httpd1] ************************************************
changed: [up]
PLAY RECAP *******************************************************************************
up : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo5]#
[root@pp demo5]# cat roles/apache/vars/main.yml
---
myport: 8080
[root@pp demo5]#
[root@pp demo5]# cat test-role1.yaml
---
- hosts: up
vars:
myport: 8080
roles:
- role: apache
[root@pp demo5]#
[root@pp demo5]# ansible-playbook test-role1.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [apache : tasks1安装httpd] ************************************************************
ok: [up]
TASK [apache : tasks拷贝配置文件] **************************************************************
ok: [up]
TASK [apache : tasks3启动httpd服务] **********************************************************
ok: [up]
PLAY RECAP *******************************************************************************
up : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo5]#
[root@up ~]# netstat -anpt | grep httpd
tcp6 0 0 :::8080 :::* LISTEN 4306/http
[root@up ~]#
[root@pp demo5]# yum -y install rhel-system-roles.noarch
正在更新 Subscription Management 软件仓库。
无法读取客户身份
本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。
上次元数据过期检查:0:54:37 前,执行于 2023年12月27日 星期三 10时13分05秒。
依赖关系解决。
==========================================================================================
软件包 架构 版本 仓库 大小
==========================================================================================
安装:
rhel-system-roles noarch 1.7.3-2.el8 aa 1.3 M
安装依赖关系:
python3-jmespath noarch 0.9.0-11.el8 aa 45 k
python3-netaddr noarch 0.7.19-8.el8 aa 1.5 M
事务概要
==========================================================================================
安装 3 软件包
总计:2.9 M
安装大小:16 M
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
准备中 : 1/1
安装 : python3-netaddr-0.7.19-8.el8.noarch 1/3
安装 : python3-jmespath-0.9.0-11.el8.noarch 2/3
安装 : rhel-system-roles-1.7.3-2.el8.noarch 3/3
运行脚本: rhel-system-roles-1.7.3-2.el8.noarch 3/3
验证 : python3-jmespath-0.9.0-11.el8.noarch 1/3
验证 : python3-netaddr-0.7.19-8.el8.noarch 2/3
验证 : rhel-system-roles-1.7.3-2.el8.noarch 3/3
已更新安装的产品。
已安装:
python3-jmespath-0.9.0-11.el8.noarch python3-netaddr-0.7.19-8.el8.noarch
rhel-system-roles-1.7.3-2.el8.noarch
[root@pp demo5]# ls /usr/share/ansible/roles/
linux-system-roles.certificate rhel-system-roles.certificate
linux-system-roles.crypto_policies rhel-system-roles.crypto_policies
linux-system-roles.ha_cluster rhel-system-roles.ha_cluster
linux-system-roles.kdump rhel-system-roles.kdump
linux-system-roles.kernel_settings rhel-system-roles.kernel_settings
linux-system-roles.logging rhel-system-roles.logging
linux-system-roles.metrics rhel-system-roles.metrics
linux-system-roles.nbde_client rhel-system-roles.nbde_client
linux-system-roles.nbde_server rhel-system-roles.nbde_server
linux-system-roles.network rhel-system-roles.network
linux-system-roles.postfix rhel-system-roles.postfix
linux-system-roles.selinux rhel-system-roles.selinux
linux-system-roles.ssh rhel-system-roles.ssh
linux-system-roles.sshd rhel-system-roles.sshd
linux-system-roles.storage rhel-system-roles.storage
linux-system-roles.timesync rhel-system-roles.timesync
linux-system-roles.tlog rhel-system-roles.tlog
linux-system-roles.vpn rhel-system-roles.vpn
[root@pp demo5]#
[root@pp demo5]# cp -r /usr/share/ansible/roles/rhel-system-roles.selinux/ roles/
[root@pp demo5]# ls roles/
apache rhel-system-roles.selinux
[root@pp demo5]#
[root@pp demo5]# getenforce
Permissive
[root@pp demo5]#
[root@pp demo5]# cat roles/rhel-system-roles.selinux/defaults/main.yml
---
selinux_state: null
selinux_policy: null
# Set up empty lists for SELinux changes.
selinux_booleans: []
selinux_fcontexts: []
selinux_logins: []
selinux_ports: []
selinux_restore_dirs: []
# Purging local modifications is disabled by default.
selinux_all_purge: no
selinux_booleans_purge: no
selinux_fcontexts_purge: no
selinux_ports_purge: no
selinux_logins_purge: no
[root@pp demo5]# cat test-role2.yaml
---
- hosts: up
vars:
selinux_state: enforcing
roles:
- role: rhel-system-roles.selinux
[root@pp demo5]#
[root@pp demo5]# ansible-playbook test-role2.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [rhel-system-roles.selinux : Install SELinux py
[root@up ~]# getenforce
Enforcing
[root@up ~]#
[root@pp demo5]# cat roles/rhel-system-roles.selinux/defaults/main.yml
---
selinux_state: null
selinux_policy: null
# Set up empty lists for SELinux changes.
selinux_booleans: []
selinux_fcontexts: []
selinux_logins: []
selinux_ports: []
selinux_restore_dirs: []
# Purging local modifications is disabled by default.
selinux_all_purge: no
selinux_booleans_purge: no
selinux_fcontexts_purge: no
selinux_ports_purge: no
selinux_logins_purge: no
[root@pp demo5]#
[root@pp demo5]# vim roles/rhel-system-roles.selinux/tasks/main.yml
- name: Set an SELinux label on a port
seport:
ports: "{{ item.ports }}"
proto: "{{ item.proto | default('tcp') }}"
setype: "{{ item.setype }}"
state: "{{ item.state | default('present') }}"
with_items: "{{ selinux_ports }}"
[root@pp demo5]# cat test-role1.yaml
---
- hosts: up
vars:
myport: 8080
selinux_ports:
- ports: "{{myport}}"
setype: http_port_t
roles:
- role: rhel-system-roles.selinux
- role: apache
[root@pp demo5]#
[root@pp demo5]# ansible-playbook test-role1.yaml
[root@up ~]# netstat -ntulp | grep httpd
tcp6 0 0 :::8080 :::* LISTEN 4306/http
[root@up ~]#