annan 2020-04-21
1.不太灵活,臃肿 2.全部写在一起,修改不方便 3.配置文件随便放,不标准
1.把剧本 拆分 拆分 拆分 2.解耦,结构更清晰,调试更方便
1.初级阶段,不要直接写角色,先写好剧本,然后再拆分 2.一开始不要想一步到位,不用拆的很细,尤其是变量
https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html
注意!这里的目录结构必须按照官方定义的要求来做!不是自己随便乱起!
tasks #存放主任务执行文件 handlers #存放handlers文件 files #存放需要发送的文件或压缩包 templates #存放jinja模版配置文件 vars #存放变量文件
1.先写好剧本 2.创建角色目录 3.拷贝需要发送的文件到指定目录 4.拆分剧本
- hosts: backup vars: user_id: ‘666‘ rsync_user: ‘www‘ tasks: #1.创建www组和www用户 - name: create_group group: name: "{{ rsync_user }}" gid: "{{ user_id }}" #2.创建www用户 - name: create_user user: name: "{{ rsync_user }}" uid: "{{ user_id }}" group: "{{ rsync_user }}" create_home: no shell: /sbin/nologin #3.创建数据目录并更改授权 - name: create_data file: path: "{{ item }}" state: directory owner: "{{ rsync_user }}" group: "{{ rsync_user }}" mode: ‘755‘ loop: - /data/ - /backup/ #4.安装rsync软件 - name: install_rsync yum: name: rsync state: latest #5.复制配置文件和密码文件 - name: copy pwd&conf copy: src: "{{ item.src }}" dest: /etc/ mode: "{{ item.mode }}" notify: - restart rsyncd loop: - { src: /root/script/rsync/rsyncd.conf, mode: ‘644‘} - { src: /root/script/rsync/rsync.passwd, mode: ‘600‘} #6.启动服务 - name: start systemd: name: rsyncd state: started enabled: yes #7.重启服务 handlers: - name: restart rsyncd systemd: name: rsyncd state: restarted
[ ~]# cd /etc/ansible/roles/ [ /etc/ansible/roles]# mkdir rsync_server/{tasks,handlers,files,templates,vars} -p [ /etc/ansible/roles]# tree rsync_server/ rsync_server/ ├── files ├── handlers ├── tasks ├── templates └── vars
├── tasks │ └── main.yaml
cp script/rsync/* /etc/ansible/roles/rsync_server/files/
[ ~]# cat /etc/ansible/roles/rsync_server/handlers/main.yaml - name: restart rsyncd systemd: name: rsyncd state: restarted
[ ~]# cat /etc/ansible/roles/rsync_server/vars/main.yaml user_id: ‘666‘ rsync_user: ‘www‘
[ ~]# cat /etc/ansible/roles/rsync_server/tasks/main.yaml #1.创建www组和www用户 - name: create_group group: name: "{{ rsync_user }}" gid: "{{ user_id }}" #2.创建www用户 - name: create_user user: name: "{{ rsync_user }}" uid: "{{ user_id }}" group: "{{ rsync_user }}" create_home: no shell: /sbin/nologin #3.创建数据目录并更改授权 - name: create_data file: path: "{{ item }}" state: directory owner: "{{ rsync_user }}" group: "{{ rsync_user }}" mode: ‘755‘ loop: - /data/ - /backup/ #4.安装rsync软件 - name: install_rsync yum: name: rsync state: latest #5.复制配置文件和密码文件 - name: copy pwd&conf copy: src: "{{ item.src }}" dest: /etc/ mode: "{{ item.mode }}" notify: - restart rsyncd loop: - { src: rsyncd.conf, mode: ‘644‘} - { src: rsync.passwd, mode: ‘600‘} #6.启动服务 - name: start systemd: name: rsyncd state: started enabled: yes
[ ~]# cat /etc/ansible/rsync_server.yaml - hosts: rsync_server roles: - rsync_server
[ ~]# cat /etc/ansible/hosts [rsync_server] 172.16.1.41
cd /etc/ansible/ ansible-playbook -C rsync_server.yaml ansible-playbook rsync_server.yaml
1.先拷贝配置文件到template目录下并重命名为j2 2.编写tasks文件 3.调试运行
cd /etc/ansible/roles/ mkdir sshd/{tasks,handlers,files,templates,vars} -p
jinja模板注意:
1.模块必须是template 2.模版文件必须以.j2结尾 3.模版文件必须放在template目录下
关键配置:
#复制sshd配置文件到template文件夹下 Port {{ ssh_port }} ListenAddress {{ ansible_facts.eth1.ipv4.address }}
[ /etc/ansible/roles/sshd]# cat vars/main.yaml ssh_port: ‘22‘
[ /etc/ansible/roles/sshd]# cat handlers/main.yaml - name: restart sshd systemd: name: sshd state: restarted
[ /etc/ansible/roles/sshd]# cat tasks/main.yaml #1.复制配置文件和密码文件 - name: 01_copy_sshd template: src: sshd_config.j2 dest: /etc/ssh/sshd_config mode: ‘600‘ backup: yes notify: - restart sshd #2.启动服务 - name: start systemd: name: sshd state: started enabled: yes
[root@m01 /etc/ansible/roles]# tree sshd/ sshd/ ├── files ├── handlers │ └── main.yaml ├── tasks │ └── main.yaml ├── templates │ └── sshd_config.j2 └── vars └── main.yaml
[root@m01 /etc/ansible/roles]# cat ../sshd.yaml - hosts: ssh roles: - sshd
1.先拷贝配置文件到template目录下并重命名为j2 2.编写handlers 3.编写tasks
cd /etc/ansible/roles/ mkdir nfs_server/{tasks,handlers,files,templates,vars} -p
[ ~]# cat /etc/ansible/roles/nfs_server/templates/exports.j2 /data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
[ ~]# cat /etc/ansible/roles/nfs_server/handlers/main.yaml - name: restart nfs systemd: name: nfs state: restarted
[ ~]# cat /etc/ansible/roles/nfs_server/tasks/main.yaml #1.创建www组和www用户 - name: create_group group: name: www gid: 666 #2.创建www用户 - name: create_user user: name: www uid: 666 group: www create_home: no shell: /sbin/nologin #3.创建数据目录并更改授权 - name: create_data file: path: "{{ item }}" state: directory owner: www group: www mode: ‘755‘ loop: - /data/ - /backup/ #4.安装nfs软件 - name: install_nfs yum: name: nfs-utils state: latest #5.复制配置文件和密码文件 - name: copy_exports template: src: exports.j2 dest: /etc/exports notify: - restart nfs #6.启动服务 - name: start systemd: name: nfs state: started enabled: yes
[ ~]# cat /etc/ansible/nfs_server.yaml - hosts: nfs roles: - nfs_server
1.先分析以前写过所有的角色里重复的操作 2.把重复的操作内容单独写一个角色,例如:init 3.先备份一份以前写好的角色文件 4.精简以前的角色文件,删除重复的内容 5.调试,运行,检查
1.创建www组和www用户 2.创建www用户 3.创建数据目录并更改授权 4.安装rsync软件 4.安装nfs软件
cd /etc/ansible/roles/ mkdir init/{tasks,handlers,files,templates,vars} -p
[ /etc/ansible]# cat /etc/ansible/roles/init/tasks/main.yaml #1.创建www组和www用户 - name: create_group group: name: www gid: 666 #2.创建www用户 - name: create_user user: name: www uid: 666 group: www create_home: no shell: /sbin/nologin #3.创建数据目录并更改授权 - name: create_data file: path: "{{ item }}" state: directory owner: www group: www mode: ‘755‘ loop: - /data/ - /backup/ #4.安装nfs软件 - name: install_soft yum: name: "{{ item }}" state: latest loop: - rsync - nfs-utils
[ ~]# cat /etc/ansible/roles/rsync_server/tasks/main.yaml #1.复制配置文件和密码文件 - name: copy pwd&conf copy: src: "{{ item.src }}" dest: /etc/ mode: "{{ item.mode }}" notify: - restart rsyncd loop: - { src: rsyncd.conf, mode: ‘644‘} - { src: rsync.passwd, mode: ‘600‘} #2.启动服务 - name: start systemd: name: rsyncd state: started enabled: yes
[ ~]# cat /etc/ansible/roles/nfs_server/tasks/main.yaml #1.复制配置文件和密码文件 - name: copy_exports template: src: exports.j2 dest: /etc/exports notify: - restart nfs #2.启动服务 - name: start systemd: name: nfs state: started enabled: yes
[ ~]# cat /etc/ansible/rsync_server.yaml - hosts: rsync_server roles: - init - rsync_server
[ ~]# cat /etc/ansible/nfs_server.yaml - hosts: nfs roles: - init - nfs_server