ansible软件服务部署

1.ansible软件服务部署

1.1环境准备

[root@test1 ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

1.准备三台服务器,分别如下

test1 192.168.56.32
test2 192.168.56.33
test3 192.168.56.34

1.2openssh服务相关命令参数

1.openssh服务命令ssh-keygen介绍

 ssh-keygen [-q] [-b bits] [-t type] [-N new_passphrase] [-C comment] [-f output_keyfile]
 ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]
 ssh-keygen -i [-m key_format] [-f input_keyfile]
 ssh-keygen -e [-m key_format] [-f input_keyfile]
 ssh-keygen -y [-f input_keyfile]
 ssh-keygen -c [-P passphrase] [-C comment] [-f keyfile]
 ssh-keygen -l [-f input_keyfile]
 ssh-keygen -B [-f input_keyfile]
 ssh-keygen -D pkcs11
 ssh-keygen -F hostname [-f known_hosts_file] [-l]
 ssh-keygen -H [-f known_hosts_file]
 ssh-keygen -R hostname [-f known_hosts_file]
 ssh-keygen -r hostname [-f input_keyfile] [-g]
 ssh-keygen -G output_file [-v] [-b bits] [-M memory] [-S start_point]
 ssh-keygen -T output_file -f input_file [-v] [-a rounds] [-J num_lines] [-j start_line] [-K checkpt] [-W generator]
 ssh-keygen -s ca_key -I certificate_identity [-h] [-n principals] [-O option] [-V validity_interval] [-z serial_number] file ...
 ssh-keygen -L [-f input_keyfile]
 ssh-keygen -A
 ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number] file ...
 ssh-keygen -Q -f krl_file file ...

1.常用参数

-t type  #指定创建密钥类型,有可能的类型为rsa1关于ssh协议版本v1的加密,还有可能是dsa、ecdsa、rsa关于ssh协议版本v2的加密

-f filename  #指定私钥文件名称及文件保存路径信息

-P passphrase   #提供密码信息

-N new_passphrase    #提供新的密码信息

-q   #ssh-keygen静默模式,用于通过/etc/rc创建新的key的时候

2.案例演示

[root@test1 ~]# rm -f /root/.ssh/* && ssh-keygen -t dsa -f /root/.ssh/id_dsa -P ""
Generating public/private dsa key pair.
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
83:5b:61:45:20:36:a0:32:42:df:92:d1:c2:8e:b8:b3 root@test1
The key's randomart image is:
+--[ DSA 1024]----+
| ...o.+ .oo      |
|. .++o o .       |
|= ++.. o         |
|o+ .. o .        |
| . . S           |
|o o .            |
| o .             |
|E                |
|                 |
+-----------------+

命令说明:rm -f删除家目录下的.ssh目录下面的所有文件;接着ssh-keygen -t 指定类型为dsa,-f指定把密钥保存在此目录下,命名
为id_dsa;/root/.ssh/id_dsa; -P大写的p指定密码为空;

2. openssh服务命令ssh-copy-id

1.案例演示

[root@test1 ~]# sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub -o StrictHostKeyChecking=no "root@192.168.56.34"
[root@test1 ~]# ssh 'root@192.168.56.34' hostname
test3

2.脚本

