ansible自动化管理

(30) 2024-04-25 11:01:01

ansible简介

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

实验环境
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# systemctl stop NetworkManager
[root@localhost ~]# systemctl disable NetworkManager
#通过yum的方式安装ansible
[root@ansible ~]# yum -y install epel-release
[root@ansible ~]# yum -y install ansible
#通过Python的pip方式安装ansible
[root@ansible ~]# yum -y install epel-release
[root@ansible ~]# yum -y install python2-pip
[root@ansible ~]# pip install ansible
生产环境Ansible管理服务器ssh登陆安全策略
[root@zhuxinwang ~]# cat -n  /etc/ssh/sshd_config.bak | sed -n '17p;38p;43p;47p;65p;79p;115p'
    17	#Port 22    		#修改ssh端口号
    38	#PermitRootLogin yes		 #是否允许root账号远程登录
    43	#PubkeyAuthentication yes  	#是否开启公钥认证
    47	AuthorizedKeysFile	.ssh/authorized_keys 		 #公钥文件的所在位置
    65	PasswordAuthentication yes  	#是否开启密码验证登录
    79	GSSAPIAuthentication yes  		#是否关闭GSSAPI认证
	115	#UseDNS yes  		#是否关闭dns反向解析
[root@zhuxinwang ~]# 
[root@zhuxinwang~]# cat -n /etc/ssh/sshd_config | sed -n '17p;38p;43p;47p;65p;79p;115p'
    17  Port 22221                  #nmap服务会检测端口10000以下端口,所以工作中需要设定到1万以上的端口,避免被扫描出来。
    38  PermitRootLogin yes         #如果不是超大规模的服务器,为了方便我们可以暂时开启root远程登录
    43  PubkeyAuthentication yes    #开启公钥认证模式
    47  AuthorizedKeysFile  .ssh/authorized_keys    #公钥放置位置
    65  PasswordAuthentication no   #为了安全我们关闭服务器的密码认证方式
    79  GSSAPIAuthentication no     #关闭GSSAPI认证,极大提高ssh连接速度
   115  UseDNS no                   #关闭DNS反向解析,极大提高ssh连接速度
设置xshell私钥登陆Linux
[root@zhuxinwang .ssh]# ssh-keygen 
Generating public/private rsa key pair.   #一直按住回车键
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:jZplM78ENd4uxJStOU+0/QxJmN8bL/YPIRKybvSc22E root@zhuxinwang
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|           o o   |
|        . * = .  |
|         X B = o |
|        S X = *..|
|       B O B . =+|
|      o o B E +.+|
|       . . * o + |
|          o .   +|
+----[SHA256]-----+
[root@zhuxinwang .ssh]# 

