本章主要介绍ansible中角色的使用
????????正常情况下,配置一个服务如 apache时,要做一系列的操作:安装、拷贝、启动服务等。如果要在不同的机器上重复配置此服务,需要重新执行这些操作。
[blab@node01 ~]$ mkdir demo5
[blab@node01 ~]$ cp ansible.cfg hosts demo5/
[blab@node01 ~]$ cd demo5/
[blab@node01 demo5]$
[blab@node01 demo5]$ cat ansible.cfg
[defaults]
inventory = ./hosts
command_warnings=False //增加内容
roles_path = ./roles //增加内容
[privilege_escalation]
become=True
become_method=sudo
become_user=root
[blab@node01 demo5]$
[blab@node01 demo5]$ ansible-galaxy init roles/apache
- Role roles/apache was created successfully
[blab@node01 demo5]$ ls roles/
apache
[blab@node01 demo5]$
[blab@node01 demo5]$ ls roles/apache/
defaults files handlers meta README.md tasks templates tests vars
[blab@node01 demo5]$
[blab@node01 demo5]$ cat /home/blab/demo4/hand-1.yml
---
- hosts: node03
vars:
myport: 80
tasks:
- name: task1安装http
yum: name=httpd state=installed
- name: tasks2拷贝配置文件
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
- name: task3启动httpd服务
service: name=httpd state=started
handlers:
- name: restart httpd1
service: name=httpd state=restarted
- name: restart httpd2
service: name=httpd state=restarted
[blab@node01 demo5]$
[blab@node01 demo5]$ cat roles/apache/tasks/main.yml
---
- name: task1 安装httpd
yum: name=httpd state=installed
- name: task2 拷贝配置文件
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify: restart httpd1
- name: task3 启动httpd 服务
service: name=httpd state=started
[blab@node01 demo5]$
[blab@node01 demo5]$ cat roles/apache/handlers/main.yml
---
- name: restart httpd1
service: name=httpd state=restarted
- name: restart httpd2
service: name=httpd state=restarted
[blab@node01 demo5]$
[blab@node01 demo5]$ cp ../demo4/httpd.conf.j2 roles/apache/templates/
[blab@node01 demo5]$ ls roles/apache/templates/
httpd.conf.j2
[blab@node01 demo5]$
[blab@node01 demo5]$ cat roles/apache/vars/main.yml
---
myport: 8080
[blab@node01 demo5]$
[blab@node01 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
[blab@node01 demo5]$
roles:
‐ name: 名称1
role: rolesname1
‐ name: 名称2
role: rolesname2
roles:
‐ role: rolesname1
‐ role: rolesname2
[blab@node01 demo5]$ cat test-roles1.yml
---
- hosts: node02
roles:
- role: apache
[blab@node01 demo5]$
[blab@node01 demo5]$ ansible-playbook test-roles1.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [apache : task1 安装httpd] **************************************************
changed: [node02]
TASK [apache : task2 拷贝配置文件] ***************************************************
changed: [node02]
TASK [apache : task3 启动httpd 服务] ***********************************************
changed: [node02]
RUNNING HANDLER [apache : restart httpd1] **************************************
changed: [node02]
PLAY RECAP *********************************************************************
node02 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo5]$
[blab@node01 demo5]$ cat roles/apache/vars/main.yml
---
myport: 8080
[blab@node01 demo5]$
[blab@node01 demo5]$ cat test-roles1.yml
---
- hosts: node02
vars:
myport: 808
roles:
- role: apache
[blab@node01 demo5]$
[blab@node01 demo5]$ ansible-playbook test-roles1.yml
PLAY [node02] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [apache : task1 安装httpd] **************************************************
ok: [node02]
TASK [apache : task2 拷贝配置文件] ***************************************************
ok: [node02]
TASK [apache : task3 启动httpd 服务] ***********************************************
ok: [node02]
PLAY RECAP *********************************************************************
node02 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo5]$
[root@node02 ~]# netstat -napt | grep httpd
tcp6 0 0 :::8080 :::* LISTEN 6297/httpd
[root@node02 ~]#
[root@node01 ~]# yum -y install rhel-system-roles.noarch //root用户安装
[root@node01 ~]# su - blab //切换用户
[blab@node01 ~]$ cp -r /usr/share/ansible/roles/rhel-system-roles.selinux/ /home/blab/demo5/
[blab@node01 ~]$ cd demo5
[blab@node01 demo5]$ ls
ansible.cfg hosts rhel-system-roles.selinux roles test-roles1.yml
[blab@node01 demo5]$
[root@node02 ~]# setenforce 0 //临时关闭
[root@node02 ~]# getenforce //查看是否关闭
Permissive
[root@node02 ~]#
[blab@node01 demo5]$ cat 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
[blab@node01 demo5]$
[blab@node01 demo5]$ cat test-role2.yml
---
- hosts: node02
vars:
selinux_state: enforcing
roles:
- role: rhel-system-roles.selinux
[blab@node01 demo5]$
PLAY RECAP *********************************************************************
node02 : ok=7 changed=1 unreachable=0 failed=0 skipped=16 rescued=0 ignored=0
[blab@node01 demo5]$
[root@node02 ~]# getenforce
Enforcing
[root@node02 ~]#
[blab@node01 demo5]$ cat 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
[blab@node01 demo5]$
[blab@node01 demo5]$ vim rhel-system-roles.selinux/tasks/main.yml
114 - name: Set an SELinux label on a port
115 seport:
116 ports: "{{ item.ports }}"
117 proto: "{{ item.proto | default('tcp') }}"
118 setype: "{{ item.setype }}"
119 state: "{{ item.state | default('present') }}"
120 with_items: "{{ selinux_ports }}"
[blab@node01 demo5]$ cat test-roles1.yml
---
- hosts: node02
vars:
myport: 808
selinux_ports:
- ports: "{{myport}}"
setype: http_port_t
roles:
- role: rhel-system-roles.selinux
- role: apache
[blab@node01 demo5]$
[blab@node01 demo5]$ cat roles/apache/vars/main.yml
---
#myport: 8080
[blab@node01 demo5]$
[blab@node01 demo5]$ ansible-playbook test-roles1.yml
PLAY RECAP *********************************************************************
node02 : ok=10 changed=1 unreachable=0 failed=0 skipped=16 rescued=0 ignored=0
[blab@node01 demo5]$
[root@node02 ~]# netstat -napt | grep httpd
tcp6 0 0 :::808 :::* LISTEN 13421/httpd
[root@node02 ~]#