MENU

Ansible服务-剧本-角色

• 2019 年 02 月 05 日 • 默认分类,Linux技术

第一章 Ansible介绍

1.什么是Ansible

Ansible 是 python 中的一套模块,系统中的一套自动化工具,只需要使用 ssh 协议连接及可用来系统管理、自动化执行命令等任务。

2.为什么需要Ansible?

批量管理功能:

  1. 可以实现批量系统操作配置
  2. 可以实现批量软件服务部署
  3. 可以实现批量文件数据分发
  4. 可以实现批量系统信息收集

管理服务意义:

  1. 提高工作的效率(部署综合架构)
  2. 提高工作准确度
  3. 减少维护的成本
  4. 减少重复性工作

安装部署简单:

  1. 没有配置文件(不需要配置)
  2. 不需要启动服务
  3. 客户端没有需要部署任务

如何学习Ansible?

1.官方文档
2.帮助文档
3.其他人写好的文档

第二章 Ansible安装部署

Ansible的安装部署十分简单,只需要yum安装就行 (需要epel源)

[root@xoxo1 ~]# yum install ansible -y

第三章 Ansible主机清单

/etc/ansible/hosts 主机资产清单文件,用于定义被管理主机的认证信息, 例如 ssh 登录用户名、密码以及 key相关信息。

1.主机支持主机名通配以及正则表达式,例如 web[1:3].xoxo.com 代表三台主机
2.主机支持基于非标准的 ssh 端口,例如 web1.xoxo.com:6666
3.主机支持指定变量,可对个别主机的特殊配置,如登陆用户,密码
4.主机组支持指定变量[group_name:vars],同时支持嵌套组[game:children]

1.指定主机组相关配置

#主机组
[root@xoxo1 ~]# cat /etc/ansible/hosts
[webserver]
192.168.10.2 
192.168.10.3
#主机+端口+密码
[webserver]
192.168.10.2 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'
192.168.10.3 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'
#对整个主机组都生效的变量
[webserver:vars]
ansible_ssh_pass='123456'

#针对所有webserver组都生效的变量
[webserver:vars]       
ansible_ssh_pass='123456'

第四章 Ansible常用模块

0.如何学习模块

ansible官方网站:
https://docs.ansible.com/
模块的应用语法格式:
ansible 主机名称/主机组名称/主机地址信息/all -m(指定应用的模块信息) 模块名称 -a(指定动作信息) "执行什么动作"

1.ping

应用场景:
测试主机和ansible之间的连通性
举例:
对webserver主机组测试是否连通

ansible webserver -m ping

2.command 简单模块
应用场景:
类似shell,但是只能执行简单的命令,复杂的命令和有些符号并不能识别,用的比较少
01.查看主机名,可以执行成功

[root@xoxo1 ~]# ansible webserver -m ping       
192.168.10.3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.10.2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

02.使用awk拼接查看主机IP执行失败

[root@xoxo1 ~]# ansible webserver -m command -a "ifconfig eth0|grep 10"     
192.168.10.2 | FAILED | rc=255 >>
SIOCSIFADDR: 没有那个设备
eth0|grep: ERROR while getting interface flags: 没有那个设备non-zero return code
192.168.10.3 | FAILED | rc=255 >>
SIOCSIFADDR: 没有那个设备
eth0|grep: ERROR while getting interface flags: 没有那个设备non-zero return code

3.shell 万能模块

万能模块,所有命令都可以执行,和本地执行效果一样
01.使用管道查询IP地址

[root@xoxo1 ~]# ansible webserver -m shell -a "ifconfig eth0|grep 192.168.10"
192.168.10.3 | CHANGED | rc=0 >>
        inet 192.168.10.41  netmask 255.255.255.0  broadcast 192.168.10.255
192.168.10.2 | CHANGED | rc=0 >>
        inet 192.168.10.31  netmask 255.255.255.0  broadcast 192.168.10.255

02.批量执行脚本
在其他主机上创建一个脚本,内容为打印主机名

cat > echo.sh << EOF 
#!/bin/bash
echo "$(hostname)"
EOF