#!/bin/bash
 
 rm -f /root/.ssh/* && ssh-keygen -t dsa -f /root/.ssh/id_dsa -P ""
 sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no root@172.16.1.31"
 
 for ip in 31 41 7
 do
  sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no root@172.16.1.$ip"
  ssh root@172.16.1.$ip hostname
 done
 #for ip in 31 41 7
 #do
  # ssh root@172.16.1.$ip hostname
 #done

2.ansible介绍

  • ansible是一个基于python开发的自动化运维工具
  • 其功能实现基于SSH远程连接服务!
  • ansible可以实现批量系统配置、批量软件部署、批量文件拷贝、批量运行命令等功能

1.ansible软件特点概述

  • 不需要单独安装客户端(no agents)基于系统自带的sshd服务,sshd就相当于ansible的客户端
  • 不需要服务器端(no server)
  • 需要依靠大量的模块实现批量管理
  • 配置文件/etc/ansible/ansible.cfg

2.ansible借助公钥批量管理

[root@test1 ~]# sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub -o StrictHostKeyChecking=no "root@192.168.56.34"
[root@test1 ~]# sshpass -p123456 ssh "root@192.168.56.34" -a "uptime"
 22:36:38 up 2:23, 3 users, load average: 0.00, 0.01, 0.05
[root@test1 ~]# sshpass -p123456 scp /etc/hosts root@192.168.56.34:~
[root@test1 ~]# sshpass -p123456 ssh "root@192.168.56.34" -a "ls"
anaconda-ks.cfg
hosts

3. ansible软件安装部署信息

搭建yum仓库,定制rpm包是自动化运维关键内容
先保留yum安装的软件

[root@test1 ~]# sed -i.bak 's#keepcache=0#keepcache=1#g' /etc/yum.conf
[root@test1 ~]# grep keepcache /etc/yum.conf
keepcache=1

1.安装ansible

1.安装ansible需要借助epel源进行下载安装

[root@test1 ~]# yum install -y epel-release  #临时epel源
[root@test1 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

2.安装ansible软件

[root@test1 ~]# yum install -y ansible
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package ansible.noarch 0:2.4.2.0-1.el7 will be installed
--> Processing Dependency: PyYAML for package: ansible-2.4.2.0-1.el7.noarch
....省略部分...
Dependency Updated:
 openssl.x86_64 1:1.0.2k-8.el7 openssl-libs.x86_64 1:1.0.2k-8.el7 

Complete!

2.客户端配置

1.所有被管理端需要安装libselinux-python软件

批量安装

[root@test1 scripts]# vim yum.sh 
#!/bin/bash
rm -f /root/.ssh/* && ssh-keygen -t dsa -f /root/.ssh/id_dsa -P ""
for ip in $*
do
 sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub -o StrictHostKeyChecking=no "root@$ip"
 ssh root@$ip -a "yum install -y libselinux-python"
done
[root@test1 scripts]# sh /server/scripts/yum.sh 192.168.56.33 192.168.56.34

2. ansible实践部署基础配置

编辑ansible的主机配置文件hosts,添加主机组test

[root@test1 scripts]# cp /etc/ansible/hosts{,.bak}
[root@test1 scripts]# cat >/etc/ansible/hosts<<EOF
> [test]
> 192.168.56.33
> 192.168.56.34
> EOF
[root@test1 scripts]# cat /etc/ansible/hosts
[test]
192.168.56.33
192.168.56.34

3.测试演练

[root@test1 scripts]# ansible test -m command -a "uptime"
192.168.56.34 | SUCCESS | rc=0 >>
 00:58:00 up 4:45, 3 users, load average: 0.00, 0.01, 0.05

192.168.56.33 | SUCCESS | rc=0 >>
 10:25:56 up 9:36, 2 users, load average: 0.08, 0.03, 0.05

ansible 命令 主机组/单独 ip/域名 -m command -a “uptime”
说明:-m指定使用的模块 -a 指定使用模块中相应的命令参数
 命令参数只能是基本命令,并不支持管道执行操作

1. ansible远程批量拷贝信息

[root@test1 scripts]# ansible test -m copy -a "src=/etc/passwd dest=/tmp/word.txt owner=root group=root mode=755 backup=yes"
192.168.56.33 | SUCCESS => {
 "changed": true, 
 "checksum": "9a7b620f0073eac60ea25aaa03a3f6246245e49d", 
 "dest": "/tmp/word.txt", 
 "gid": 0, 
 "group": "root", 
 "md5sum": "35ddf73adfd3c03aedc27ca220917edc", 
 "mode": "0755", 
 "owner": "root", 
 "size": 1126, 
 "src": "/root/.ansible/tmp/ansible-tmp-1514480431.34-28895657066901/source", 
 "state": "file", 
 "uid": 0
}
192.168.56.34 | SUCCESS => {
 "changed": true, 
 "checksum": "9a7b620f0073eac60ea25aaa03a3f6246245e49d", 
 "dest": "/tmp/word.txt", 
 "gid": 0, 
 "group": "root", 
 "md5sum": "35ddf73adfd3c03aedc27ca220917edc", 
 "mode": "0755", 
 "owner": "root", 
 "secontext": "unconfined_u:object_r:admin_home_t:s0", 
 "size": 1126, 
 "src": "/root/.ansible/tmp/ansible-tmp-1514480431.35-201433909750103/source", 
 "state": "file", 
 "uid": 0
}

说明:
1)如果指定的目标目录不存在,系统会自动创建,是否源目录会放到目标目录下去
2)如果copy的文件,dest指定的名字和源如果不同,并且它不是已经存在的目录,相当于copy过去后再重命名
3)如果dest是目标机器上已经存在的目录,则会直接把文件copy到该目录下面
4)设定的用户和组root在所有客户端必须存在
5)改变文件是否备份;backup=yes改变文件进行备份


[root@test1 scripts]# ansible test -m command -a "ls -l /tmp/"
192.168.56.33 | SUCCESS | rc=0 >>
total 4
drwx------ 2 root root 63 Jan 3 10:37 ansible_1WnXKy
drwxr-xr-x 3 root root 34 Jan 3 01:24 linuxprobe
-rwxr-xr-x 1 root root 1126 Jan 3 10:37 word.txt

192.168.56.34 | SUCCESS | rc=0 >>
total 4
drwx------. 2 root root 63 Jan 4 01:09 ansible_oy89d6
-rwxr-xr-x. 1 root root 1126 Jan 4 01:09 word.txt

2.ansible批量执行脚本文件

[root@test1 scripts]# vim yum-telnet.sh
#!/bin/bash

count=`rpm -qa telnet |wc -l`

if [ "$count" -lt 1 ]; then
 yum install -y telnet
fi

方法一

[root@test1 scripts]# ansible test -m script -a "/server/scripts/yum-telnet.sh"

方法二

[root@test1 scripts]# ansible test -m copy -a "src=/server/scripts/yum-telnet.sh dest=/server/scripts/ mode=655"
[root@test1 scripts]# ansible test -m shell -a "/server/scripts/yum-telnet.sh"
192.168.56.33 | SUCCESS | rc=0 >>


192.168.56.34 | SUCCESS | rc=0 >>

3.ansible模块介绍

1.yum模块

通过yum模块安装nmap软件

[root@test1 scripts]# ansible test -m yum -a "name=nmap state=installed"
[root@test1 scripts]# ansible test -m command -a "rpm -qa nmap"
 [WARNING]: Consider using yum, dnf or zypper module rather than running rpm

192.168.56.33 | SUCCESS | rc=0 >>
nmap-6.40-7.el7.x86_64

192.168.56.34 | SUCCESS | rc=0 >>
nmap-6.40-7.el7.x86_64

2.service模块

通过service模块管理软件服务

[root@test1 scripts]# ansible test -m service -a "name=crond state=stopped enabled=no"

说明:此处的name是服务名,表示将crond停止,并且取消开机自启动

3.mount挂载模块

选项: 
dump
fstype:必选项,挂载文件的类型 
name:必选项,挂载点 
opts:传递给mount命令的参数
src:必选项,要挂载的文件 
state:必选项 
present:只处理fstab中的配置 
absent:删除挂载点 
mounted:自动创建挂载点并挂载之 
umounted:卸载

【案例】:
[root@test1 scripts]# ansible test -m mount -a "name=/mnt src=/dev/loop0 fstype=ext4 state=mounted opts=rw,noatime,nodiratime,noikeep,nobarrier"

4.file模块

[root@test1 scripts]# ansible test -m file -a "path=/tmp/foo.conf state=touch"
192.168.56.33 | SUCCESS => {
 "changed": true, 
 "dest": "/tmp/foo.conf", 
 "gid": 0, 
 "group": "root", 
 "mode": "0644", 
 "owner": "root", 
 "size": 0, 
 "state": "file", 
 "uid": 0
}
192.168.56.34 | SUCCESS => {
 "changed": true, 
 "dest": "/tmp/foo.conf", 
 "gid": 0, 
 "group": "root", 
 "mode": "0644", 
 "owner": "root", 
 "secontext": "unconfined_u:object_r:user_tmp_t:s0", 
 "size": 0, 
 "state": "file", 
 "uid": 0
}
ansible test -m file -a "src=/etc/hosts dest=/tmp/hosts state=link"
说明:表示批量创建软件
ansible 172.16.1.31 -m file -a "path=/data state=directory"
选项参数 选项说明
force 需要在两种情况下强制创建软链接
1. 是源文件不存在,但之后会建立的情况下
2. 是目标软链接已存在,需要先取消之前的软链接,然后创建新的软链接
有两个选项:yes|no
group 定义文件/目录的属组
mode 定义文件/目录的权限
owner 定义文件/目录的属主
path 必选项,定义文件/目录的路径
recurse 递归设置文件的属性,只对目录有效
src  directory:如果目录不存在,就创建目录
     file:即使文件不存在,也不会被
     link:创建软链
     hard:创建硬链
     touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改
     absent:删除目录、文件或者取消链接文件

5. 【ansible软件常用模块信息汇总表】

常用模块 模块说明
command (重要参数)      执行命令模块,ansible命令执行默认模块
shell (重要参数)        执行shell脚本模块
script (重要参数)       把脚本发到客户端,然后执行;执行脚本命令在远端服务器上
copy (重要参数)         把本地文件发送到远端
file                     设定文件属性模块
service                  系统服务管理模块
cron                     计划任务管理模块
yum                      软件包安装管理模块
synchronize              使用rsync同步文件模块
ping                     检测主机连通性 ansible test -m ping
mount                    配置挂载点

6. ansiblle软件帮助信息

ansible-doc -l #<-列出所有的模块。
ansible-doc -s service #<-查看指定模块用法。

#注意:centos6.8 使用ansible-doc -l报错
#解决方法:
sed -i 's@#deprecation_warnings=True@ deprecation_warnings=False@g' /etc/ansible/ansible.cfg
mv /usr/lib/python2.6/site-packages/ansible/modules/extras/cloud/misc/rhevm.py /tmp/

3.ansible进阶部分

1.定时任务模块

[root@test1 scripts]# ansible 192.168.56.33 -m cron -a "name='utptime info' minute=00 hour=00 job='/usr/sbin/ntpdate ntp1.aliyun.com >>/dev/null 2>&1' state=present"
192.168.56.33 | SUCCESS => {
 "changed": true, 
 "envs": [], 
 "jobs": [
 "utptime info"
 ]
}

2.临时关闭与开启指定定时任务效果,但不删除定时任务条目

ansible 192.168.56.33 -m cron -a "name='restart network' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' disabled=yes"

3.删除无用的定时任务信息

ansible 192.168.56.33 -C -m cron -a "name='None' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' state=absent"
ansible 192.168.56.33 -m cron -a "name='None' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' state=absent"

3.编写剧本

[root@test1 playbooks]# vim cron.yml 

- hosts: test
  tasks:
    - name: restart-network
      cron: name='restart network' minute=05 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'
    - name: sync time
      cron: name='sync time' minute=*/5 job="/usr/sbin/ntpdate pool.ntp.com >/dev/null 2>&1"

