每天都学一点

ansible批量管理服务工具

04 05月
作者:林健|分类:Linux

ansible批量管理服务工具

 

批量管理服务器的工具

 
  1. 无须部署agent,通过ssh进行管理


  2. 流行的自动化运维工具:https://github.con/ansible/ansible

 

三种批量管理工具

 
  1. ansible (so easy) 500以下服务器


  2. saltstack (比较复杂) 10004万服务器


  3. puppet (超级复杂) 只有很老企业在用

 

jkenkins简介

 
  1. 可视化运维(主要用在可视化部署)


  2. 持续构建,可以和git,svn结合(存放开发代码的仓库)


  3. 可结合ssh实现可视化运维


  4. 可结合ansible实现可视化运维

 

Ansible服务器简单的综合安全管理策略

 
  1. #禁止非root用户查看Ansible管理服务器端/etc/hosts文件

  2. [root@ansible ~]# ll /etc/hosts

  3. -rw-r--r--. 1 root root 180 9 9 00:38 /etc/hosts

  4. [root@ansible ~]# chmod 600 /etc/hosts

  5. #禁止非root用户查看Ansible的主机清单配置文件

  6. [root@ansible ~]# ll /etc/ansible/hosts

  7. -rw-r--r-- 1 root root 87 9 9 21:59 /etc/ansible/hosts

  8. [root@ansible ~]# chmod 600 /etc/ansible/hosts

 

ansible查看帮助

 
  1. /usr/local/python/bin/ansible-doc -l(查看总帮助)


  2. /usr/local/python/bin/ansible-doc -s shell(查看shell模块的帮助)


  3. /usr/local/python/bin/ansible-doc -s raw

 

安装ansible流程

 

如果Centos7版本

需要安装yun -y install net-toolsvim

关闭防火墙:systemctl stop firewalld 关闭防火墙开机启动:systemctl disable fierwalld

关闭selinux

 

7.5yum安装ansible

7.5yum仓库全可以用,本地的需要自己手动打开

yum -y install epel-release

yum -y install ansible (自动安装sshpass软件包)

 

安装支持包

yum -y install lrzsz vim net-tools gcc gcc-c++ ncurses ncurses-devel unzip zlib-devel zlib openssl-devel openssl libffi-devel

image_1cpuccfs01qshm4113hs1p4etkj9.png-56.5kB

 

下载python源码包

(需要云yum)

wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz

image_1cpuciu9n1051r9q1rie7fu165jm.png-60.8kB

 

源码编译Python3.5

tar xf Python-3.5.2.tgz -C /usr/src

cd /usr/src/Python-3.5.2

./configure --prefix=/usr/local/python

make && make install

ln -s /usr/local/python/bin/python3 /usr/bin/python3(制作软链接)

which python3(查看命令是否存在)

python3 -V(查询python版本)

image_1cpucputdi7e1kfrdio18sl5np20.png-11.5kB

 

静心等待ansible安装完毕后

ln -s /usr/local/python/bin/ansible /usr/local/bin(制作软链接)

which ansible(查看命令是否存在)

ansible --version(查看ansible版本)

image_1cpuclvd7174d9k45ho1pl8m5q13.png-34.5kB

 

ansible的简单配置

通过pip安装的ansible是没有配置文件的

mkdir -p /etc/ansible(默认没有,需要手动创建)

vim /etc/ansible/hosts(默认没有,需要手动创建)

image_1cpuej1g8gvsanj1u8tilk15f02d.png-16.8kB

 

ansible命令使用格式

ansible -i 主机或主机组 -m 指定模块 -a 命令 (-i指定配置文件,不写就默认路径下/etc/ansible/hosts,-m指定模块,-a发布命令)

 

command模块发布命令使用格式

(对方需要有python包,发布命令)

ansible nginx -m command -a 'hostname -I' 
(分发模块内容格式,nginx是模块名,-a是条件,-m command是调用ansible里面的模块发布命令用)

ansible client2 -m command -a 'hostname -I' 
(分发单个主机格式,client2是主机名,-a是条件,-m command是调用ansible里面的模块发布命令用)