然后使用ansible的shell模块批量执行

[root@xoxo1 ~]# ansible webserver -m shell -a "/bin/bash /root/echo.sh"
192.168.10.3 | CHANGED | rc=0 >>
backup

192.168.10.2 | CHANGED | rc=0 >>
nfs01

4.copy 拷贝文件

01.拷贝xoxo1的hostsname文件到其他主机的/opt目录下

[root@xoxo1 ~]# ansible 192.168.10.2 -m copy -a "src=/etc/hostname dest=/opt"
192.168.10.2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "f434396716e2c9aed47cfde87c491cce5a2c08fa", 
    "dest": "/opt/hostname", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "318d7defb693a2eb0d4f1a7a96575a57", 
    "mode": "0644", 
    "owner": "root", 
    "size": 4, 
    "src": "/root/.ansible/tmp/ansible-tmp-1563780990.63-144254987732501/source", 
    "state": "file", 
    "uid": 0
}

02.在传输文件时修改文件属主和属组信息

ansible 192.168.10.2 -m copy -a "src=/root/rsync.password dest=/etc/ owner=xoxo group=xoxo"

03.在传输文件时修改文件的权限信息

ansible 192.168.10.2 -m copy -a "src=/root/rsync.password dest=/etc/ mode=0600"

04.创建文件并直接写入内容

ansible 192.168.10.2 -m copy -a "content='xoxo' dest=/etc/rsync.password mode=0600"

05.复制目录

src后面目录没有/: 将目录本身以及目录下面的内容都进行远程传输复制

ansible 192.168.10.2 -m copy -a "src=/data dest=/data"

src后面目录有/: 只将目录下面的内容都进行远程传输复制

ansible 192.168.10.2 -m copy -a "src=/data/ dest=/data"

参数说明:

src #推送数据的源文件信息
dest #推送数据的目标路径
backup #对推送传输过去的文件,进行备份
content #直接批量在被管理端文件中添加内容
group #将本地文件推送到远端,指定文件属组信息
owner #将本地文件推送到远端,指定文件属主信息
mode #将本地文件推送到远端,指定文件权限信息

5.file 设置文件属性

01.创建文件夹

ansible xoxo -m file -a "path=/root/xoxo state=directory"

02.创建文件并更改属性

ansible xoxo -m file -a "path=/root/test.txt state=touch mode=777 owner=root group=root"

03.创建软链接

ansible xoxo -m file -a "src=/root/abc path=/root/abc_link state=link"

参数说明:

path #指定远程主机目录或文件信息
recurse #递归授权
state
directory #在远端创建目录
touch #在远端创建文件
link #link 或 hard 表示创建链接文件
absent #表示删除文件或目录
mode #设置文件或目录权限
owner #设置文件或目录属主信息
group #设置文件或目录属组信息

6.script模块

 编写脚本
[root@xoxo1 ~]# mkdir -p /server/scripts
[root@xoxo1 ~]# cat /server/scripts/yum.sh
#!/usr/bin/bash
yum install -y iftop
#在本地运行模块,等同于在远程执行,不需要将脚本文件进行推送目标主机执行
[root@xoxo1 ~]# ansible xoxo -m script -a "/server/scripts/yum.sh"

7.cron

01.创建一条定时任务

ansible xoxo -m cron -a "minute=* hour=* day=* month=* weekday=* job='/bin/sh
/server/scripts/test.sh'"

02.添加定时任务注释信息,防止重复

ansible xoxo -m cron -a "name='cron01' job='/bin/sh /server/scripts/test.sh'"

03.删除相应定时任务

ansible xoxo -m cron -a "name='ansible cron02' minute=0 hour=0 job='/bin/sh
/server/scripts/test.sh' state=absent"

04.注释相应定时任务,使定时任务失效

ansible xoxo -m cron -a "name='ansible cron01' minute=0 hour=0 job='/bin/sh
/server/scripts/test.sh' disabled=no"

8.user

01.创建用户指定uid,gid,不创建家目录也不允许登陆

ansible xoxo -m user -a "name=oldgirl uid=888 group=888 shell=/sbin/nologin
create_home=no"

