Ansible详解快速上手

一、什么是Ansible?

ansible是基于 paramiko 开发的,并且基于模块化工作,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。

  • 优点?
    基于python开发,支持API及自定义模块;
    丰富的内置模块,可以用ansbile-doc -l去查看可用的模块;
    无需客户端,无需再client配置,主要通过ssh来和远程主机通信;
    批量部署
  • 缺点?
    ansible 是一个相对较完美的自动运维工具,要说缺点就是,对windows被管节点需要加强、执行效率相对较低。

1. 简介
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
(1)、连接插件connection plugins:负责和被监控端实现通信;
(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
(3)、各种模块核心模块、command模块、自定义模块;
(4)、借助于插件完成记录日志邮件等功能;
(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。
2. 特性
(1)、no agents:不需要在被管控主机上安装任何客户端;
(2)、no server:无服务器端,使用时直接运行命令即可;
(3)、modules in any languages:基于模块工作,可使用任意语言开发模块;
(4)、yaml,not code:使用yaml语言定制剧本playbook;
(5)、ssh by default:基于SSH工作;
(6)、strong multi-tier solution:可实现多级指挥。
4、优点
(1)、轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
(2)、批量任务执行可以写成脚本,而且不用分发到远程就可以执行;
(3)、使用python编写,维护更简单,ruby语法过于复杂;
(4)、支持sudo。

二、安装Ansbile

yum install -y epel-release #安装epel源
yum install ansible -y #安装ansible
ansible-doc -l #列出有哪些可用的模块
ansible-doc -l | grep user #列出与user有关的模块

三、Ansible详解

Ansible常用参数:
-m:要执行的模块,默认为command
-a:指定模块的参数
-u:ssh连接的用户名,默认用root,ansible.cfg中可以配置
-b:--become:变成那个用户身份,不提示密码
-k:提示输入ssh登录密码,当使用密码验证的时候用
-s:sudo运行
-U:sudo到哪个用户,默认为root
-K:提示输入sudo密码,当不是NOPASSWD模式时使用
-C:只是测试一下会改变什么内容,不会真正去执行
-c:连接类型(default=smart)
-f:fork多少进程并发处理,默认为5-i:指定hosts文件路径,默认default=/etc/ansible/hosts
-I:指定pattern,对已匹配的主机中再过滤一次
-list-host:只打印有哪些主机会执行这个命令,不会实际执行
-M:要执行的模块路径,默认为/usr/share/ansible
-o:压缩输出,摘要输出
--private-key:私钥路径
-T:ssh连接超时时间,默认是10-t:日志输出到该目录,日志文件名以主机命名
-v:显示详细日志

Ansible的配置文件
1、/etc/ansible/hosts:主机列表清单,也叫Inventory。所有被管理的主机都需要定义在该文件中。如果不想使用默认清单的话可以用-i选项指定自定义的清单文件,防止多人混合使用一个主机清单。如果没有定义在主机列表文件中,执行命令会提示“No hosts matched”
2、/etc/ansible/ansible.cfg:Ansible服务主配置文件,比如并发数控制等在此文件定义

Inventory定义方法
方法1:将主机IP、端口、用户名、密码写在配置文件的不同组中,多种写法格式如下

vim /etc/ansible/hosts
[webserver]  #组名为webserver
192.168.1.31 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.1.32 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.1.33 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.1.36 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"

[dbserver]  #组名为dbserver
192.168.1.4[1:3] ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"  #连续IP的简写
[redis]
192.168.1.5[1:3]
[redisserver:vars]  #将信息定义为变量
ansible_ssh_pass="123456"

[nginx] #推荐写法
nginx_1 ansible_ssh_host=192.168.1.61 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
nginx_2 ansible_ssh_host=192.168.1.62 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"

基于秘钥管理

基于秘钥管理客户端的话需要在管理端创建公钥和私钥,然后发送给被管理端的机器

#管理端创建秘钥并发送到每个被管理端
ssh-keygen
ssh-copy-id -i $被管理端IP

#管理端清单文件
vim /etc/ansible/hosts
[webserver]  #做好秘钥的机器只用写IP即可
192.168.1.31:22
192.168.1.32
192.168.1.33
192.168.1.36

[dbserver]  #也可以通过别名方式进行配置
node1 ansible_ssh_host=192.168.1.31 ansible_ssh_port=22
node2 ansible_ssh_host=192.168.1.32 ansible_ssh_port=22
node3 ansible_ssh_host=192.168.1.33 ansible_ssh_port=22

Inventory参数说明

#将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.
ansible_ssh_host
#ssh端口号.如果不是默认的端口号,通过此变量设置.
ansible_ssh_port
#默认的 ssh 用户名
ansible_ssh_user
#ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)
ansible_ssh_pass
#sudo 密码(这种方式并不安全,我们强烈建议使用 --ask-sudo-pass)
ansible_sudo_pass
#sudo 命令路径(适用于1.8及以上版本)
ansible_sudo_exe (new in version 1.8)
# 与主机的连接类型.比如:local, ssh 或者 paramiko.
# Ansible 1.2 以前默认使用 paramiko.1.2 以后默认使用 'smart','smart' 方式
# 会根据是否支持 ControlPersist, 来判断'ssh' 方式是否可行.
ansible_connection
#ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况.
ansible_ssh_private_key_file
# 目标系统的shell类型.默认情况下,命令的执行使用 'sh' 语法,
# 可设置为 'csh' 或 'fish'.
ansible_shell_type
# 目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径
# 不是"/usr/bin/python",比如  \*BSD, 或者 /usr/bin/python
# 不是 2.X 版本的 Python.我们不使用 "/usr/bin/env" 机制,
# 因为这要求远程用户的路径设置正确,
# 且要求 "python" 可执行程序名不可为 python以外的名字(实际有可能名为python26).
# 与 ansible_python_interpreter 的工作方式相同,可设定如 ruby 或 perl 的路径.
ansible_python_interpreter

四、Ansible常用模块?

Ansible模块很多,我列举了一些我写ansible-polybook经常用到的一些模块
#查看ansible一共有多少个模块

[root@localhost ~]# ansible-doc -l | wc -l
  • ping模块
测试终端和远程主机是否连接成功
语法:ansible hosts 主机清单 -m ping
[root@localhost ~]# ansible webserver(机组) -m ping
[root@localhost ~]# ansible all -m ping
all代表所有主机清单
  • command模块
语法:
ansible  主机清单 -m  模块名 -a  '执行命令'

[root@localhost ~]# ansible dbserver -m command  -a 'ls /root'


creates:当指定文件存在时,后一条命令不执行 / 指定文件不存在,后一条命令执行
removes:当指定文件存在时,后一条命令执行 / 指定文件不存在,后一条命令不执行

# /etc/fstab文件存在,则cat命令不执行
[root@localhost ~]# ansible dbserver -m command -a 'creates=/etc/fstab   cat /etc/fastab'
192.168.168.21 | SUCCESS | rc=0 >>
skipped, since /etc/fstab exists

# /etc/fstab文件存在,则cat命令执行
[root@localhost ~]# ansible dbserver -m command -a 'removes=/etc/fstab   cat /etc/fstab'
192.168.168.21 | CHANGED | rc=0 >>
 /etc/fstab
 Created by anaconda on Wed Mar 11 02:53:53 2020
  • shell模块
    在远程机上执行复杂的命令
语法:
ansible 主机清单 -m  模块名 -a  '执行命令'

[root@localhost ~]# ansible dbserver -m shell -a 'ls /root | grep .cfg'
[root@localhost ~]# ansible dbserver  -m shell -a 'mkdir /root/ansible'
  • script模块
    在远程主机执行主控制端脚本
chdir=/目录    进入到指定目录
creates 文件存在 脚本不执行
removes 文件存在 脚本执行

#在ansible创建一个脚本,并赋予执行权限
[root@localhost ~]# vim 1.sh
#!/bin/bash
echo 'hello world' > /tmp/1.txt
[root@localhost ~]# chmod +x 1.sh 

#在被控端执行脚本
[root@localhost ~]# ansible db -m script -a 'chdir=/root 1.sh'
[root@localhost ~]# ansible db -m command -a 'cat /tmp/1.txt'
192.168.10.22 | CHANGED | rc=0 >>
hello world

#可以针对某个文件存在或者不存在来执行脚本
[root@localhost ~]# ansible db -m script -a 'creates=/etc/passwd chdir=/root 2.sh'
192.168.10.22 | SKIPPED		/etc/passwd文件存在,所以后面的语句被跳过
  • copy模块

将主控制的文件复制到远程主机,只针对文件

src  源文件路径
dest   目标文件路径
content  将指定内覆盖写入到目标主机文件中
force=no 	当主控端拷贝的文件名和目标名一致,但是内容不一致,放弃拷贝
force=yes   当主控端拷贝的文件名和目标名一致,但是内容不一致,则进行覆盖
backup=yes	 当主控端拷贝的文件名和目标名一致,但是内容不一致,则进行备份

#将源文件,copy到被控端
[root@localhost ~]# ansible db -m copy -a 'src=/root/shm.txt dest=/tmp/shm.txt'

#将指定内容覆盖到被控端文件内
[root@localhost ~]# ansible db -m copy -a "content='this is content\n hello world' dest=/tmp/shm.txt"

#将文件发送到被控端,如果内容不同则进行备份,并覆盖
[root@localhost ~]# ansible db -m copy -a 'src=/root/shm.txt dest=/tmp/shm.txt backup=yes'
[root@localhost ~]# ansible db -m shell -a 'ls /tmp/shm.*'
192.168.10.22 | CHANGED | rc=0 >>
/tmp/shm.txt
/tmp/shm.txt.6134.2020-03-13@19:31:25~


权限参数
owner  属主
group  属组
mode   权限
 - 将 mode.txt 文件拷贝到被控端,并设置好属主,属组,权限
[root@localhost ~]# ansible db -m copy -a 'src=/root/mode.txt dest=/tmp owner=root group=qqq mode=744'
[root@localhost ~]# ansible db -m shell -a 'ls -lhd /tmp/mode.txt'
192.168.10.22 | CHANGED | rc=0 >>
-rwxr--r--. 1 root qqq 0 313 19:47 /tmp/mode.txt
  • yum模块
    在远程主机上使用安装软件
state:installed 安装软件包
	   removed  卸载软件包
disable_gpg_check=yes  :取消密钥的认证
update_cache=yes  更新缓存,需要在指定安装包时使用

#安装一个httpdyum源
[root@localhost ~]# ansible db -m yum -a 'name=wget* state=installed'

#安装一个独立的rpm软件包
[root@localhost ~]# ansible db -m yum -a 'name=rpm软件包'

#安装一个dstat监控程序,并清除缓存
[root@localhost ansible]# ansible db -m yum -a 'name=dstat update_cache=yes'
  • service模块
    管理远程主机上的服务
name=服务名
state=started开启 / stopped 停止 / reloaded 重新加载 /  restarted重启
enabled=yes 开机自启

#启动httpd服务
[root@localhost ~]# ansible db -m service -a 'name=httpd state=started'

#让httpd服务开机自启
[root@localhost ~]# ansible db -m service -a 'name=httpd enabled=yes'

#检测httpd是否开机自启
[root@localhost ~]# ansible db -m shell -a 'systemctl is-enabled httpd'
enabled			开机自启状态
disablednon-zero return code		非开机自启状态
  • setup模块
#查看远程主机的信息
ansbile lenss -m setup
  • file模块
    创建或删除远程主机上的文件或目录
path 指定文件 	如果远程主机上没有该文件,则进行创建
state 创建类型   touch 文件  directory 目录 
state=absent  删除文件或者目录

link 软连接	src=源文件名  path=目标链接文件名
hard 硬链接	src=源文件名  path=目标链接文件名

以下三个参数,既可以修改,也可以自动添加
mod:权限	可以在添加时设置特殊权限,前提要有执行权限( set 粘滞位)
owner:属主
group:属组

1.创建一个普通文件
[root@localhost ~]# ansible db -m file -a 'path=/tmp/file.txt state=touch'

2.创建一个目录
[root@localhost ~]# ansible db -m file -a 'path=/tmp/test state=directory'

3.创建一个软连接文件
[root@localhost ~]# ansible db -m file -a 'state=link src=/tmp/test path=/tmp-test-ln'

4.创建一个硬链接文件
[root@localhost ~]# ansible db -m file -a 'state=hard src=/etc/passwd  path=/passwd'

5.创建一个文件,并设置权限,属主,属组
[root@localhost ~]# ansible db -m file -a 'state=touch path=/tmp/3.txt mode=744 owner=zs group=zs'

5-2.修改一个文件得权限,属主,属组
[root@localhost ~]# ansible db -m file -a 'path=/tmp/3.txt mode=777'
[root@localhost ~]# ansible db -m file -a 'path=/tmp/3.txt owner=root'
[root@localhost ~]# ansible db -m file -a 'path=/tmp/3.txt group=root'

set:4777 属主特殊权限
	2777 属组特殊权限
	1777 执行特殊权限
	7777 全部特殊权限

5.3.修改一个文件得set位特殊权限
[root@localhost ~]# ansible db -m file -a 'path=/tmp/3.txt mode=2777'
[root@localhost ~]# ansible db -m command -a 'ls -lhd /tmp/3.txt'
-rwxrwsrwx. 1 root root 0 313 21:02 /tmp/3.txt

6.删除文件 / 目录
[root@localhost ~]# ansible db -m file -a 'state=absent path=/tmp-test-ln'
[root@localhost ansible]# ansible db -m file -a 'state=absent path=/目录名'
  • cron模块
    执行任务计划
minute	分		minute=* 每分钟
hour 	时		special_time=hourly  每小时
day	  天
month	月
weekday	周
job  计划任务的工作
name=*  计算任务名称
disbaled=true  禁用某个计划任务
disabled=false  再次开启某个计划任务
state=absent  删除某个计划任务
 
 1.每周2,4,6 间隔1分钟告警
 [root@localhost ~]# ansible db -m cron -a 'minute=* weekday=2,4,6 job="/usr/bin/wall warning" name=warningcron'

2.将设置好的计划任务禁用(必须带上 job=工作任务,name=计划任务名称)
[root@localhost ~]# ansible db -m cron -a 'disabled=true name=warningcron job="/usr/bin/wall warning"'

3.开启某个计划任务(必须带上 job=工作任务,name=计划任务名称)
[root@localhost ~]# ansible db -m cron -a 'disabled=false name=warningcron job="/usr/bin/wall warning"'

4.删除创建好的任务计划(必须带上 job=工作任务,name=计划任务名称)
[root@localhost ansible]# ansible db -m cron -a 'name=warningcron job="/usr/bin/wall warning" state=absent'
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值