自动化运维专题(一):Ansible批量自动化管理工具入门

Jenkins自动化运维专题
—私人课件,不公开,不出版,禁止传播
想做好运维工作,人先要学会勤快;
居安而思危,勤记而补拙,方可不断提高;
别人资料不论你用着再如何爽那也是别人的;
自己总结东西是你自身特有的一种思想与理念的展现;
精髓不是看出来的,精髓是记出来的;
请同学们在学习的过程中养成好的学习习惯;
勤于实践,抛弃教案,勤于动手,整理文档。

一,Ansible概述

  • 由于互联网的快速发展导致产品更新换代速度逐步增长,运维人员每天都要进行大量的维护操作,按照传统方式进行维护使得工作效率低下。这时部署自动化运维工具就可以尽可能安全,高效的完成这些工作。
  • Ansible是基于Python开发,集合了众多优秀运维工具的优点,实现了批量运行命令,部署程序,配置系统等功能的自动化运维管理工具。默认通过SSH协议进行远程命令执行或下发配置,无需部署任何客户端代理软件,从而使得自动化环境部署变得更加简单。可同时支持多台主机并行管理,使得管理主机更加敏捷。
  • Ansible可以看作是一种基于模块进行工作的框架结构,批量部署能力就是由Ansible所运行的模块实现的。简单说Ansible是基于“模块”完成各种任务的。

image.png

二,ansible服务的安装和部署

2.1 实验环境

主机名 IP地址 备注
ansible 192.168.200.183 管理服务器
Web01 192.168.200.184 被管理主机
Web02 192.168.200.185 被管理主机
  1. #
  2. [root@localhost~]#cat/etc/redhat-release
  3. CentOSLinuxrelease7.5.1804(Core)
  4. [root@localhost~]#uname-r
  5. 3.10.0-862.3.3.el7.x86_64
  6. [root@localhost~]#systemctl stop firewalld
  7. [root@localhost~]#systemctl disable firewalld
  8. [root@localhost~]#systemctl stopNetworkManager
  9. [root@localhost~]#systemctl disableNetworkManager
  10. 通过yum源方式安装ansible

  11. [root@ansible~]#yum-y install epel-release
  12. [root@ansible~]#yum-y install ansible
  13. 通过Python的pip方式安装ansible

  14. [root@ansible~]#yum-y install epel-release
  15. [root@ansible~]#yum-y install python2-pip
  16. [root@ansible~]#pip install ansible

yum源
#cd /etc/yum.repos.d/
#vim /etc/yum.repos.d/aliBase.repo
#以下是配置yum源
[Base]
name=aliBase
baseurl=https://mirrors.aliyun.com/centos/$releasever/os/$basearch/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-$releasever
# cat aliEpel.repo
[aliEpel]
name=aliEpel
baseurl=https://mirrors.aliyun.com/epel/$releasever\Server/$basearch/
enabled=1
gpgcheck=0

2.2 生产环境Ansible管理服务器ssh登陆安全策略

(1)生产环境ssh登陆策略
备份:cp /etc/ssh/sshd_config{,.bak}

  1. [root@www~]#cat-n/etc/ssh/sshd_config.bak|sed-n’17p;38p;43p;47p;65p;79p;115p’
  2. 17#Port 22 #修改ssh连接端口
  3. 38#PermitRootLogin yes #是否允许root账号远程登陆
  4. 43#PubkeyAuthentication yes #是否开启公钥连接认证
  5. 47AuthorizedKeysFile.ssh/authorized_keys#公钥文件的放置位置
  6. 65PasswordAuthenticationyes#是否开启密码验证登陆
  7. 79GSSAPIAuthenticationyes#是否关闭GSSAPI认证
  8. 115#UseDNS yes #是否关闭DNS反向解析
  9. [root@www~]#cat-n/etc/ssh/sshd_config|sed-n’17p;38p;43p;47p;65p;79p;115p’
  10. 17Port22221#工作中需要设定到1万以上的端口,避免被扫描出来。
  11. 38PermitRootLoginyes#如果不是超大规模的服务器,为了方便我们可以暂时开启root远程登录
  12. 43PubkeyAuthenticationyes#开启公钥认证模式
  13. 47AuthorizedKeysFile.ssh/authorized_keys#公钥放置位置
  14. 65PasswordAuthenticationno#为了安全我们关闭服务器的密码认证方式
  15. 79GSSAPIAuthenticationno#关闭GSSAPI认证,极大提高ssh连接速度
  16. 115UseDNSno#关闭DNS反向解析,极大提高ssh连接速度