ansible client1 -m command -a 'hostname -I' 
(分发单个主机格式,client1是主机名,-a是条件,-m command是调用ansible里面的模块发布命令用)

ansible client1:client2 -m command -a 'hostname -I' 
(分发多个主机格式,client1:client2是主机名,-a是条件,-m command是调用ansible里面的模块发布命令用)

ansible all -m command -a 'hostname -I' 
(all是分发所有主机格式,-a是条件,-m command是调用ansible里面的模块发布命令用)

image_1cpuhnnvu7uk1q4c10qmuarsdadh.png-8.9kB 
image_1cpuhnafp1cmsi2t1dpb12j1t40d4.png-8.5kB 
image_1cpugtmg3rjg12kt1oi5111m1kbj23.png-12.8kB
image_1cpughcpg1r101ffd1ambnv71l5c19.png-62.3kB

 

ping模块发布命令使用格式

(对方需要有python包)

ping模块检查服务器是否连接正常,ping模块不需要-a指定参数

ansible all -m ping (ansible的ping模块格式)

image_1cpuhde631ru5ducsv5tcd765g.png-16.3kB

 

shell模块发布命令使用格式

(对方需要有python包)

shell模块支持管道符格式 
ansible all -m shell -a 'echo test | grep t'

shell模块支持重定向格式 
ansible all -m shell -a "echo bb >> /tmp/testansible"

shell模块支持awk格式 
ansible all -m shell -a "cat /etc/passwd | awk -F":" '{print $1}'" (如果遇到特殊符号需要加入\转义)

image_1cpuhhpa8116vmed1i539pc50r5t.png-46.8kB
image_1cpuhkcjddac8tunid1oqo81f6a.png-27.7kB
image_1cpuhlo491o4j6e2mp5jnr1saqc7.png-20.8kB

 

raw模块使用格式仅通过ssh实现

(不依赖python包)

image_1cpuhtka7tir1dthe68582r2g8.png-24.1kB 
image_1cpuhtsprnfcmc51rns1tql35jgl.png-39.2kB 
image_1cpuhuavg1e3712a01b1qi8sgbai2.png-37kB

 

copy模块注意事项

yum -y install libselinux-python(传送失败的话说明对方没有这个支持包)

 

copy模块拷贝文件目录使用格式

ansible all -m copy -a 'src=/root/xin dest=/tmp' 
(src源文件,dest目标位置,对方没有目录模块自动创建)

image_1cpuitgi11bmn36h1lv7e9m154pif.png-85.8kB
image_1cpuitpn1hu11bdr1n0m2td13n2is.png-7.1kB 
image_1cpuitvbopga1huj1s99ko5j3vj9.png-7.3kB

ansible all -m copy -a 'src=/root/xin dest=/tmp backup=yes' 
(src源文件,dest目标位置,backup=yes覆盖同时是否备份源文件)

image_1cpuj2a891d9am9g193mjdbjlpjm.png-89kB
image_1cpuj857v6gut3d8jjnrie3mk3.png-10kB 
image_1cpuj8dvv13ho1qggo3d1gt8op5kg.png-11.2kB

ansible all -m copy -a 'src=/root/xin dest=/tmp owner=nobody group=nobody mode=0600' 
(owner=属主是谁,group=属组是谁,mode=它权限)

image_1cpujil351hviphgft21fto27vkt.png-86.2kB
image_1cpujjumbu2414e81erjqvj1vsla.png-12.8kB 
image_1cpujke903mrl101qel58o1o54ln.png-11.7kB

 

script模块批量运行脚本使用格式

ansible all -m script -a "/service/scripts/auto_nginx.sh" (6版本必须/bin/sh运行脚本,7版本直接绝对路径启动脚本即可)

 

cron定时任务模块

Ansible中的cron模块用于定义任务计划。主要包括两种状态(state)