将生成的公钥导入到服务器端的~/.ssh/authorized_keys文件里
[root@zhuxinwang .ssh]# cat id_rsa.pub > authorized_keys
[root@zhuxinwang .ssh]# 
[root@zhuxinwang .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub
[root@zhuxinwang .ssh]# cat authorized_keys 
[root@zhuxinwang .ssh]# chmod 600 authorized_keys  #权限必须设置为600否则权限过大执行不了
ssh-rsa   #AAAAB3NzaC1yc2EAAAADAQABAAABAQC6LqAFrSsK2Ycik4ZPlH+fEpiefoDiXLohtpSN+YV6GZygu4tkqySicW546+W1rSGxL0OKLkdTh0JdBGxZrpYF71dG0RPWsWbqD+ex4U+Jwfeouk7MH2Mkpd5GSA3WMeMWjYR4kjyImRBjXDiupbkOoAlLCiXXgWj4c0V1Bb9B/NlZ8tQ9L2BN7/eKJ58u8oaTbLFv1bVShea4q22bou1yVbGimoGo6DNFdgt/Ulp+R221xcgm1L7EtAcW5qrgdFmJvr3RPmd7+ihqJ+1nlIjtu8EmdjC9qh/E4E9j4YeF/yFz/Om6sXKKb/VEKcMQ7J++txhGq6eFPCdo0oDzS1fT root@zhuxinwang
[root@zhuxinwang .ssh]# 

将私钥文件id_rsa复制一份改名为rd_rsa_zxw并导出到宿主机桌面上
[root@zhuxinwang .ssh]# cp id_rsa id_rsa_zxw

ansible自动化管理 (https://mushiming.com/)  第1张

然后打开xshell新建一个链接 ,然后点击用户身份验证

ansible自动化管理 (https://mushiming.com/)  第2张

用户权限策略
[root@zhuxinwang~]# useradd yunjisuan
[root@zhuxinwang ~]# echo "123123" | passwd --stdin yunjisuan
更改用户 yunjisuan 的密码 。
passwd:所有的身份验证令牌已经成功更新。
#以root账号授权普通用户yunjisuan所有权限并免输入密码
[root@zhuxinwang ~]# sed -n '93p' /etc/sudoers
yunjisuan   ALL=(ALL)   ALL
#切换到yunjisuan用户测试提权
[root@zhuxinwang ~]# su - yunjisuan
[yunjisuan@zhuxinwang ~]$ sudo -l
配置xshell远程密钥登录普通用户
[root@zhuxinwang ~]# mkdir -p /home/yunjisuan/.ssh
[root@zhuxinwang ~]# chmod 700 /home/yunjisuan/.ssh
[root@zhuxinwang ~]# chown yunjisuan.yunjisuan /home/yunjisuan/.ssh
[root@zhuxinwang ~]# cp ~/.ssh/authorized_keys /home/yunjisuan/.ssh/
[root@zhuxinwang ~]# chmod 600 /home/yunjisuan/.ssh/authorized_keys 
[root@zhuxinwang ~]# chown yunjisuan.yunjisuan /home/yunjisuan/.ssh/authorized_keys

#在生产环境我们一般是要禁止服务器root账号远程登录功能的(一旦关闭,密钥和密码登陆方式都不能再登陆)
#如果我们想用root账号进行操作,那么远程密钥连接普通用户在切换成root账号即可

(4)配置Ansible管理服务器sudo审计日志

Centos6.x和Centos7.x的配置方法相同,rsyslog服务是所有日志记录的服务进程

配置Ansible管理服务器sudo审计日志
开启sudo日志
[root@zhuxinwang ~]# echo "local2.debug /var/log/sudo.log" >> /etc/rsyslog.conf 
[root@zhuxinwang ~]# echo "Defaults logfile=/var/log/sudo.log" >> /etc/sudoers
[root@zhuxinwang ~]# systemctl restart rsyslog
#测试sudo日志记录
[root@zhuxinwang ~]# exit
登出
[yunjisuan@zhuxinwang ~]$ sudo su -
[sudo] yunjisuan 的密码:
上一次登录:日 9月  9 21:40:11 CST 2018pts/0 上
#查看/var/log/sudo.log日志
[root@zhuxinwang ~]# cat /var/log/sudo.log 
Sep  9 21:49:12 : yunjisuan : TTY=pts/0 ; PWD=/home/yunjisuan ; USER=root ;
    COMMAND=/bin/su -

我们信任您已经从系统管理员那里了解了日常注意事项。
总结起来无外乎这三点:
尊重别人的隐私。
输入前要先考虑(后果和风险)。
权力越大,责任越大。

安装ansible
[root@zhuxinwang ~]# yum -y install epel-release
[root@zhuxinwang ~]# yum -y install ansible
[root@zhuxinwang .ssh]# ansible --version   #ansible的版本号
ansible 2.4.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
[root@zhuxinwang .ssh]# 

配置主机清单

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

[root@zhuxinwang .ssh]# cat /etc/ansible/hosts 
[host]
zxw1 ansible_ssh_host=192.168.233.146
zxw2 ansible_ssh_host=192.168.233.148
[root@zhuxinwang .ssh]# 
注:
ansible_ssh_host:被管理主机IP
ansible_ssh_user:被管理主机用户名
ansible_ssh_pass:被管理主机用户的登陆密码
ansible_sudo_pass:被管理主机用户sudo时的密码
设置ssh免密码登录

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

[root@zhuxinwang ~]# ls ~/.ssh/
authorized_keys  id_rsa  id_rsa.pub
[root@zhuxinwang ~]# ssh-copy-id 192.168.233.146
[root@zhuxinwang ~]# ssh-copy-id 192.168.233.148

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

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

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

[root@zhuxinwang ~]# ll /etc/hosts
-rw-r--r--. 1 root root 180 9月   9 00:38 /etc/hosts
[root@zhuxinwang ~]# chmod 600 /etc/hosts
#禁止非root用户查看Ansible的主机清单配置文件
[root@zhuxinwang ~]# ll /etc/ansible/hosts
-rw-r--r-- 1 root root 87 9月   9 21:59 /etc/ansible/hosts
[root@zhuxinwang ~]# chmod 600 /etc/ansible/hosts

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

ansible <被操控的主机或主机组或all> [-m 模块名] [-a 具体命令]
注意:
主机组名====> /etc/ansible/hosts里设定的nginx,apache,web
主机名====> Web01,Web02
all --------->/etc/ansible/hosts里设定的所有主机
模块名-----> command,cron,shell,file等

ping模块

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

#测试单机web01
[root@zhuxinwang ansible]# ansible web01 -m ping 
web01 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
[root@zhuxinwang ansible]# 
#测试单机web02
[root@zhuxinwang ansible]# ansible web02 -m ping 
web02 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
[root@zhuxinwang ansible]# 
#测试主机名
[root@zhuxinwang ansible]# ansible nginx -m ping 
web02 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
web01 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
[root@zhuxinwang ansible]# 
#测试所有的被管理主机
[root@zhuxinwang ansible]# ansible all -m ping 
web02 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
web01 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
[root@zhuxinwang ansible]# 

command模块

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

[root@zhuxinwang ansible]# ansible web01 -m command -a 'uptime'
web01 | CHANGED | rc=0 >>
 15:14:55 up 38 min,  3 users,  load average: 0.01, 0.04, 0.05
 
[root@zhuxinwang ansible]# ansible web01 -m command -a 'ls'
web01 | CHANGED | rc=0 >>
anaconda-ks.cfg
nginx-1.10.2.tar.gz

shell模块
[root@zhuxinwang ansible]# ansible web01 -m shell -a "useradd yunjisuan"
web01 | CHANGED | rc=0 >>

[root@zhuxinwang ansible]# ansible web01 -m shell -a "echo 123123 | passwd --stdin yunjisuan"
web01 | CHANGED | rc=0 >>
 #更改用户 yunjisuan 的密码 。
Changing password for user yunjisuan.
#passwd:所有的身份验证令牌已经成功更新。
passwd: all authentication tokens updated successfully.

[root@zhuxinwang ansible]# ansible web01 -m shell -a "id yunjisuan"
web01 | CHANGED | rc=0 >>
uid=1001(yunjisuan) gid=1001(yunjisuan) groups=1001(yunjisuan)

[root@zhuxinwang ansible]# ansible web01 -m shell -a "tail -1 /etc/shadow"
web01 | CHANGED | rc=0 >>
yunjisuan:$6$aUViV7AY$U8MQUY6kEX2iTiVatf0YN0xZd3mWc.DgcRA/0twXaak7QUSmewsqUlDtxjUfDCQsUm7mwDxSmUx4HJJue83LL/:17848:0:99999:7:::
#在所有被管理的主机的/etc/hosts文件里添加Ansible管理服务器的IP地址映射
[root@zhuxinwang ansible]# ansible all -m shell -a 'echo "ansible 192.168.233.146" >> /etc/hosts'
web01 | CHANGED | rc=0 >>

web02 | CHANGED | rc=0 >>

[root@zhuxinwang ansible]# 

[root@zhuxinwang ansible]# ansible all -m shell -a 'tail -1 /etc/hosts'
web01 | CHANGED | rc=0 >>
ansible 192.168.233.146

web02 | CHANGED | rc=0 >>
ansible 192.168.233.146

[root@zhuxinwang ansible]# 

cron模块

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

crontab时间周期:

  • minute:分钟
  • hour:小时
  • day:日期
  • month:月份
  • weekday:周期
    crontab任务:
  • job:指明运行的命令是什么
    crontab任务描述:
  • name:定时任务描述(定时任务清除的依据)
    state状态:
  • present:表示添加(省略状态时默认使用);
  • absent:表示移除;
    crontab任务的用户身份:
  • user:指定定时任务以哪个用户身份执行
#添加定时任务计划,在所有被管理的主机里每十分钟输出hello字符串,定时任务描述为test cron job
[root@zhuxinwang ~]# ansible all -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job"'
Web02 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test cron job"
    ]
}
Web01 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test cron job"
    ]
}