(2)设置xshell私钥登陆Linux

  1. 查看服务器端IP

  2. [root@ansible.ssh]#hostname-I
  3. 192.168.200.183
  4. 在Linux服务器端生成rsa密钥对

  5. [root@ansible~]#ssh-keygen#一直回车
  6. Generatingpublic/private rsa key pair.
  7. Enterfileinwhich to save the key(/root/.ssh/id_rsa):
  8. Enterpassphrase(emptyforno passphrase):
  9. Entersame passphrase again:
  10. Youridentification has been savedin/root/.ssh/id_rsa.
  11. Yourpublic key has been savedin/root/.ssh/id_rsa.pub.
  12. Thekey fingerprint is:
  13. SHA256:royhAEKx9bhe4jLZ3SzfZ/yvhkzPgToDIx+1gSxoOLMroot@www
  14. Thekeys randomart image is:
  15. +—-[RSA2048]——+
  16. |..|
  17. |+o|
  18. |o……|
  19. |.+o..o o|
  20. |o=o..S o.|
  21. |oE=+.o=.o.|
  22. |.++.ooo==+.|
  23. |.o.+oo.+*+|
  24. |..o..=ooo.|
  25. +——[SHA256]——-+
  26. 将生成的公钥导入到服务器端的~/.ssh/authorized_keys文件里

  27. [root@ansible~]#cd.ssh/
  28. [root@ansible.ssh]#ls
  29. id_rsa id_rsa.pub
  30. [root@ansible.ssh]#cat id_rsa.pub>authorized_keys
  31. [root@ansible.ssh]#chmod600authorized_keys#权限必须600否则不生效
  32. [root@ansible.ssh]#cat authorized_keys
  33. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDS7U4vgjCpWrMFwnWjUlrebldvPw5NNQpnyGT/1cTsyI6ryPm19J+IQ2wNn67BpYz0NKyLjq/hYlSxlQmD7xHwNM5KQirUYPgwPhhDqGuNE+UrBZ2lUkknt358YWGpEC+TUPy/MLNbnIepPpZr0y0qyXmtp7KpeXJwLeKLzZLpHnzA8Vr3A7w/jNaDnQJmKYvDvD0Q6O54CVkkSdxaYPAT1hVfX1pKz0dSNQbJpl5ZJXigQo26M+7qYXeUBxI5Guaapl6uT5sySzTBwwd9Yt49NKE/kIivClegVfHPGF4iSqfCiCd2BTJGTuCVBS2j4lhrjTLyWRO8po7BM4yImRGf root@www
  34. 将私钥文件id_rsa复制一份改名为rd_rsa_root并导出到宿主机桌面上

  35. [root@ansible.ssh]#ls
  36. id_rsa #私钥
  37. id_rsa.pub #公钥
  38. authorized_keys id_rsa id_rsa.pub
  39. [root@ansible.ssh]#cp id_rsa id_rsa_root
  40. [root@ansible.ssh]#ls
  41. authorized_keys id_rsa.pub id_rsa id_rsa_root

查看导入到桌面上的私钥文件
image.png
image.png
image.png
image.png
image.png
而后xshell显示登陆成功!
三:向SELinux中添加修改的SSH端口
在网上很多的修改ssh默认端口号的教程中都是只说到上面那一部分然后重新启动ssh服务,我是不知道他怎么实现的啊,反正我是怎么也实现不了,不管怎样配置都不起作用,ssh端口号一直是22,最后我发现了,这是由于我们需要向SELinux中添加修改的SSH端口
1:安装semanage
semanage是SELinux的管理工具,是用于向SELinux添加和修改ssh端口号
#安装依赖
yuminstallpolicycoreutils-python
#安装semanage
yum provides semanage
2:使用semanage向SELinux中添加我们刚刚添加的端口号(22221)
查询当前 ssh 服务端口:
semanage port -l| grep ssh