执行

[root@test1 playbooks]# ansible-playbook -C cron.yml

PLAY [***************

TASK [Gathering Facts] ****************************************************************************************
ok: [192.168.56.33]
ok: [192.168.56.34]

TASK [restart-network] ****************************************************************************************
changed: [192.168.56.33]
changed: [192.168.56.34]

TASK [sync time] **********************************************************************************************
changed: [192.168.56.33]
changed: [192.168.56.34]

PLAY RECAP ****************************************************************************************************
192.168.56.33 : ok=3 changed=2 unreachable=0 failed=0 
192.168.56.34 : ok=3 changed=2 unreachable=0 failed=0

4.规则

说明:- 的后面必须有一个空格,tasks:前面必须空两个空格,tasks下面的一行,必须空四个空格;
pyYAML语法规则:
规则一:缩进
 yaml使用一个固定的缩进风格表示数据层结构关系,Saltstack/ansible需要每个缩进级别由两个空格组 成。一定不能使用tab键
 规则二:冒号
 yaml:
 mykey: my_value
 每个冒号后面一定要有一个空格(以冒号结尾不需要空格,表示文件路径的模版可以不需要空格)
 规则三:短横线
 想要表示列表项,使用一个短横杠加一个空格。多个项使用同样的缩进级别作为同一个列表的一部分 - hosts 
 
 核心规则:有效的利用空格进行剧本的编写,剧本编写是不支持tab的

特点:

1、不需要单独安装客户端(No agents),基于系统自带的sshd服务,sshd就相当于ansible的客户端

2、不需要服务端

3、需要依靠大量的模板实现批量管理

4、配置文件/etc/ansible/ansible.cfg

 

 

 

0
如无特殊说明,文章均为本站原创,转载请注明出处

该文章由 发布

这货来去如风,什么鬼都没留下!!!
发表我的评论

Hi,请填写昵称和邮箱!

取消评论
代码 贴图 加粗 链接 删除线 签到