参数说明:

uid #指定用户的 uid
group #指定用户组名称
groups #指定附加组名称
password #给用户添加密码
shell #指定用户登录 shell
create_home #是否创建家目录

9.group

01.创建用户组

ansible xoxo -m group -a "name=yongheng gid=888 state=present"

参数说明:

name #指定创建的组名
gid #指定组的 gid
state
absent #移除远端主机的组
present #创建远端主机的组(默认)

10.yum

ansible xoxo -m yum -a "name=httpd state=installed"

参数说明:

name       #指定要安装的软件包名称
state      #指定使用 yum 的方法
installed, present #安装软件包
removed, absent #移除软件包
latest     #安装最新软件包

11.service

ansible xoxo -m service -a "name=nfs state=stopped enabled=yes"

参数说明:

name # 定义要启动服务的名称
state # 指定服务状态
started #启动服务
stopped #停止服务
restarted #重启服务
reloaded #重载服务
enabled #开机自启

12.mount

ansible xoxo -m mount -a "src=192.168.10.2:/data path=/data fstype=nfs opts=defaults
state=present"
ansible web -m mount -a "src=192.168.10.2:/data path=/data fstype=nfs opts=defaults
state=mounted"
 web -m mount -a "src=192.168.10.2:/data path=/data fstype=nfs opts=defaults
state=unmounted"
web -m mount -a "src=192.168.10.2:/data path=/data fstype=nfs opts=defaults
state=absent"

状态解释

present # 开机挂载,仅将挂载配置写入/etc/fstab
mounted # 挂载设备,并将配置写入/etc/fstab
unmounted # 卸载设备,不会清除/etc/fstab 写入的配置
absent # 卸载设备,会清理/etc/fstab 写入的配置

13.unarchive
01.解压远程服务器的压缩包到指定目录
创建压缩包:

cd /etc && tar zxvf /opt/sys.tar.gz etc/fstab etc/hosts

执行命令:

ansible 192.168.10.2 -m unarchive -a "src=/opt/sys.tar.gz dest=/opt/ remote_src=yes"

02.把本地文件解压到目标机器指定目录
创建命令

cd / && tar zcvf /opt/log.tar.gz var/log/messages

14.archive

01.压缩单个文件

ansible 192.168.10.2 -m archive -a "path=/var/log/message dest=/tmp/log.tar.gz format=gz force_archive=true" 

15.setup 获取主机信息

01.直接执行获取主机信息

ansible 192.168.10.2 -m setup

02.只将主机某个信息打印出来

[root@xoxo1 /server/scripts/test]# ansible 192.168.10.3 -m setup -a "filter=ansible_all_ipv4_addresses"              
192.168.10.3 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.10.3", 
            "192.168.10.41"
        ], 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

16.查看帮助

ansible-doc -l
ansible-doc copy

Ansible输出信息颜色解释

01. 绿色信息:  查看主机信息/对主机未做改动
02. 黄色信息:  对主机数据信息做了修改
03. 红色信息:  命令执行出错了
04. 粉色信息:  忠告信息
05. 蓝色信息:  显示ansible命令执行的过程

模块-剧本

第一章 使用ansible模块实现安装rsync服务

01.服务端操作

第一步:安装软件

ansible 172.16.1.41 -m yum -a "name=rsync state=installed"

第二步:编写文件

ansible 172.16.1.41 -m copy -a "src=/server/scripts/rsyncd.conf dest=/etc/"

第三步:创建用户组和用户

ansible 172.16.1.41 -m group -a "name=www gid=666"
ansible 172.16.1.41 -m user -a "name=www create_home=no shell=/sbin/nologin group=www uid=666"

第四步:创建目录

ansible 172.16.1.41 -m file -a "dest=/backup state=directory owner=www group=www"

第五步:创建密码文件

ansible 172.16.1.41 -m copy -a "content='rsync_backup:Andu dest=/etc/rsync.password mode=600"

第六步:启动服务

ansible 172.16.1.41 -m service -a "name=rsyncd state=started enabled=yes"

02.客户端操作