image_1cq03is0tutncsk2gful3hme9.png-43.2kB

 
  1. #添加定时任务计划,在所有被管理的主机里每十分钟输出hello字符串,定时任务描述为test cron job

  2. [root@ansible ~]# ansible all -m cron -'minute="*/10" job="/bin/echo hello" name="test cron job"'

  3. Web02 | SUCCESS => {

  4. "changed": true,

  5. "envs": [],

  6. "jobs": [

  7. "test cron job"

  8. ]

  9. }

  10. Web01 | SUCCESS => {

  11. "changed": true,

  12. "envs": [],

  13. "jobs": [

  14. "test cron job"

  15. ]

  16. }

  17. [root@ansible ~]# ansible all -m shell -'crontab -l'

  18. Web01 | SUCCESS | rc=0 >>

  19. #Ansible: test cron job

  20. */10 * * * * /bin/echo hello

  21. Web02 | SUCCESS | rc=0 >>

  22. #Ansible: test cron job

  23. */10 * * * * /bin/echo hello

  24. #删除描述为test cron job的定时任务

  25. [root@ansible ~]# ansible all -m cron -'minute="*/10" job="/bin/echo hello" name="test cron job" state=absent'

  26. Web02 | SUCCESS => {

  27. "changed": true,

  28. "envs": [],

  29. "jobs": []

  30. }

  31. Web01 | SUCCESS => {

  32. "changed": true,

  33. "envs": [],

  34. "jobs": []

  35. }

  36. [root@ansible ~]# ansible all -m shell -'crontab -l'

  37. Web02 | SUCCESS | rc=0 >>

  38. Web01 | SUCCESS | rc=0 >>

  39. #给Web01服务器上的普通用户yunjisuan添加一个定时任务

  40. [root@ansible ~]# ansible Web01 -m shell -'id yunjisuan'

  41. Web01 | SUCCESS | rc=0 >>

  42. uid=1000(yunjisuan) gid=1000(yunjisuan) 组=1000(yunjisuan)

  43. [root@ansible ~]# ansible Web01 -m cron -'minute="*/10" job="/bin/echo hello" name="yunjisuan cron job" user="yunjisuan"'

  44. Web01 | SUCCESS => {

  45. "changed": true,

  46. "envs": [],

  47. "jobs": [

  48. "yunjisuan cron job"

  49. ]

  50. }

  51. [root@ansible ~]# ansible Web01 -m shell -'crontab -u yunjisuan -l'

  52. Web01 | SUCCESS | rc=0 >>

  53. #Ansible: yunjisuan cron job

  54. */10 * * * * /bin/echo hello

  55. [root@ansible ~]# ansible Web01 -m cron -'minute="*/10" job="/bin/echo hello" name="yunjisuan cron job" user="yunjisuan" state="absent"'

  56. Web01 | SUCCESS => {

  57. "changed": true,

  58. "envs": [],

  59. "jobs": []

  60. }

  61. [root@ansible ~]# ansible Web01 -m shell -'crontab -u yunjisuan -l'

  62. Web01 | SUCCESS | rc=0 >>

 

yum模块批量安装软件包

利用yum模块安装软件包,虽然能被shell模块替代,但是用yum模块更显专业一些

image_1cq03lsq3ttmiin8lvf461bp3m.png-34.1kB

 

user模块批量创建用户

用户管理模块。管理用户账号

image_1cq03nl19108b1a0811ng1iaqhea23.png-64.3kB

 
  1. #在Web02上创建一个普通用户yunjisuan,并设置用户的密码为123123

  2. [root@ansible ~]# ansible Web02 -m user -'name=yunjisuan comment="welcom to yunjisuan" uid=1066 groups=wheel password=123123 shell=/bin/bash home=/home/yunjisuan'

  3. Web02 | SUCCESS => {

  4. "changed": true,

  5. "comment": "welcom to yunjisuan",

  6. "create_home": true,

  7. "group": 1066,

  8. "groups": "wheel",

  9. "home": "/home/yunjisuan",

  10. "name": "yunjisuan",

  11. "password": "NOT_LOGGING_PASSWORD",

  12. "shell": "/bin/bash",

  13. "state": "present",

  14. "system": false,

  15. "uid": 1066

  16. }

  17. [root@ansible ~]# ansible Web02 -m shell -'tail -1 /etc/passwd'

  18. Web02 | SUCCESS | rc=0 >>

  19. yunjisuan:x:1066:1066:welcom to yunjisuan:/home/yunjisuan:/bin/bash

  20. [root@ansible ~]# ansible Web02 -m shell -'tail -1 /etc/shadow'

  21. Web02 | SUCCESS | rc=0 >>

  22. yunjisuan:123123:17783:0:99999:7::: #密码居然是明文!!!