验证 ssh 端口是否添加成功:
semanage port -l| grep ssh
重启 ssh 服务:
systemctlrestartsshd.service
(2)用户权限策略
在生产环境中,如果遇到禁止root用户远程登录系统,授权仅普通用户登陆系统,那么需要管理员权限执行sudo提权即可,避免root用户之间登陆

  1. 创建一个普通用户yunjisuan

  2. [root@ansible~]#useradd yunjisuan
  3. [root@ansible~]#echo”123123”|passwd—stdin yunjisuan
  4. 更改用户yunjisuan的密码。
  5. passwd:所有的身份验证令牌已经成功更新。
  6. [root@server-2 ~]# passwd gongwenhua
  7. 给普通用户修改密码

  8. 以root账号授权普通用户yunjisuan所有权限并免输入密码

  9. [root@ansible~]#sed-n’93p’/etc/sudoers
  10. yunjisuan ALL=(ALL)ALL
  11. 切换到yunjisuan用户测试提权

  12. [root@ansible~]#su-yunjisuan
  13. [yunjisuan@ansible~]$ sudo-l
  14. 我们信任您已经从系统管理员那里了解了日常注意事项。
  15. 总结起来无外乎这三点:
  16. 1) 尊重别人的隐私。

  17. 2) 输入前要先考虑(后果和风险)。

  18. 3) 权力越大,责任越大。

  19. [sudo]yunjisuan的密码:
  20. 匹配%2$s上%1$s的默认条目:
  21. !visiblepw,always_set_home,match_group_by_gid,env_reset,env_keep=”COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS”,
  22. env_keep+=”MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE”,env_keep+=”LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES”,
  23. env_keep+=”LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE”,env_keep+=”LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY”,
  24. secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin
  25. 用户yunjisuan可以在ansible上运行以下命令:
  26. (ALL)ALL
  27. 测试提权

  28. [yunjisuan@ansible~]$ ls/root
  29. ls:无法打开目录/root:权限不够
  30. [yunjisuan@ansible~]$ sudo ls/root
  31. anaconda-ks.cfg

(3)配置xshell远程密钥登陆服务器端普通用户

  1. 给yunjisuan普通用户创建公钥认证。注意权限。权限过大,公钥验证会失败

  2. [root@ansible~]#mkdir-p/home/yunjisuan/.ssh
  3. [root@ansible~]#chmod700/home/yunjisuan/.ssh
  4. [root@ansible~]#chown yunjisuan.yunjisuan/home/yunjisuan/.ssh
  5. [root@ansible~]#cp~/.ssh/authorized_keys/home/yunjisuan/.ssh/
  6. [root@ansible~]#chmod600/home/yunjisuan/.ssh/authorized_keys
  7. [root@ansible~]#chown yunjisuan.yunjisuan/home/yunjisuan/.ssh/authorized_keys

然后我们xshell远程登陆普通用户到Ansible服务器端
xshell还是用刚才导入的那个私钥文件即可。
image.png
image.png
然后就登陆成功了。
最后我们关闭Ansible管理服务器端的root账号SSH远程登录功能即可。

  1. 在生产环境我们一般是要禁止服务器root账号远程登录功能的(一旦关闭,密钥和密码登陆方式都不能再登陆)

  2. 如果我们想用root账号进行操作,那么远程密钥连接普通用户在切换成root账号即可

  3. yunjisuan@ansible~]$ sudo su-
  4. [sudo]yunjisuan的密码:
  5. 上一次登录:日9月921:01:31CST2018从192.168.200.1pts/1上
  6. [root@ansible~]#
  7. 关闭Ansible管理服务器的root账号SSH远程登录功能

  8. [root@ansible~]#sed-n’38p’/etc/ssh/sshd_config
  9. PermitRootLoginno
  10. 重启动sshd服务

  11. [root@ansible~]#systemctl restart sshd