[root@zhuxinwang ~]# ansible all -m shell -a 'crontab -l'
Web01 | SUCCESS | rc=0 >>
#Ansible: test cron job
*/10 * * * * /bin/echo hello
Web02 | SUCCESS | rc=0 >>
#Ansible: test cron job
*/10 * * * * /bin/echo hello

#删除描述为test cron job的定时任务
[root@zhuxinwang ~]# ansible all -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job" state=absent'
Web02 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}
Web01 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}

[root@zhuxinwang ~]# ansible all -m shell -a 'crontab -l'
Web02 | SUCCESS | rc=0 >>
Web01 | SUCCESS | rc=0 >>

#给Web01服务器上的普通用户yunjisuan添加一个定时任务
[root@zhuxinwang ~]# ansible Web01 -m shell -a 'id yunjisuan'
Web01 | SUCCESS | rc=0 >>
uid=1000(yunjisuan) gid=1000(yunjisuan) 组=1000(yunjisuan)

[root@zhuxinwang ~]# ansible Web01 -m cron -a 'minute="*/10" job="/bin/echo hello" name="yunjisuan cron job" user="yunjisuan"'
Web01 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "yunjisuan cron job"
    ]
}

[root@zhuxinwang ~]# ansible Web01 -m shell -a 'crontab -u yunjisuan -l'
Web01 | SUCCESS | rc=0 >>
#Ansible: yunjisuan cron job
*/10 * * * * /bin/echo hello