利用ansible的user模块状态用户时要注意在password参数的后边添加密文,否则不能登陆用户 
通过Python的pip程序安装passlib即可为密码加密

 
  1. #安装Python2的pip工具,并通过pip工具安装Python的加密模块来给密码加密

  2. [root@ansible ~]# yum -y install epel-release

  3. [root@ansible ~]# yum -y install python2-pip

  4. [root@ansible ~]# pip install passlib

  5. #生成密文密码

  6. [root@ansible ~]# python -"from passlib.hash import sha512_crypt;import getpass;print sha512_crypt.encrypt(getpass.getpass())"

  7. Password: #输入你想要加密的密码

  8. $6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1 #加密后的密码

  9. #删除之前创建的yunjisuan用户,并删除它的家目录

  10. [root@ansible ~]# ansible Web02 -m user -'name=yunjisuan state=absent remove=true'

  11. Web02 | SUCCESS => {

  12. "changed": true,

  13. "force": false,

  14. "name": "yunjisuan",

  15. "remove": true,

  16. "state": "absent"

  17. }

  18. #继续在Web02上创建yunjisuan用户

  19. [root@ansible ~]# ansible Web02 -m user -'name=yunjisuan comment="welcom to yunjisuan" uid=1066 groups=wheel password=$6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1 shell=/bin/bash' home=/home/yunjisuan'

  20. [root@ansible ~]# ansible Web02 -m shell -a 'tail -1 /etc/shadow'

  21. Web02 | SUCCESS | rc=0 >>

  22. yunjisuan:$6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1:17783:0:99999:7::: #终于密文了

 

setup模块批量查看服务的所有属性