(4)配置Ansible管理服务器sudo审计日志
Centos6.x和Centos7.x的配置方法相同,rsyslog服务是所有日志记录的服务进程

  1. 开启sudo日志

  2. [root@ansible~]#echo”local2.debug /var/log/sudo.log”>>/etc/rsyslog.conf
  3. [root@ansible~]#echo”Defaults logfile=/var/log/sudo.log”>>/etc/sudoers
  4. [root@ansible~]#systemctl restart rsyslog
  5. 测试sudo日志记录

  6. [root@ansible~]#exit
  7. 登出
  8. [yunjisuan@ansible~]$ sudo su-
  9. [sudo]yunjisuan的密码:
  10. 上一次登录:日9月921:40:11CST2018pts/0上
  11. 查看/var/log/sudo.log日志

  12. [root@ansible~]#cat/var/log/sudo.log
  13. Sep921:49:12:yunjisuan:TTY=pts/0;PWD=/home/yunjisuan;USER=root;
  14. COMMAND=/bin/su-

    2.3 安装ansible

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

  16. [root@ansible~]#yum-y install ansible
  17. [root@ansible~]#ansible—version
  18. ansible2.6.3
  19. config file=/etc/ansible/ansible.cfg
  20. configuredmodulesearch path=[u’/root/.ansible/plugins/modules’,u’/usr/share/ansible/plugins/modules’]
  21. ansible pythonmodulelocation=/usr/lib/python2.7/site-packages/ansible
  22. executable location=/usr/bin/ansible
  23. python version=2.7.5(default,Apr112018,07:36:10)[GCC4.8.520150623(RedHat4.8.5-28)]

    2.4 配置主机清单

    /etc/ansible/hosts文件中可以定义被管理主机,Ansible通过读取/etc/ansible/hosts文件内定义的主机清单批量做一些操作。比如定义一个nginx组,包含一台主机Web01,再定义一个apache组,包含另一台主机Web02.

  24. [root@ansible~]#cat/etc/ansible/hosts

  25. [nginx]
  26. Web01ansible_ssh_host=192.168.200.184
  27. Web02ansible_ssh_host=192.168.200.185
  28. 说明:
  29. ansible_ssh_host:被管理主机IP
  30. ansible_ssh_user:被管理主机用户名
  31. ansible_ssh_pass:被管理主机用户的登陆密码
  32. ansible_sudo_pass:被管理主机用户sudo时的密码

    2.5 设置SSH免密码登陆

    为了避免Ansible下发指令时需要输入被管理主机的密码,可以通过证书签名达到SSH无密码登陆。使用ssh-keygen产生一对密钥,并通过ssh-copy-id命令来发送生成的公钥。

  33. [root@ansible~]#ls~/.ssh/

  34. authorized_keys id_rsa id_rsa.pub
  35. [root@ansible~]#ssh-copy-id192.168.200.184
  36. [root@ansible~]#ssh-copy-id192.168.200.185