第一步:安装软件

ansible 172.16.1.41 -m yum -a "name=rsync state=installed"

第二章 playbook 剧本

1.什么是playbook 剧本

playbook 翻译过来就是“剧本”, 那 playbook 组成如下
play: 定义的是主机的角色
task: 定义的是具体执行的任务
playbook: 由一个或多个 play 组成,一个 play 可以包含多个 task 任务

简单理解为: 使用不同的模块完成一件事情

2.playbook 的优势

功能比ansible命令更强大
能很好的控制先后执行顺序, 以及依赖关系
语法展现更加的直观
ansible命令无法持久使用, playbook 可以持久使用

第三章 剧本的书写格式要求

01.剧本的组成

 - host: all
   remote_user: root
   vars:
    file_name: andu
   tasks:
    - name: touch new files
    shell: touch /tmp/{{file_name}}

02.注意缩进

  • 1.合理的信息缩进,两个空格表示一个缩进关系
  • 2.一定不要使用tab
标题一
_ _ 标题二
_ _ _ _ 标题三

03.冒号

所有冒号后面都要加上空格
hosts: 172.16.1.41
tasks:
yum: name=rsync state=installed 

04.短横线 - 列表功能
使用短横线构成列表信息,短横线后面需要有空格

- 老张
  男
- 爱好
  游泳

第四章 剧本书写

01.文件名格式

剧本文件拓展名为xxx.yaml
1.方便识别文件是一个剧本文件
2.文件编写时会有颜色提示

练习: 写一个剧本,使用yum/copy/service模块安装部署启动rsync服务
rsync剧本:

[root@m01 /server/scripts]# cat rsync_install.yaml 
- hosts: 172.16.1.41
  tasks:
  - name: 01-add group
    group: name=www gid=666
  - name: 02-add user
    user: name=www create_home=no shell=/sbin/nologin group=www uid=666
  - name: 03-install rsync
    yum: name=rsync state=installed
  - name: 04-copy rsync conf
    copy: src=/server/scripts/rsyncd.conf dest=/etc/
  - name: 05-create passwd conf
    copy: content='rsync_backup:xoxo' dest=/etc/rsync.passwd mode=600
  - name: 06-create backup dir
    file: path=/backup state=directory owner=www group=www
  - name: 07-create backup dir
    file: path=/data state=directory owner=www group=www
  - name: 08-start rsyncd service
    service: name=rsyncd state=started
  - name: 09-enabled rsyncd service
    systemd: name=rsyncd enabled=yes

nfs剧本:
NFS服务端:

[root@m01 /server/scripts]# cat nfs_server_install.yaml 
- hosts: nfs_server
  tasks:
  - name: 01-add group
    group: name=www gid='666'
  - name: 02-add user
    user: name=www create_home=no shell=/sbin/nologin group=www uid=666
  - name: 03-install nfs service
    yum: name=nfs-utils state=latest
  - name: 04-copy nfs exports
    copy: src=/server/scripts/exports dest=/etc/
  - name: 05-create data dir
    file: path=/data state=directory owner=www group=www
  - name: 06-start rpcbind
    service: name=rpcbind state=started
  - name: 07-start nfs
    service: name=nfs state=started
  - name: 08-enable rpcbind                                                                                                                                      
    systemd: name=rpcbind enabled=yes
  - name: 09-enable nfs 
    systemd: name=nfs enabled=yes

nfs客户端

[root@m01 /server/scripts]# cat nfs_client_install.yaml 
- hosts: nfs_client
  tasks:
  - name: 01-add group
    group: name=www gid=666
  - name: 02-add user
    user: name=www create_home=no shell=/sbin/nologin group=www uid=666
  - name: 03-install nfs service
    yum: name=nfs-utils state=latest
  - name: 04-create data dir
    file: path=/data state=directory owner=www group=www
  - name: 05-start rpcbind
    service: name=rpcbind state=started
  - name: 06-enable rpcbind
    systemd: name=rpcbind enabled=yes
  - name: 07-mount data
    mount: path=/data src=172.16.1.31:/data fstype=nfs opts=defaults state=mounted