[root@zhuxinwang ~]# ansible Web01 -m cron -a 'minute="*/10" job="/bin/echo hello" name="yunjisuan cron job" user="yunjisuan" state="absent"'
Web01 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}

[root@zhuxinwang ~]# ansible Web01 -m shell -a 'crontab -u yunjisuan -l'
Web01 | SUCCESS | rc=0 >>
copy模块

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

#将本地的/etc/hosts文件拷贝到所有被管理的主机的/etc/hosts路径下覆盖同名文件,并指定属主和权限,若拷贝的文件与目标文件内容不同,则备份目标文件再覆盖。
[root@zhuxinwang ansible]# ansible all -m shell -a 'tail -1 /etc/hosts'
web02 | CHANGED | rc=0 >>
ansible 192.168.233.146

web01 | CHANGED | rc=0 >>
ansible 192.168.233.146

[root@zhuxinwang ansible]# echo "web01 192.168.233.146" >> /etc/hosts
[root@zhuxinwang ansible]# tail -1 /etc/hosts
web01 192.168.233.146

[root@zhuxinwang ansible]# ansible all -m copy -a 'src=/etc/hosts dest=/etc/hosts owner=root mode=640 backup=yes'
web01 | CHANGED => {
    "backup_file": "/etc/hosts.2715.2018-11-13@16:10:28~", 
    "changed": true, 
    "checksum": "7e713befaeb1ab890051dd46c25644170065bea0", 
    "dest": "/etc/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "3ccca4e2629ce11dd2d7b90c7d952278", 
    "mode": "0640", 
    "owner": "root", 
    "size": 180, 
    "src": "/root/.ansible/tmp/ansible-tmp-1542096627.13-76633854420444/source", 
    "state": "file", 
    "uid": 0
}
web02 | CHANGED => {
    "backup_file": "/etc/hosts.2049.2018-11-13@16:10:28~", 
    "changed": true, 
    "checksum": "7e713befaeb1ab890051dd46c25644170065bea0", 
    "dest": "/etc/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "3ccca4e2629ce11dd2d7b90c7d952278", 
    "mode": "0640", 
    "owner": "root", 
    "size": 180, 
    "src": "/root/.ansible/tmp/ansible-tmp-1542096627.13-77760089112753/source", 
    "state": "file", 
    "uid": 0
}
[root@zhuxinwang ansible]# 

