项目作者: loong576

项目描述 :
ansible生产环境使用场景(二)
高级语言: Shell
项目地址: git://github.com/loong576/ansible-production-practice-2.git
创建时间: 2020-08-01T08:58:23Z
项目社区:https://github.com/loong576/ansible-production-practice-2

开源协议:

下载


前言:

​ 生产上新入网的服务器都需要安装prometheus的监控客户端软件,主要步骤有:新建监控用户、拷贝客户端软件、拉起客户端进程、开机自启动。本文记录了使用ansible的role方式批量快速的安装该客户端软件。

​ 本文使用到的主要模块:user、stat、copy、shell、script、lineinfile等。

环境说明:

主机名 操作系统版本 ip ansible version 备注
ansible Centos 7.6.1810 172.27.34.51 2.9.9 ansible管理服务器
ansible-awx Centos 7.6.1810 172.27.34.50 / 被管服务器

一、创建目录列表

  1. [root@ansible ~]# cd /etc/ansible/roles
  2. [root@ansible roles]# ansible-galaxy init prometheus
  3. - Role prometheus was created successfully
  4. [root@ansible roles]# tree prometheus
  5. prometheus
  6. ├── defaults
  7. └── main.yml
  8. ├── files
  9. ├── handlers
  10. └── main.yml
  11. ├── meta
  12. └── main.yml
  13. ├── README.md
  14. ├── tasks
  15. └── main.yml
  16. ├── templates
  17. ├── tests
  18. ├── inventory
  19. └── test.yml
  20. └── vars
  21. └── main.yml
  22. 8 directories, 8 files

image-20200801154532425

使用ansible-galaxy命令初始化role的目录

二、生成密码

1.安装pip3

  1. [root@ansible ~]# yum -y install python3-pip

2.安装passlib模块

  1. [root@ansible ~]# cd /tmp
  2. [root@ansible tmp]# pip3 download passlib==1.7.2 -d /tmp/pkg
  3. [root@ansible tmp]# more requirements.txt
  4. passlib==1.7.2
  5. [root@ansible tmp]# pip3 install --no-index --find-links=./pkg -r requirements.txt
  6. WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
  7. Collecting passlib==1.7.2 (from -r requirements.txt (line 1))
  8. Installing collected packages: passlib
  9. Successfully installed passlib-1.7.2

生产密码会使用到Python的passlib模块

3.生成密码密文

  1. [root@ansible ~]# python3 -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
  2. Password:
  3. $6$irgqm/Fea6/O07B7$LJpYtZoKqUkF.pN4D71LX2Cac3TNrF2.1GKGLfaSWxvKupknNLbWNcYym3LuojT3BqUeUCgsrmD/M6FqTx4lK/

image-20200801155016968

输入明文密码会生成密码密文,复制该密码,后面创建用户时会用到。

三、文件总览

1.执行文件

  1. [root@ansible ansible]# pwd
  2. /etc/ansible
  3. [root@ansible ansible]# more prometheus.yaml
  4. ---
  5. - hosts: "{{ hostlist }}"
  6. gather_facts: no
  7. roles:
  8. - role: prometheus

2.task文件

  1. [root@ansible ~]# cd /etc/ansible/roles
  2. [root@ansible roles]# more prometheus/tasks/main.yml
  3. ---
  4. # tasks file for prometheus
  5. # author: loong576
  6. - name: user search
  7. shell: id {{ user_name }}
  8. register: user_search
  9. ignore_errors: true
  10. - name: user add
  11. user:
  12. name: "{{ user_name }}"
  13. shell: "{{ user_bash }}"
  14. password: "{{ user_password }}"
  15. when: user_search.failed == true
  16. - name: file search
  17. stat:
  18. path: "{{ file_dest }}/{{ file_src }}"
  19. register: file_search
  20. - name: copy files
  21. copy:
  22. src: "{{ file_src }}"
  23. dest: "{{ file_dest }}"
  24. mode: 0755
  25. when: file_search.stat.exists == false
  26. - name: process search
  27. shell: "ps -ef|grep node_exporter |grep -v grep"
  28. register: process
  29. ignore_errors: true
  30. - name: install node_exporter
  31. environment:
  32. dest: "{{ file_dest }}"
  33. src: "{{ file_src }}"
  34. port: "{{ node_port }}"
  35. script:
  36. startup.sh
  37. register: start
  38. tags: start
  39. when: process.failed == true
  40. - name: exec when startup
  41. lineinfile:
  42. dest: /etc/rc.local
  43. line: nohup {{ file_dest }}/{{ file_src }} --web.listen-address=:{{ node_port }} >/dev/null &

执行逻辑为:判断被执行主机上有无监控用户,若无则新增;判断被执行主机有无客户端文件,若无则拷贝;判断被执行主机有无客户端进程,若无则拉起;最后设置客户端进程开机自启动。

3.default文件

  1. [root@ansible roles]# more prometheus/defaults/main.yml
  2. ---
  3. # defaults file for prometheus
  4. user_name: sysmonitor
  5. user_bash: /bin/bash
  6. user_password: $6$bB7R8JF3U7L7s/3E$fKOQwpoZ7RESfMmX6uqts1gw4yeXniRNctI2JRBRS2/120EgrHCWS3DboiRhO5sN0CjoVxvtAKgeDVQRaPlc0/
  7. file_src: node_exporter
  8. file_dest: /home/sysmonitor
  9. node_port: 9100

定义监控用户的用户名、shell、密码,客户端执行文件的文件名、文件路径和端口。

4.file文件

  1. [root@ansible roles]# ll prometheus/files/
  2. 总用量 16512
  3. -rw-r--r-- 1 root root 16900416 7 30 16:04 node_exporter
  4. -rwxr--r-- 1 root root 102 7 31 11:32 startup.sh
  5. [root@ansible roles]# more prometheus/files/startup.sh
  6. #/bin/bash
  7. echo $dest
  8. echo $src
  9. echo $port
  10. nohup $dest/$src --web.listen-address=:$port >/dev/null &

file文件有两个,node_exporter为客户端执行文件,startup.sh为客户端进程拉起脚本。

四、运行role

  1. [root@ansible ansible]# pwd
  2. /etc/ansible
  3. [root@ansible ansible]# ansible-playbook prometheus.yaml -e hostlist=test50

image-20200801162837735

‘ -e hostlist=test50’指定被执行的主机为test50,即172.27.34.50

五、运行结果复核

image-20200801162918444

登陆被管主机test50,发现监控用户和监控进程都在且加入到了开机自启动文件中,符合预期。