当然,我们也可以在控制端主机的hosts文件里直接写入连接方式,用户,密码也能下发指令。但是生产环境不建议这么做。因为这样明文密码容易泄露,另外如果被控制主机修改了密码,这里也需要一起更改,不便于管理。

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

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

  2. [root@ansible~]#ll/etc/hosts
  3. -rw-r—r—.1root root1809月900:38/etc/hosts
  4. [root@ansible~]#chmod600/etc/hosts
  5. 禁止非root用户查看Ansible的主机清单配置文件

  6. [root@ansible~]#ll/etc/ansible/hosts
  7. -rw-r—r—1root root879月921:59/etc/ansible/hosts
  8. [root@ansible~]#chmod600/etc/ansible/hosts

    2.7 ansible查看帮助

    [root@ansible ~]# /usr/local/python/bin/ansible-doc -l 查看总帮助
    [root@ansible ~]# /usr/local/python/bin/ansible-doc -s shell 查看shell模块的帮助

    三,ansible的基础应用

    Ansible可以使用命令行的方式进行自动化管理。命令的基本语法如下所示:

  9. ansible<被操控的主机或主机组或all>[-m模块名][-a具体命令]

  10. 说明:
  11. 主机组名====>/etc/ansible/hosts里设定的nginx,apache,web
  12. 主机名====>Web01,Web02
  13. all====>/etc/ansible/hosts里设定的所有主机
  14. 模块名====>command,cron,shell,file等

    3.1 ping模块

    Ansible中使用ping模块来检测指定主机的连通性

  15. 测试单主机

  16. [root@ansible~]#ansibleWeb01-m ping
  17. Web01|SUCCESS=>{
  18. “changed”:false,
  19. “ping”:”pong”
  20. }
  21. 测试单主机

  22. [root@ansible~]#ansibleWeb02-m ping
  23. Web02|SUCCESS=>{
  24. “changed”:false,
  25. “ping”:”pong”
  26. }
  27. 测试主机组

  28. [root@ansible~]#ansible nginx-m ping
  29. Web02|SUCCESS=>{
  30. “changed”:false,
  31. “ping”:”pong”
  32. }
  33. Web01|SUCCESS=>{
  34. “changed”:false,
  35. “ping”:”pong”
  36. }
  37. 测试所有的被管理主机

  38. [root@ansible~]#ansible all-m ping
  39. Web02|SUCCESS=>{
  40. “changed”:false,
  41. “ping”:”pong”
  42. }
  43. Web01|SUCCESS=>{
  44. “changed”:false,
  45. “ping”:”pong”
  46. }

    3.2 command模块

    在远程主机执行命令,不支持管道符和重定向等复杂命令,可完全被shell模块替代

  47. [root@ansible~]#ansibleWeb01-m command-a’uptime’

  48. Web01|SUCCESS|rc=0>>
  49. 22:14:43up9:43,3users,load average:0.00,0.01,0.05
  50. [root@ansible~]#ansibleWeb01-m command-a’ls’
  51. Web01|SUCCESS|rc=0>>
  52. anaconda-ks.cfg

    3.3 shell模块

    Ansible中的shell模块可以在被管理主机上运行命令,并支持像管道符重定向这样的复杂命令。

  53. 在Web01上创建用户yunjisuan,并非交互方式设定密码

  54. [root@ansible~]#ansibleWeb01-m shell-a’useradd yunjisuan’
  55. Web01|SUCCESS|rc=0>>
  56. [root@ansible~]#ansibleWeb01-m shell-a’echo 123123 | passwd —stdin yunjisuan’
  57. Web01|SUCCESS|rc=0>>
  58. 更改用户yunjisuan的密码。
  59. passwd:所有的身份验证令牌已经成功更新。
  60. [root@ansible~]#ansibleWeb01-m shell-a’id yunjisuan’
  61. Web01|SUCCESS|rc=0>>
  62. uid=1000(yunjisuan)gid=1000(yunjisuan)组=1000(yunjisuan)
  63. [root@ansible~]#ansibleWeb01-m shell-a’tail -1 /etc/shadow’
  64. Web01|SUCCESS|rc=0>>
  65. yunjisuan:$6$4y7c1tkV$oPZW0psDdAzJp5RomBrOpSlTuvsdQ/5JaBYHU.LOPsYQ0o7EpPFRMuh/X9ruwcmBcZbN.l/glBTfDKm//jJP60:17782:0:99999:7:::
  66. 在所有被管理的主机的/etc/hosts文件里添加Ansible管理服务器的IP地址映射

  67. [root@ansible~]#ansible all-m shell-a’echo “ansible 192.168.200.183” >> /etc/hosts’
  68. Web02|SUCCESS|rc=0>>
  69. Web01|SUCCESS|rc=0>>
  70. [root@ansible~]#ansible all-m shell-a’tail -1 /etc/hosts’
  71. Web01|SUCCESS|rc=0>>
  72. ansible192.168.200.183
  73. Web02|SUCCESS|rc=0>>
  74. ansible192.168.200.183

    3.4 cron模块

    Ansible中的cron模块用于定义任务计划。主要包括两种状态(state);
  • crontab时间周期:
    • minute:分钟
    • hour:小时
    • day:日期
    • month:月份
    • weekday:周期
  • crontab任务:
    • job:指明运行的命令是什么
  • crontab任务描述:
    • name:定时任务描述(定时任务清除的依据)
  • state状态:
    • present:表示添加(省略状态时默认使用);
    • absent:表示移除;
  • crontab任务的用户身份:
    • user:指定定时任务以哪个用户身份执行
  1. 添加定时任务计划,在所有被管理的主机里每十分钟输出hello字符串,定时任务描述为test cron job

  2. [root@ansible~]#ansible all-m cron-a’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-a’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-a’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-a’crontab -l’
  37. Web02|SUCCESS|rc=0>>
  38. Web01|SUCCESS|rc=0>>
  39. 给Web01服务器上的普通用户yunjisuan添加一个定时任务

  40. [root@ansible~]#ansibleWeb01-m shell-a’id yunjisuan’
  41. Web01|SUCCESS|rc=0>>
  42. uid=1000(yunjisuan)gid=1000(yunjisuan)组=1000(yunjisuan)
  43. [root@ansible~]#ansibleWeb01-m cron-a’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~]#ansibleWeb01-m shell-a’crontab -u yunjisuan -l’
  52. Web01|SUCCESS|rc=0>>
  53. Ansible: yunjisuan cron job

  54. /10 * /bin/echo hello
  55. [root@ansible~]#ansibleWeb01-m cron-a’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~]#ansibleWeb01-m shell-a’crontab -u yunjisuan -l’
  62. Web01|SUCCESS|rc=0>>

    3.5 copy模块

    Ansible中的copy模块用于实现文件复制和批量下发文件。其中使用src来定义本地源文件路径;使用dest定义被管理主机文件路径;使用content则是使用指定信息内容来生成目标文件。

  63. 将本地的/etc/hosts文件拷贝到所有被管理的主机的/etc/hosts路径下覆盖同名文件,并指定属主和权限,若拷贝的文件与目标文件内容不同,则备份目标文件再覆盖。

  64. [root@ansible~]#ansible all-m shell-a’tail -1 /etc/hosts’
  65. Web01|SUCCESS|rc=0>>
  66. ansible192.168.200.183
  67. Web02|SUCCESS|rc=0>>
  68. ansible192.168.200.183
  69. [root@ansible~]#echo”web01 192.168.200.184”>>/etc/hosts
  70. [root@ansible~]#tail-1/etc/hosts
  71. web01192.168.200.184
  72. [root@ansible~]#ansible all-m copy-a’src=/etc/hosts dest=/etc/hosts owner=root mode=640 backup=yes’
  73. Web01|SUCCESS=>{
  74. “backup_file”:”/etc/hosts.13083.2018-09-09@00:38:35~”,
  75. “changed”:true,
  76. “checksum”:”80244bc6f9638a3505aae1a2bcf2228e69a00420”,
  77. “dest”:”/etc/hosts”,
  78. “gid”:0,
  79. “group”:”root”,
  80. “md5sum”:”de48f3cf45d11215fa7cfd0d558be954”,
  81. “mode”:”0640”,
  82. “owner”:”root”,
  83. “size”:180,
  84. “src”:”/root/.ansible/tmp/ansible-tmp-1536424715.44-39054260112988/source”,
  85. “state”:”file”,
  86. “uid”:0
  87. }
  88. Web02|SUCCESS=>{
  89. “backup_file”:”/etc/hosts.12643.2018-09-09@00:38:36~”,
  90. “changed”:true,
  91. “checksum”:”80244bc6f9638a3505aae1a2bcf2228e69a00420”,
  92. “dest”:”/etc/hosts”,
  93. “gid”:0,
  94. “group”:”root”,
  95. “md5sum”:”de48f3cf45d11215fa7cfd0d558be954”,
  96. “mode”:”0640”,
  97. “owner”:”root”,
  98. “size”:180,
  99. “src”:”/root/.ansible/tmp/ansible-tmp-1536424715.45-15307684711466/source”,
  100. “state”:”file”,
  101. “uid”:0
  102. }
  103. [root@ansible~]#ansible all-m shell-a’tail -1 /etc/hosts’
  104. Web02|SUCCESS|rc=0>>
  105. web01192.168.200.184
  106. Web01|SUCCESS|rc=0>>
  107. web01192.168.200.184
  108. [root@ansible~]#ansible all-m shell-a’ls /etc/hosts*’
  109. Web01|SUCCESS|rc=0>>
  110. /etc/hosts
  111. /etc/hosts.13083.2018-09-09@00:38:35~#这就是备份的文件
  112. /etc/hosts.allow
  113. /etc/hosts.deny
  114. Web02|SUCCESS|rc=0>>
  115. /etc/hosts
  116. /etc/hosts.12643.2018-09-09@00:38:36~#这就是备份的文件
  117. /etc/hosts.allow
  118. /etc/hosts.deny
  119. 将本地/tmp/test.sh的脚本复制到远程主机上并远程激活

  120. [root@ansible~]#cat/tmp/test.sh
  121. !/bin/bash

  122. echo”welcome to yunjisuan”
  123. [root@ansible~]#ansible all-m copy-a’src=/tmp/test.sh dest=/tmp owner=root mode=500’
  124. Web01|SUCCESS=>{
  125. “changed”:true,
  126. “checksum”:”70ae837e7367f5d4de9a3197709639ae14743000”,
  127. “dest”:”/tmp/test.sh”,
  128. “gid”:0,
  129. “group”:”root”,
  130. “md5sum”:”5ff4338de7d9ff0ded9fa3e0ecd15bab”,
  131. “mode”:”0500”,
  132. “owner”:”root”,
  133. “size”:41,
  134. “src”:”/root/.ansible/tmp/ansible-tmp-1536425049.22-149074464676784/source”,
  135. “state”:”file”,
  136. “uid”:0
  137. }
  138. Web02|SUCCESS=>{
  139. “changed”:true,
  140. “checksum”:”70ae837e7367f5d4de9a3197709639ae14743000”,
  141. “dest”:”/tmp/test.sh”,
  142. “gid”:0,
  143. “group”:”root”,
  144. “md5sum”:”5ff4338de7d9ff0ded9fa3e0ecd15bab”,
  145. “mode”:”0500”,
  146. “owner”:”root”,
  147. “size”:41,
  148. “src”:”/root/.ansible/tmp/ansible-tmp-1536425049.23-32532320097185/source”,
  149. “state”:”file”,
  150. “uid”:0
  151. }
  152. [root@ansible~]#ansible all-m shell-a’/tmp/test.sh’
  153. Web01|SUCCESS|rc=0>>
  154. welcome to yunjisuan
  155. Web02|SUCCESS|rc=0>>
  156. welcome to yunjisuan

    3.6 script模块

    Ansible中的script模块可以将本地脚本复制到被管理主机的内存中并运行,不会在被管理主机中留下脚本文件。

  157. 编写一个脚本,然后通过ansible的script模块远程向被管理主机执行此脚本

  158. [root@ansible~]#echo’echo “1111” >> /tmp/test’>>/tmp/test.sh
  159. [root@ansible~]#cat/tmp/test.sh
  160. echo”1111”>>/tmp/test
  161. [root@ansible~]#ansible all-m script-a’/tmp/test.sh’
  162. Web01|SUCCESS=>{
  163. “changed”:true,
  164. “rc”:0,
  165. “stderr”:”Shared connection to 192.168.200.184 closed.\r\n”,
  166. “stderr_lines”:[
  167. “Shared connection to 192.168.200.184 closed.”
  168. ],
  169. “stdout”:””,
  170. “stdout_lines”:[]
  171. }
  172. Web02|SUCCESS=>{
  173. “changed”:true,
  174. “rc”:0,
  175. “stderr”:”Shared connection to 192.168.200.185 closed.\r\n”,
  176. “stderr_lines”:[
  177. “Shared connection to 192.168.200.185 closed.”
  178. ],
  179. “stdout”:””,
  180. “stdout_lines”:[]
  181. }
  182. [root@ansible~]#ansible all-m shell-a’cat /tmp/test’
  183. Web02|SUCCESS|rc=0>>
  184. 1111
  185. Web01|SUCCESS|rc=0>>
  186. 1111

    3.7 yum模块

    利用yum模块安装软件包,虽然能被shell模块替代
    但是用yum模块更显专业一些
  • 软件包名:
    • name:指定软件包的名字
  • state状态:
    • present:安装软件包(默认就是这个)
    • absent:卸载软件包
  1. 安装nmap软件包

  2. [root@ansible~]#ansible all-m yum-a’name=nmap’
  3. 卸载nmap软件包

  4. [root@ansible~]#ansible all-m yum-a’name=nmap state=absent’

    3.8 service模块

    利用service模块管理服务程序,虽然能被shell模块替代
    但是用service模块更显专业一些
  • 服务名称:
    • name:指定服务的名字
  • state状态:
    • started:启动服务
    • stopped:停止服务
    • restarted:重启服务
    • reloaded:平滑重载
  • enabled开机自启动:
    • true:设置开机自启动
    • false:设置开启不启动
  1. 启动firewalld并设置开机自启动

  2. [root@ansible~]#ansibleWeb01-m service-a’name=firewalld state=started enabled=true’
  3. 关闭firewalld并设置开机不启动

  4. [root@ansible~]#ansibleWeb01-m service-a’name=firewalld state=stopped enabled=false’

    3.9 user模块

    用户管理模块。管理用户账号
  • :指定用户名
    • name:指定操作的用户的名字
  • :用户描述
    • comment:指定用户的描述信息
  • :createhome:是否创建家目录
  • :uid:指定用户的uid号
  • :groups:指定用户的附加组(默认创建和用户名相同的组)
  • :password:指定用户的密码
  • :update_password:更新用户的密码
  • :shell指定用户的登陆方式
    • /bin/bash:能登录系统
    • /sbin/nologin:不能登录系统
  • :home:指定用户的家目录路径
  • :state状态:
    • present:创建用户(默认就是这个)
    • absent:删除用户
  • :remove:当指定state=absent时,确认是否删除用户家目录
    • true
    • false
  1. 在Web02上创建一个普通用户yunjisuan,并设置用户的密码为123123

  2. [root@ansible~]#ansibleWeb02-m user-a’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~]#ansibleWeb02-m shell-a’tail -1 /etc/passwd’
  18. Web02|SUCCESS|rc=0>>
  19. yunjisuan:x:1066:1066:welcom to yunjisuan:/home/yunjisuan:/bin/bash
  20. [root@ansible~]#ansibleWeb02-m shell-a’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-c”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~]#ansibleWeb02-m user-a’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~]#ansibleWeb02-m user-a’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::: #终于密文了

    3.10 setup模块

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

  23. 查看远程主机的facts信息

  24. [root@ansible~]#ansibleWeb01-m setup|head
  25. Web01|SUCCESS=>{
  26. “ansible_facts”:{
  27. “ansible_all_ipv4_addresses”:[
  28. “192.168.200.184”
  29. ],
  30. “ansible_all_ipv6_addresses”:[
  31. “fe80::20c:29ff:fe77:16ad”
  32. ],
  33. “ansible_apparmor”:{
  34. “status”:”disabled”

    四,作业与思考:Ansible批量管理企业应用案例

    在工作中我们如何利用ansible进行服务器的批量管理和维护?

    4.1 利用ansible批量下发文件,脚本和目录

    4.2 利用ansible给多台服务器批量创建程序用户

    4.3 利用ansible给多台服务器批量修改root密码

    4.4 利用ansible给多台服务器创建普通登陆用户并设置初始密码及sudo授权

    4.5 利用ansible给多台服务器的普通用户下发定时任务

    4.6 利用ansible批量获取多台服务器的CPU,MEM,IO信息

    4.7 利用ansible对远程服务器进行批量系统初始优化及服务器基本安全加固

    +