Ansible角色roles

Ansible角色roles

ansible角色roles

官方文档:https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html

1 角色目录规划

官方的目录结构,必须这样定义!
1.目录说明:
[root@m01 ~]# cd /etc/ansible/roles/
[root@m01 /etc/ansible/roles]# tree
.
├── nfs                   #角色名称
│   ├── files             #存放需要copy的文件,主要是压缩包
│   ├── handlers          #触发任务剧本
│   ├── tasks             #具体任务剧本
│   ├── templates         #模版文件,主要放服务的配置文件
│   └── vars              #存放变量文件

2 使用ansible角色编写rsyncd

1.创建角色目录

cd /etc/ansible/roles/
mkdir -p rsyncd/{files,tasks,handlers,vars,templates}

2.将配置文件拷贝到files目录

[root@m01 /etc/ansible/roles]# cp /server/conf/rsync* rsyncd/files/
[root@m01 /etc/ansible/roles]# ll rsyncd/files/
67961480 -rw-r--r-- 1 root root 300 2020-01-13 19:57:02 rsyncd.conf
67961487 -rw-r--r-- 1 root root  20 2020-01-13 19:57:02 rsync.passwd

3.将主任务文件rsync_server.yaml拷贝到主任务目录tasks并重命名为main.yaml,并修改

[root@m01 /etc/ansible/roles]# cp /server/scripts/rsync_server.yaml rsyncd/tasks/main.yml
[root@m01 /etc/ansible/roles/rsyncd/tasks]# cat main.yml 
- name: 01-install rsyncd
  yum:
    name: rsync
    state: latest

- name: 02-copy rsyncd conf and passwd
  copy:
    src: "{{ item.src }}"
    dest: /etc
    mode: "{{ item.mode }}"
  loop:
    - { src: 'rsyncd.conf', mode: '0644' }
    - { src: 'rsync.passwd', mode: '0600' }
  notify: Restart_Rsyncd

- name: 03-create rsyncd running group www
  group:
    name: www
    gid: 666

- name: 04-add rsyncd running user www
  user:
    name: www
    group: www
    shell: /sbin/nologin
    create_home: no

- name: 05-create directory /backup /data
  file:
    path: "{{ item.path }}"
    state: directory
    owner: "{{ item.owner }}"
    group: "{{ item.group }}"
  loop:
    - { path: '/backup', owner: 'www',  group: 'www' }
    - { path: '/data',   owner: 'root', group: 'root' }

- name: 06-start rsyncd server
  service:
    name: rsyncd
    state: started
    enabled: yes

4.将主配置文件中的handlers写入到handlers目录中的main.yml文件中

[root@m01 /etc/ansible/roles/rsyncd/handlers]# cat main.yml 
- name: Restart_Rsyncd
  service:
    name: rsyncd
    state: restarted

5.编写启动文件

[root@m01 /etc/ansible/roles]# cat rsyncd.yml 
- hosts: backup
  roles:
    - rsyncd

6.查看最终目录

[root@m01 /etc/ansible/roles]# tree rsyncd
rsyncd
├── files/
│   ├── rsyncd.conf
│   └── rsync.passwd
├── handlers/
│   └── main.yml
├── tasks/
│   └── main.yml
├── templates/
└── vars/

3 使用ansible的jinjia模板编写sshd

01.创建角色目录

cd /etc/ansible/roles/
mkdir -p sshd/{files,tasks,handlers,vars,templates}

02.编写主任务剧本

[root@m01 /etc/ansible/roles]# cat sshd/tasks/main.yml 
- name: 01-config sshd
  template:
    src: sshd_config.j2
    dest: /etc/ssh/sshd_config
  notify: Restart_SSHD

03.编写jinjia模板

jinjia模板注意事项:
	1-模板文件以.j2结尾
	2-.j2模板文件要放在templates目录下
	3-任务文件里面必须使用template模块指定模板
[root@m01 /etc/ansible/roles]# cat sshd/templates/sshd_config.j2 
...
Port {{ ssh_port }}
ListenAddress {{ ansible_facts.eth1.ipv4.address }}
...

04.编写变量

[root@m01 /etc/ansible/roles]# cat sshd/vars/main.yml 
ssh_port: 8888

05.编写handlers文件

[root@m01 /etc/ansible/roles]# cat sshd/handlers/main.yml 
- name: Restart_SSHD
  service:
    name: sshd
    state: restarted

06.编写主调用文件

[root@m01 /etc/ansible/roles]# cat sshd.yml 
- hosts: web
  roles:
    - sshd

07.查看最终目录

[root@m01 /etc/ansible/roles]# tree sshd
sshd
├── files/
├── handlers/
│   └── main.yml
├── tasks/
│   └── main.yml
├── templates/
│   └── sshd_config.j2
└── vars/
    └── main.yml

4 拆分init角色

[root@m01 /etc/ansible/roles]# tree
.
├── auto_deploy.yml
├── init/
│   ├── files/
│   ├── handlers/
│   ├── tasks/
│   │   ├── main.yml
│   ├── templates/
│   └── vars/
├── lsyncd/
│   ├── files/
│   ├── handlers/
│   │   └── main.yml
│   ├── tasks/
│   │   └── main.yml
│   ├── templates/
│   │   ├── lsyncd.conf.j2
│   │   └── rsync.passwd.j2
│   └── vars/
├── nfs/
│   ├── files/
│   ├── handlers/
│   │   └── main.yml
│   ├── tasks/
│   │   └── main.yml
│   ├── templates/
│   │   └── exports.j2
│   └── vars/
├── rpcbind_mounted_for_nfs_client/
│   ├── files/
│   ├── handlers/
│   ├── tasks/
│   │   └── main.yml
│   ├── templates/
│   └── vars/
├── rsyncd/
│   ├── files/
│   ├── handlers/
│   │   └── main.yml
│   ├── tasks/
│   │   └── main.yml
│   ├── templates/
│   │   ├── rsyncd.conf.j2
│   │   └── rsync.passwd.j2
│   └── vars/
└── sshd/
    ├── files/
    ├── handlers/
    │   └── main.yml
    ├── tasks/
    │   └── main.yml
    ├── templates/
    │   └── sshd_config.j2
    └── vars/
        └── main.yml

[root@m01 /etc/ansible/roles]# cat auto_deploy.yml 
- hosts: web
  roles:
    - init

- hosts: nfs
  roles:
    - nfs

- hosts: backup
  roles: 
    - rsyncd
    - rpcbind_mounted_for_nfs_client

- hosts: nfs
  roles:
    - lsyncd