一、简介

ansible是一种自动化运维工具,基于paramiko开发的,并且基于模块化工作,Ansible是一种集成IT系统的配置管理、应用部署、执行特定任务的开源平台,它是基于python语言,由Paramiko和YAML两个关键模块构建。集合了众多运维工具的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能.ansible是基于模块工作的,本身没有批量部署的能力.真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架.ansible不需要在远程主机上安装client/agents,因为它们是基于ssh来和远程主机通讯的。

二、部署ansible

实验环境:

主:192.168.27.3					ansible
服:192.168.27.4
服:192.168.27.5

1、关闭防火墙及安全机制

systemctl stop firewalld
setenforce 0

2、安装epel-release源

yum -y install epel-release
yum -y install ansible
yum -y install tree			#树形工具

3、使用tree工具查看ansible目录下的文件

tree /etc/ansible
/etc/ansible
├── ansible.cfg					#配置文件
├── hosts						#这个文件很重要,因为这个文件是管理主机的配置
└── roles

4、添加被管理的主机

vim /etc/ansible/hosts
#文末添加
[组名]
ip地址
...
...
[组名]
ip地址
...
...
#在后期进行管理的时候,可以不用写ip地址,用组名也是可以进行管理,一个组里可以添加多个ip地址

5、生成密钥对

ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 		#回车
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 					#输入123123
Enter same passphrase again: 									#输入123123
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:dSbvtdv02QcLn0DOhKcUAKX28ES6H1RLkuYkXo5Pjtg root@hzh
The key's randomart image is:
+---[RSA 2048]----+
|         o+ oo   |
|        .+ ..    |
|        o...     |
|   .   . =. .    |
|  . o o S o.  o  |
|.  = + = * = . + |
|..E o + X + + o  |
|.+   . + *.o . . |
|+o.     +. o+ .  |
+----[SHA256]-----+

6、将公钥推送给被管理的服务器

ssh-copy-id root@192.168.241.20				# 注意这里需要输入对方的登录密码,而不是密钥对的密码
ssh-copy-id root@192.168.241.4

7、使用ansible每次都需要进行输入密码,这里可以使用免交互代理

ssh-copy-id root@192.168.241.20				# 注意这里需要输入对方的登录密码,而不是密钥对的密码
ssh-copy-id root@192.168.241.4

三、ansible模块

1、command

ansible 192.168.27.4 -m command -a 'date'			# 指定IP地址执行查看IP地址
ansible test -m command -a 'date'					# 地址分组执行查看IP地址
ansible all -m command -a 'date'					# all代表所有分组,执行查看IP地址
ansible all -a 'date'								# -m command可以不写,默认运行command命令

ansible 配置免秘钥登录 ansible 输入密码_ip地址

2、cron模块

cron模块有两种状态(state):分别是present表示添加(可以省略),absent表示移除

ansible test01 -m cron -a 'minute="*/1" job="/bin/echo hehe" name="test01 hehe"'    	# 创建计划任务
ansible test01 -a 'crontab -l'															# 查看计划任务
ansible test01 -m cron -a ‘name="test01 hehe" state=absent’								# 移除计划任务

ansible 配置免秘钥登录 ansible 输入密码_ip地址_02


ansible 配置免秘钥登录 ansible 输入密码_ansible 配置免秘钥登录_03

3、user模块

user模块请求的是useradd,userdel,usermod三个指令

ansible test01 -m user -a 'name=lisi'								# 创建用户lisi
ansible test01 -a 'tail /etc/passwd'	
ansible test01 -m user -a 'name="lisi" state=absent'				# 删除用户

ansible 配置免秘钥登录 ansible 输入密码_IP_04

4、group模块

group模块请求的是groupadd、groupdel、groupmod三个指令

ansible test01 -m group -a 'name=mysql gid=306 system=yes'
ansible test01 -a 'tail /etc/group'						# tail查看末尾10行
ansible test01 -m user -a 'name=lisi uid=306 system=yes group=mysql'
ansible test01 -a 'id lisi'

5、copy模块

ansible test01 -m copy -a 'src=/opt/test.txt dest=/opt/test.txt owner=root mode=640'
ansible test01 -a 'ls -l /opt'
ansible test01 -a 'cat /opt/test.txt'

ansible test01 -m copy -a 'content="hello" dest=/opt/test.txt'			# 将hello写入/opt/test.txt目录下
ansible test01 -a 'cat /opt/test.txt'

ansible 配置免秘钥登录 ansible 输入密码_IP_05


ansible 配置免秘钥登录 ansible 输入密码_ip地址_06

6、file模块

ansible test01 -m user -a 'name=lisi system=yes'
ansible test01 -m group -a 'name lisi system=yes'
ansible test01 -m file -a 'owner=lisi group=lisi mode=644 path=/opt/test.txt'

ansible test01 -m file -a 'src=/opt/test.txt path=/opt/test.txt.link state=link'			# 创建软链接
ansible test01 -m file -a 'path=/opt/123 state=touch'										# 创建123文件
ansible test01 -m file -a 'path=/opt/123.txt state=absent'									# 删除123文件
ansible test01 -m file -a 'path=/opt/test state=directory mode=700'							# 创建目录

ansible 配置免秘钥登录 ansible 输入密码_ip地址_07

7、ping模块

ansible all -m ping

8、yum模块

ansible test01 -m yum -a 'name=httpd'								# 安装httpd服务
ansible test01 -m yum -a 'name=httpd state=absent'					# 卸载httpd服务

ansible 配置免秘钥登录 ansible 输入密码_IP_08


ansible 配置免秘钥登录 ansible 输入密码_IP_09

9、service模块

ansible test01 -a 'systemctl start httpd'
或者
ansible test01 -m service -a 'enabled=true name=httpd state=started'

ansible 配置免秘钥登录 ansible 输入密码_IP_10


ansible 配置免秘钥登录 ansible 输入密码_ip地址_11

10、shell模块

ansible test01 -m shell -a 'echo 123123 | passwd --stdin lisi'		# 这里需要注意的是用户必须要存在

11、script模块

vim test.sh
#!/bin/bash
echo "hello ansible from script" > /opt/script.txt

chmod +x test.sh
ansible test01 -m script -a 'test.sh'

ansible 配置免秘钥登录 ansible 输入密码_密钥对_12


ansible 配置免秘钥登录 ansible 输入密码_ansible 配置免秘钥登录_13

12、setup模块

ansible test01 -m setup			#获取test01组主机的信息