1,ansible角色
ansible官方指定的一套统一规范规则叫角色比如我们可以把配置nginx过程抽象成一个nginx角色
同理像redis也是一样可以理解为一个角色,它的配置过程有ansible固定模板
[[email protected] ~]# cd /etc/ansible/roles/
[[email protected] /etc/ansible/roles]# tree
.
├── rsync #角色名称
│ ├── files #存放需要copy的文件
│ ├── handlers #触发任务剧本
│ ├── tasks #具体任务剧本
│ ├── templates #模版文件
│ └── vars #存放变量文件
2,ansible角色目录模板(以配置rsync服务为例)
mkdir -p /etc/ansible/roles/rsync (创建角色目录,做到统一管理)
mkdir {files,handlers,tasks,templates,vars} (规定死的目录)
1)cd rsync
vi tasks/main.yml
- name: 01-add-group
group:
name: www
gid: 666
tags: 01-add-group
- name: 02-add-user
user:
name: www
create_home: no
shell: /sbin/nologin
uid: 666
group: www
tags: 02-add-user
- name: 03-install rsync
yum:
name: rsync
state: installed
tags: 03-install rsync
- name: 04-copy rsyncd.conf
copy:
src: rsyncd.conf
dest: /etc/
notify:
- restarted rsyncd
tags: 04-copy rsyncd.conf
- name: 05-create rsync.passwd
copy:
src: rsync.passwd
dest: /etc/
mode: 600
tags: 05-create rsync.passwd
- name: 06-create backup and data directory
file:
path: "{{ item }}"
state: directory
owner: www
group: www
loop:
- "{{ path_backup }}"
- "{{ path_data }}"
tags: 06-create backup and data directory
- name: 08-start rsyncd
service:
name: rsyncd
state: started
tags: 08-start rsyncd
- name: 09-enbaled rsyncd
systemd:
name: rsyncd
enabled: yes
tags: 09-enbaled rsyncd
2)cd files (配置文件统一归到files目录)
3)vi vars/main.yml (放定义的变量)
path_backup: /backup
path_data: /data
4)vi handlers/main.yml (notify的触发重启机制)
- name: restart rsyncd
service:
name: rsyncd
state: restarted
5)创建一个开关文件
6)执行
ansible-playbook -C rsync_install.yml
ansible-playbook rsync_install.yml
7,ansible角色templates功能调用
templates模块的功能和files模块的功能类似。使用场景是不一样的:files模块里的配置文件直接批量复制到其他主机就行
它里面的配置参数你写好了就不用变,像rsync和nfs配置文件,但像SSH优化配置文件,里面的设置的登录ip是要跟相应主机ip变化而变化。这个时候templates模块就简单很多
1)这里我们创建一个init角色,这是一个初始化角色,就是将我们主机需要的所有基础配置统一用这个角色去做好
mkdir -p /etc/ansible/roles/init
mkdir {files,handlers,tasks,templates,vars}
2)vi tasks/main.yml
#01.配置base源
- name: 01_configure_yum_repos
yum_repository:
name: base
description: base yum repo
baseurl:
- http://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/
gpgcheck: no
#02.配置epel源
- name: 02_configure_yum_Repos
yum_repository:
name: epel
description: epel yum repo
baseurl:
- https://mirrors.tuna.tsinghua.edu.cn/epel/7/$basearch
gpgcheck: no
#03.安装常用软件
- name: 03_install_server
yum:
name: "{{ packages }}"
vars:
packages:
- ntpdate
- lsof
- tree
- iftop
- iotop
#04.创建用户组
- name: 04_create_group
group:
name: www
gid: 666
#05.创建用户
- name: 05_create_user
user:
name: www
uid: 666
group: www
shell: /sbin/nologin
create_home: no
#06.创建数据目录和脚本目录
- name: 06_create_dir
file:
path: "{{ item }}"
state: directory
mode: ‘0755‘
loop:
- /data
- /server/scripts
#07.创建同步时间定时任务
- name: 07_cron_ntpdate
cron:
name: Time_Update
minute: "*/5"
job: ‘/sbin/ntpdate time1.aliyun.com‘
#08.拷贝优化后的ssh配置文件
- name: 08_copy_ssh
template:
src: sshd_config.j2
dest: /etc/ssh/sshd_config
mode: ‘0600‘
backup: yes
notify: restart sshd
3)cp /etc/ssh/sshd_config templates/sshd_config.j2(这里配置文件就不用写在files模块下了)优化参数
#Port 22
#AddressFamily any
ListenAddress {{ ansible_facts.eth0.ipv4.address }}
#ListenAddress ::
原文地址:https://blog.51cto.com/13858002/2433669