[root@zhuxinwang ansible]# ansible all -m shell -a 'tail -1  /etc/hosts'
web01 | CHANGED | rc=0 >>
web01 192.168.233.146

web02 | CHANGED | rc=0 >>
web01 192.168.233.146

[root@zhuxinwang ansible]# 

[root@zhuxinwang ansible]# ansible all -m shell -a  'ls /etc/hosts*'
web01 | CHANGED | rc=0 >>
/etc/hosts
/etc/hosts.2715.2018-11-13@16:10:28~  #这就是备份的文件
/etc/hosts.allow
/etc/hosts.deny

web02 | CHANGED | rc=0 >>
/etc/hosts
/etc/hosts.2049.2018-11-13@16:10:28~ #这就是备份的文件
/etc/hosts.allow
/etc/hosts.deny

#将本地/tmp/test.sh的脚本复制到远程主机上并远程激活
[root@zhuxinwang ~]# ansible all -m copy -a 'src=/tmp/test.sh dest=/tmp owner=root mode=500'
web02 | CHANGED => {
    "changed": true, 
    "checksum": "3b5b6b57b8164ce5200b9bf9ef72f51009283e66", 
    "dest": "/tmp/test.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "f6f665e562fba3a7fe6c6cff54f9d545", 
    "mode": "0500", 
    "owner": "root", 
    "size": 40, 
    "src": "/root/.ansible/tmp/ansible-tmp-1542097876.27-73447077521162/source", 
    "state": "file", 
    "uid": 0
}
web01 | CHANGED => {
    "changed": true, 
    "checksum": "3b5b6b57b8164ce5200b9bf9ef72f51009283e66", 
    "dest": "/tmp/test.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "f6f665e562fba3a7fe6c6cff54f9d545", 
    "mode": "0500", 
    "owner": "root", 
    "size": 40, 
    "src": "/root/.ansible/tmp/ansible-tmp-1542097876.26-126294137583906/source", 
    "state": "file", 
    "uid": 0
}

[root@zhuxinwang ~]# ansible all -m shell -a '/tmp/test.sh'
web02 | CHANGED | rc=0 >>
welcome to yunjisuan

web01 | CHANGED | rc=0 >>
welcome to yunjisuan

[root@zhuxinwang ~]# 

script模块

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

[root@zhuxinwang ~]# echo 'echo "123123" >> /tmp/test' >> /tmp/test.sh
[root@zhuxinwang tmp]# cat /tmp/test.sh 
#!/bin/bash
echo "123123" >> /tmp/test
[root@zhuxinwang tmp]# 


[root@zhuxinwang tmp]# ansible all -m script -a '/tmp/test.sh'
web02 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.233.147 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.233.147 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}
web01 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.233.146 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.233.146 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}

[root@zhuxinwang tmp]# ansible all -m shell -a 'cat /tmp/test'
web02 | CHANGED | rc=0 >>
123123

web01 | CHANGED | rc=0 >>
123123

[root@zhuxinwang tmp]# 


yum模块

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

软件包名:

  • name:指定软件包的名字
    state状态:
  • present:安装软件包(默认就是这个)
  • absent:卸载软件包
[root@zhuxinwang tmp]# ansible all -m yum -a 'name=nmap'
[root@zhuxinwang tmp]# ansible all -m yum -a 'name=namp state=absent'
[root@zhuxinwang tmp]# 