02.检查剧本语法

ansible-playbook --syntax-check nfs_client_install.yaml 

03.模拟执行剧本

ansible-playbook -C nfs_client_install.yaml 

04.执行剧本

ansible-playbook nfs_client_install.yaml 

第五章 剧本高级 特性

我们已经体验了使用剧本来安装服务,但是上述的简单ansible剧本存在一定的局限性

1.全部写成一行虽然看起来整洁,但是有一些特性没办法使用
2.比如同时需要创建多个目录,启动多个服务,需要重复写多条语句
3.参数不直观,不好修改
4.剧本里写的是启动服务,如果配置文件发生变化,重复执行不会重启服务
不过没有关系,等学习了下面的高级特性,然后我们可以换一种写法

01.循环
官方网址:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html
使用情景:

1.需要创建多个目录
2.需要启动多个服务

具体实现:
1.同时创建2个目录/data和/backup

[root@m01 /server/scripts/test]# cat loops.yaml 
- hosts: 172.16.1.41
  tasks:
  - name: 01-create dir data and backuo
    file:
      path: "{{ item }}"
      state: directory
      owner: www
      group: www
    loop: 
    - /data
    - /backup

2.同时启动2个服务

[root@m01 /server/scripts/test]# cat service.yaml 
- hosts: 172.16.1.31
  tasks: 
  - name: 01-start rpcbind nfs service
    service: 
      name: "{{ item }}"
      state: started
    loop:
      - rpcbind
      - nfs

02.变量
官方网址:

https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html

使用情景:

1.自定义某个名称,在任务中会多次引用
2.从主机收集的系统信息中提取某个变量并引用,例如网卡信息

具体实现:
1.自定义一个文件名变量,创建文件时引用

[root@m01 /server/scripts/test]# cat vars.yaml 
- hosts: 172.16.1.41
  vars:
    file_name: Andu

  tasks: 
  - name: 01-use vars create dir
    file: 
      path: "/root/{{ file_name }}"
      state: directory
      owner: www
      group: www

2.使用变量获取主机的eth1地址

[root@m01 /server/scripts/test]# cat ip.yaml 
- hosts: 172.16.1.41
  tasks:
  - name: 01-get ip address
    shell: "echo {{ ansible_facts.eth1.ipv4.address }} > /root/ip_eth1.txt"

3.在主机hosts中指定变量

[root@m01 ~]# tail -5 /etc/ansible/hosts
[backup]
172.16.1.41

[backup:vars]
file_name="Andu"

03.注册变量
使用情景:将配置文件的状态注册成一个变量,方便其他任务引用
具体实现:
1.将配置文件的状态注册成一个服务变量并打印出来

[root@m01 /server/scripts/test]# cat register.yaml 
- hosts: 172.16.1.41
  tasks:
  - name: 01-register rsync status
    shell: netstat -lntp|grep rsync
    register: rsync_port

  - name: 02-out rsync status
    debug:
      msg: "{{ rsync_port.stdout_lines }}"

打印多个信息

- hosts: nfs
  tasks:
  - name: 01-echo hostname
    shell: echo $(hostname)
    register: nfs_hostname

  - name: debug nfs_hostname
    debug:
      msg: "{{ item }}"
    loop:
      - "{{ nfs_hostname.stdout }}"
      - "{{ nfs_hostname.cmd }}"

04.服务管理
官方文档:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html?highlight=handlers#handlers-running-operations-on-change
使用情景:如果配置文件发生了变化,就重启服务,否则什么都不操作
具体实现:

[root@m01 /server/scripts/test]# cat handlers.yaml 
- hosts: 172.16.1.41
  tasks:
  - name: 01-if nfs conf changed,then restart nfs service
    copy:
      src: exports
      dest: /etc/
    notify: Restart_Nfs_Server

  handlers:
  - name: Restart_Nfs_Server
    service: 
      name: nfs
      state: restarted

05.标签
使用情景:从我们指定的任务开始执行,而不是从头到尾执行一遍
具体实现:

