roles小技巧
1.创建roles目录结构,手动使用ansible-galaxy init basic
2.编写roles功能
3.在playbook中引用
[root@m01 basic]# mkdir ansible
[root@m01 basic]# cd ansible
[root@m01 basic]# ansible-galaxy init basic
[root@m01 ansible]# ll basic/
total 0
drwxr-xr-x 2 root root 6 Jul 10 2022 files
drwxr-xr-x 2 root root 6 Jul 10 2022 handlers
drwxr-xr-x 2 root root 22 Jul 12 2022 tasks
drwxr-xr-x 2 root root 6 Jul 10 2022 templates
drwxr-xr-x 2 root root 6 Jul 10 2022 vars
[root@m01 ansible]# cd basic/
[root@m01 basic]# vim tasks/main.yml
- name: create group www
group:
name: www
gid: 666
- name: create user www
user:
name: www
uid: 666
group: www
shell: /sbin/nologin
create_home: no
- name: install package
yum:
name: "{{ item }}"
state: present
loop:
- vim
- wget
- lrzsz
- tree
- net-tools
- name: yum repo
yum_repository:
name: nginx
description: nginx yum repo
baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
enabled: yes
gpgcheck: no
[root@m01 ansible]# vim site.yml
- hosts: all
roles:
- role: basic
when: ansible_hostname == "web01"
[root@m01 ansible]# ansible-playbook --syntax-check site.yml
[root@m01 ansible]# ansible-playbook site.yml