service模块
利用service模块管理服务程序,虽然能被shell模块替代
但是用service模块更显专业一些

服务名称:

  • name:指定服务的名字
    state状态:
  • started:启动服务
  • stopped:停止服务
  • restarted:重启服务
  • reloaded:平滑重载
    enabled开机自启动:
  • true:设置开机自启动
  • false:设置开启不启动
#启动firewalld并设置开机自启动
[root@zhuxinwang tmp]# ansible web01 -m service -a 'name=firewalld state=started enabled=true'
[root@zhuxinwang tmp]# ansible web01 -m service -a 'name=firewalld state=stopped enabled=false'

user模块

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

指定用户名

  • name:指定操作的用户的名字
    用户描述
  • comment:指定用户的描述信息
    createhome:是否创建家目录
    uid:指定用户的uid号
    groups:指定用户的附加组(默认创建和用户名相同的组)
    password:指定用户的密码
    update_password:更新用户的密码

    shell指定用户的登陆方式
  • /bin/bash:能登录系统
  • /sbin/nologin:不能登录系统
    home:指定用户的家目录路径
    state状态:
  • present:创建用户(默认就是这个)
  • absent:删除用户
    remove:当指定state=absent时,确认是否删除用户家目录
  • true
  • false
[root@zhuxinwang tmp]# ansible web02 -m user -a 'name=yunjisuan comment="welcome to yunjisuan" uid=1066 groups=wheel password=123123 shell=/bin/bash home=/home/yunjisuan'

 [WARNING]: The input password appears not to have been hashed. The 'password' argument
must be encrypted for this module to work properly.

web02 | SUCCESS => {
    "append": false, 
    "changed": false, 
    "comment": "welcome to yunjisuan", 
    "group": 1000, 
    "groups": "wheel", 
    "home": "/home/yunjisuan", 
    "move_home": false, 
    "name": "yunjisuan", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "uid": 1066
}
[root@zhuxinwang tmp]# 
[root@zhuxinwang tmp]# ansible web02 -m shell -a 'tail -1 /etc/passwd'
web02 | CHANGED | rc=0 >>
yunjisuan:x:1066:1000:welcome to yunjisuan:/home/yunjisuan:/bin/bash

[root@zhuxinwang tmp]# 
[root@zhuxinwang tmp]# ansible web02 -m shell -a 'tail -1 /etc/shadow'
web02 | CHANGED | rc=0 >>
yunjisuan:123123:17848:0:99999:7:::  #密码是明文的!!!

----------------------------------------------------------

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

#安装Python2的pip工具,并通过pip工具安装Python的加密模块来给密码加密
[root@ansible ~]# yum -y install epel-release
[root@ansible ~]# yum -y install python2-pip
[root@ansible ~]# pip install passlib
#生成密文密码
[root@ansible ~]# python -c "from passlib.hash import sha512_crypt;import getpass;print sha512_crypt.encrypt(getpass.getpass())"
Password:       #输入你想要加密的密码
$6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1          #加密后的密码
#删除之前创建的yunjisuan用户,并删除它的家目录
[root@ansible ~]# ansible Web02 -m user -a 'name=yunjisuan state=absent remove=true'
Web02 | SUCCESS => {
    "changed": true, 
    "force": false, 
    "name": "yunjisuan", 
    "remove": true, 
    "state": "absent"
}
#继续在Web02上创建yunjisuan用户
[root@ansible ~]# ansible Web02 -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'
[root@ansible ~]# ansible Web02 -m shell -a 'tail -1 /etc/shadow'
Web02 | SUCCESS | rc=0 >>
yunjisuan:$6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1:17783:0:99999:7:::      #终于密文了

setup模块

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

[root@zhuxinwang tmp]# ansible web01  -m setup | head
web01 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.233.146"
        ], 
        "ansible_all_ipv6_addresses": [
            "fe80::a5a9:9b64:5045:9fe", 
            "fe80::ae57:bfcb:5ac:1e11"
        ], 
        "ansible_apparmor": {
[root@zhuxinwang tmp]# 
THE END

发表回复