Ansible中使用setup模块收集,查看被管理主机的facts(facts是Ansible采集被管理主机设备信息的一个功能)。每个被管理主机在接收并运行管理命令之前,都会将自己的相关信息(操作系统版本,IP地址等)发送给控制主机

 
  1. #查看远程主机的facts信息

  2. [root@ansible ~]# ansible Web01 -m setup | head

  3. Web01 | SUCCESS => {

  4. "ansible_facts": {

  5. "ansible_all_ipv4_addresses": [

  6. "192.168.200.184"

  7. ],

  8. "ansible_all_ipv6_addresses": [

  9. "fe80::20c:29ff:fe77:16ad"

  10. ],

  11. "ansible_apparmor": {

  12. "status": "disabled"

 

ansible-playbook的初步使用

(playbook可以把ansible的模块进行组合)

 
  1. ln -/usr/local/python/bin/ansible-playbook /usr/local/bin (优先制作软链接)


  2. shell模块支持很多模式,copy模块分发文件或目录,register模块输出命令运行结果,nginx_conf配置下发并检测,vars自定义变量,setupvars内置变量,filevars可变配置文件


  3. vim test_shell[copy,register,nginx_conf,vars,setupvars,filevars].yaml (执行模板)


  4. ansible-playbook test_shell[copy,register,nginx_conf,vars,setupvars,filevars].yaml (执行配置文件)


  5. 我们可以使用ansible all -m setup | less (查看ansible内置变量)


  6. vim if.j2 (下发配置文件模板样式)

 

下发配置文件里面使用判断语法

 
  1. vim /tmp/if.j2


  2. {% if PORT %} #if PORT存在

  3. ip=0.0.0.0:{{ PORT }}

  4. {% else %} #否则的话

  5. ip=0.0.0.0:80

  6. {% endif %} #结尾


  7. vim test_ifvars.yaml


  8. ---

  9. - hosts: all

  10. gather_facts: True #开启系统内置变量

  11. vars:

  12. - PORT: 90 #自定义变量

  13. tasks:

  14. - name: jinja2 if test

  15. template: src=/tmp/if.j2 dest=/root/test


  16. ansible-playbook test_ifvars.yaml (下达分发命令)



  17. 如果我们将变量PORT值为空的话,就会进入else否则的80端口


  18. ---

  19. - hosts: all

  20. gather_facts: True

  21. vars:

  22. - PORT: #置空

  23. tasks:

  24. - name: jinja2 if test

  25. template: src=/tmp/if.j2 dest=/root/test

 

Playbook下发可变配置文件

 
  1. ---

  2. - hosts: all

  3. gather_facts: True #开启系统变量

  4. vars:

  5. - myname: "yunjisuan" #自定义变量

  6. tasks:

  7. - name: template test

  8. template: src=/tmp/test dest=/root/test #使用template下发可变配置文件



  9. 配置文件如果使用copy模块去下发的话,那配置都是一样的;

  10. 如果下发的配置文件里有可变的配置,需要用到template模块。

 

playbook使用register输出命令运行结果

 
  1. ---

  2. - hosts: all

  3. tasks:

  4. - name: test register

  5. shell: echo "welcome to yunjisuan"

  6. register: print_result #将之前命令的输出结果保存在变量print_result里

  7. - debug: var=print_result #将变量的值作为debug输出出来。



  8. 我们在用playbook进行ansible模块操作的时候,并没有命令的执行结果输出,默认被隐藏了

  9. 我们可以通过register模块追加输出命令的执行结果

 

playbook的自定义变量和内置变量

 
  1. ---

  2. - hosts: all

  3. vars: #定义变量

  4. - name: "yunjisuan" #第一个name变量

  5. age: "3" #第二个age变量

  6. tasks:

  7. - name: "{{ name }}" #{{}}两对大括号引用变量,变量名两头空格

  8. shell: echo "myname {{ name }},myage {{ age }}"

  9. register: var_result

  10. - debug: var=var_result


  11. 特别提示:

  12. 引用变量需要在双引号中引用。


  13. 在使用自定义变量时,我们要特别注意不要和系统的内置保留变量同名,容易引发问题。


  14. Found variable using reserved name: name #name是一个保留的内置变量,我们在自定义时不能用,会有警告



  15. ---

  16. - hosts: all

  17. gather_facts: True #使用ansible内置变量

  18. tasks:

  19. - name: setup var

  20. shell: echo "ip {{ ansible_all_ipv4_addresses[0] }} cpu {{ ansible_processor_count }}" >> /tmp/test

  21. - name: setup var2

  22. shell: echo "time {{ ansible_date_time["date"] }}" >> /tmp/test

  23. register: var_result

  24. - debug: var=var_result



  25. #python取变量方法


  26. = [1,3,5] --> a [0]=1 #这样取值就不会带[]


  27. = {"sl":123123,"sl1"123321} --> a[sl]=123123 #这样取值就不会带[]

 

playbook的简单shell模块的使用

 
  1. --- #开头三个小-开头

  2. - hosts: webB

  3. tasks:

  4. - name: test

  5. shell: echo "welcome to yunjisaun" >> /tmp/username

  6. - name: test2

  7. shell: echo "welcome to yunjisuan" >> /tmp/username

  8. 模板说明:

  9. --- #开头必须有三个小-,顶格写

  10. - hosts #正文配置代码的第一级,必须有两个空格(-占一个空格位)

  11. - host: webB #webB是host参数的值,值和hosts:之间要有一个空格

  12. tasks: #tasks:表示接下来要执行的具体任务

  13. - name: #相对于tasks再多缩进两个格(-占一个空格位),表示属于tasks的下一级

  14. - name: test #test只是要执行的具体命令的名字可以随便写。name:后还是有一个空格要注意

  15. shell: #表示调用shell模块执行命令相对于tasks仍旧要多缩进两个空格

  16. shell: echo "xxx" >> xxx #shell:后边还是要有个空格,需要注意。

 

playbook的简单copy模块的使用

 
  1. ---

  2. - hosts: all

  3. tasks:

  4. - name: test copy

  5. copy: src=/tmp/copy_test dest=/tmp/

  6. 模板说明:

  7. --- #开头必须有三个小-,顶格写

  8. - hosts #正文配置代码的第一级,必须有两个空格(-占一个空格位)

  9. tasks: #tasks:表示接下来要执行的具体任务

  10. - name: test #test只是要执行的具体命令的名字可以随便写。name:后还是有一个空格要注意

  11. copy: src=/tmp/copy_test dest=/tmp/ #把源文件拷贝到对方的位置

 

Playbook的notify通知和下发nginx配置

 
  1. vim nginx.j2

  2. worker_processes {{ ansible_processor_count }}; #可变的参数


  3. #实战下发可执行动作的可变的nginx配置文件


  4. ---------------------------------------------------------------------------


  5. vim test_nginxvars.yaml

  6. ---

  7. - hosts: all

  8. gather_facts: True #开启系统内置变量

  9. tasks:

  10. - name: nginx conf

  11. template: src=/tmp/nginx.j2 dest=/usr/local/nginx/conf/nginx.conf

  12. notify:

  13. - reload nginx #下发通知给handlers模块执行名字叫做reload nginx的动作

  14. handlers: #定义动作

  15. - name: reload nginx #动作的名字

  16. shell: /usr/local/nginx/sbin/nginx -s reload


  17. ansible-playbook test_nginxvars.yaml (执行分发命令)

 

nginx配置下发并检测

 
  1. ---

  2. - hosts: all

  3. tasks:

  4. - name: copy nginx.conf

  5. copy: src=/tmp/nginx.conf dest=/usr/local/nginx/conf/ backup=yes

  6. - name:

  7. shell: /usr/local/nginx/sbin/nginx -t

  8. register: nginx_result

  9. - debug: var=nginx_result

 

ansible的script模块批量运行脚本

 
  1. #操作示例-->远程批量分发并自动部署nginx

  2. #所有被管理端需要挂载光盘,并创建本地yum配置文件



  3. auto_nginx.sh #自动安装nginx脚本

  4. fenfa.sh #批量分发脚本

  5. nginx-1.10.2.tar.gz #nginx源码包



  6. #!/bin/sh

  7. #nginx install shell scripts

  8. test -/media/cdrom || mkdir -/media/cdrom

  9. mount /dev/sr0 /media/cdrom &>/dev/null

  10. yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel &>/dev/null

  11. test -/service/scripts || exit 3

  12. cd /service/scripts/

  13. tar xf nginx-1.10.2.tar.gz -/usr/src/

  14. cd /usr/src/nginx-1.10.2/

  15. ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module &>/dev/null

  16. make &>/dev/null

  17. make install &>/dev/null

  18. exit 0



  19. #源码包和安装脚本的批量分发脚本



  20. #!/bin/sh

  21. #批量分发脚本

  22. Group=$1

  23. ansible $Group -m copy -"src=/service/scripts/ dest=/service/scripts/"

  24. ansible $Group -m script -"/service/scripts/auto_nginx.sh"

 

ansible批量管理企业案例

4.1 ansible all -m copy -a 'src=/root/xin.sh dest=/root'

4.2 ansible all -m user -a 'name=yunjisuan shell=/sbin/nologin createhome=no'

4.3 ansible all -m shell -a 'echo "123123" | pass --stdin root'

4.4 ansible all -m user -a 'name=benet password=321321 shell=/bin/bash'

4.5 ansible all -m cron -a 'job="/bin/echo xin > /tmp/sl" name="test cron job"'

4.6 ansible all -m shell -a 'vmstat'

image_1cqeabap11fkl18uk14c6m5618qs9.png-227.6kB


    浏览1 评论0
    返回
    目录
    返回
    首页
    Docker Swarm容器集群管理 容器服务的监控,注册与发现

    发表评论