- hosts: 172.16.1.41
  tasks:
  - 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 group=www uid=666
    tags: 02-add-user
  - name: 03-install rsync
    yum: name=rsync state=installed
    tags: 03-install-rsync
  - name: 04-copy rsync conf
    copy: src=/server/scripts/rsyncd.conf dest=/etc/
    tags: 04-copy-conf
  - name: 05-create passwd conf
    copy: content='rsync_backup:xoxo' dest=/etc/rsync.passwd mode=600
    tags: 05-create-passwd
  - name: 06-create backup dir
    file: path=/backup state=directory owner=www group=www
    tags: 06-create-backup
  - name: 07-create backup dir
    file: path=/data state=directory owner=www group=www
    tags: 07-create-data
  - name: 08-start rsyncd service
    service: name=rsyncd state=started
    tags: 08-start-rsyncd
  - name: 09-enabled rsyncd service
    systemd: name=rsyncd enabled=yes
    tags: 09-enable

调用标签:
1.打印出playbook里要执行的所有标签

[root@m01 /server/scripts/test]# ansible-playbook --list-tags tags2.yml 

playbook: tags2.yml

  play #1 (172.16.1.41): 172.16.1.41    TAGS: []
      TASK TAGS: [01-add-group, 02-add-user, 03-install-rsync, 04-copy-conf, 05-create-passwd, 06-create-backup, 07-create-data, 08-start-rsyncd, 09-enable]

2.指定运行某个标签

ansible-playbook -t 05-create-passwd tags2.yml

3.指定运行多个标签,使用逗号隔开

ansible-playbook -t 05-create-passwd,06-create-backup tags2.yml

3.指定不运行某个标签

ansible-playbook --skip-tags=05-create-passwd tags2.yml

4.指定不运行多个标签

ansible-playbook --skip-tags=05-create-passwd,06-create-backup tags2.yml

06.运行检查规范
00.检查剧本拼写规范

ansible-playbook --syntax-check check.yaml 

01.检查这个任务执行的主机对象

ansible-playbook --list-host check.yaml 

02.检查这个剧本需要执行哪些任务

ansible-playbook --list-tasks check.yaml 

03.检查这个剧本执行哪些tag

ansible-playbook --list-tags check.yaml

04.模拟执行剧本

ansible-playbook -C check.yaml

第6章 实战剧本部署rsync/nfs/lsyncd
脚本实例:

- hosts: rsync_server
  vars: 
    rsync_conf_path: '/server/scripts/rsync_yaml/rsyncd.conf'

  tasks:
  - name: 01-install rsync
    yum: 
      name: rsync 
      state: installed

  - name: 02-backup & copy
    copy:
      src: "{{ rsync_conf_path }}"
      dest: /etc/
      backup: yes
    notify:
      - restart rsyncd

  - name: 03-create user group
    group: 
      name: www 
      gid: 666

  - name: 04-create user user 
    user: 
      name: www 
      create_home: no 
      shell: /sbin/nologin 
      group: www 
      uid: 666 
 
  - name: 05-create backup dir
    file: 
      dest: "{{ item }}"
      state: directory 
      owner: www 
      group: www
    loop:
      - /backup
      - /data 

  - name: 06-create passwd
    copy: 
      content: 'rsync_backup:Andu' 
      dest: /etc/rsync.passwd 
      mode: '0600'

  - name: 09-start rsynd
    service: 
      name: rsyncd 
      state: started 
      enabled: yes
    tags: 09_start_rsynd

  handlers:  
  - name: restart rsyncd
    service: 
      name: rsyncd
      state: restarted

01.rsync剧本
02.nfs剧本
03.lsync剧本

Ansible角色

第一章 Ansible rolers介绍

官方地址:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html

第二章 角色目录规划

01.目录说明:
官方的目录结构,必须这样定义!

[root@m01 ~]# cd /etc/ansible/roles/
[root@m01 /etc/ansible/roles]# tree
.
├── nfs                   #角色名称
│   ├── files             #存放需要copy的文件
│   ├── handlers          #触发任务剧本
│   ├── tasks             #具体任务剧本
│   ├── templates         #模版文件
│   └── vars              #存放变量文件

02.创建项目目录
因为每台服务器都需要创建用户组,用户,安装服务等,所以我们可以将这些相同的任务单独创建一个init初始化角色。

角色规划:
1.init      #初始化任务
2.rsync     #rsync服务
3.nfs       #nfs服务
4.lsyncd    #lsyncd服务

创建角色目录:

[root@m01 ~]# cd /etc/ansible/roles/
[root@m01 /etc/ansible/roles]# mkdir {init,nfs,rsync,lsyncd}/{vars,tasks,templates,handlers,files} -p     
[root@m01 /etc/ansible/roles]# tree
/etc/ansible/roles/
.
├── init
│   ├── files
│   ├── handlers
│   ├── tasks
│   ├── templates
│   └── vars
├── lsyncd
│   ├── files
│   ├── handlers
│   ├── tasks
│   ├── templates
│   └── vars
├── nfs
│   ├── files
│   ├── handlers
│   ├── tasks
│   ├── templates
│   └── vars
├── rsync
│   ├── files
│   ├── handlers
│   ├── tasks
│   ├── templates
│   └── vars
└── site.yml

第三章 编写init角色剧本

01.创建对应目录

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

02.编写任务剧本

[root@m01 ~]# cat /etc/ansible/roles/init/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

03.编写jinja模版文件

[root@m01 ~]# tree /etc/ansible/roles/init/templates/
/etc/ansible/roles/init/templates/
└── sshd_config.j2

04.编写handlers文件

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

第四章 编写rsync角色剧本

01.创建对应目录

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

02.编写任务剧本

[root@m01 ~]# cat /etc/ansible/roles/rsync/tasks/main.yml    

01.安装rsync服务

  - name: 01_install_rsync
    yum: 
      name: rsync 
      state: installed

02.拷贝配置文件模版

  - name: 02_copy_conf
    template:
      src: "{{ item.src}}"
      dest: "/etc/{{ item.dest }}"
      mode: "{{ item.mode }}"
      backup: yes
    loop:
      - { src: 'rsyncd.conf.j2',  dest: 'rsyncd.conf',  mode: '0644' }
      - { src: 'rsync.passwd.j2', dest: 'rsync.passwd', mode: '0600' }
    notify:
      - restart rsyncd

03.创建备份目录

  - name: 03_create_backup_dir
    file: 
      dest: "{{ item }}"
      state: directory 
      owner: www 
      group: www
    loop:
      - /backup
      - /data 

04.启动服务

  - name: 04_start_rsynd
    service: 
      name: rsyncd 
      state: started 
      enabled: yes

03.编写jinja模版文件

[root@m01 ~]# tree /etc/ansible/roles/rsync/templates/
/etc/ansible/roles/rsync/templates/
├── rsyncd.conf.j2
└── rsync.passwd.j2

[root@m01 ~]# cat  /etc/ansible/roles/rsync/templates/rsync.passwd.j2 
{{ user_rsyncd }}:{{ passwd_rsyncd }}

[root@m01 ~]# cat  /etc/ansible/roles/rsync/templates/rsyncd.conf.j2 
uid = www 
gid = www 
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = {{ user_rsyncd }}
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
[backup]
path = /backup
[data]
path = /data

04.编写变量文件

[root@m01 ~]# cat /etc/ansible/roles/rsync/vars/main.yml 
user_rsyncd: rsync_backup 
passwd_rsyncd: Andu 

05.编写handlers文件

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

06.编写主任务文件

[root@m01 ~]# cat /etc/ansible/roles/site.yml 
- hosts: rsync 
  roles:
    - init
    - rsync

07.最终目录

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

第四章 编写nfs角色剧本(省略)
01.创建对应目录
02.编写任务剧本
03.编写jinja模版文件
04.编写变量文件
05.编写handlers文件
06.编写主任务文件
第五章 编写lsyncd角色剧本
01.创建对应目录
02.编写任务剧本
03.编写jinja模版文件
04.编写变量文件
05.编写handlers文件
06.编写主任务文件


返回文章列表 文章二维码 打赏
本页链接的二维码
打赏二维码