目录
一.ansible 简介
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
ansible是基于 paramiko 开发的,并且基于模块化工作,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。ansible不需要在远程主机上安装client/agents,因为它们是基于ssh来和远程主机通讯的。ansible目前已经已经被红帽官方收购,是自动化运维工具中大家认可度最高的,并且上手容易,学习简单。是每位运维工程师必须掌握的技能之一。
二.ansible 特点
- 部署简单,只需在主控端部署Ansible环境,被控端无需做任何操作;
- 默认使用SSH协议对设备进行管理;
- 有大量常规运维操作模块,可实现日常绝大部分操作;
- 配置简单、功能强大、扩展性强;
- 支持API及自定义模块,可通过Python轻松扩展;
- 通过Playbooks来定制强大的配置、状态管理;
- 轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
- 提供一个功能强大、操作性强的Web管理界面和REST API接口——AWX平台。
三.ansible架构图
- 核心: ansible
- Core Modules: ansible自带的模块
- Custom Modules: 核心模块功能不足时,用户可以添加扩展模块
- Plugins: 通过插件来实现记录日志,发送邮件或其他功能
- Playbooks: 剧本,YAML格式文件,多个任务定义在一个文件中,定义主机需要调用哪些模块来完成的功能
- Connectior Plugins: ansible基于连接插件连接到各个主机上,默认是使用ssh
- Host Inventory: 记录由Ansible管理的主机信息,包括端口、密码、ip等
四.ansible 命令执行过程
- 加载自己的配置文件,默认
/etc/ansible/ansible.cfg
; - 查找对应的主机配置文件,找到要执行的主机或者组;
- 加载自己对应的模块文件,如 command;
- 通过ansible将模块或命令生成对应的临时py文件(python脚本), 并将该文件传输至远程服务器;
- 对应执行用户的家目录的
.ansible/tmp/XXX/XXX.PY
文件; - 给文件 +x 执行权限;
- 执行并返回结果;
- 删除临时py文件,
sleep 0
退出;
五.安装配置ansible
[root@master ~]# yum -y install epel-release
[root@master ~]# yum -y install ansible #安装ansible
[root@master ~]# ansible --version ##查看ansible版本
ansible 2.9.16
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modle/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-pack
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC .8.5-16)]
[root@master ~]# vi /etc/ansible/hosts ##配置ansible hosts文件
[webserver]
192.168.100.52
[mysql]
192.168.100.53
配置ssh免密钥登陆
[root@master ~]# ssh-keygen -t rsa
[root@master ~]# ssh-copy-id root@192.168.100.52
[root@master ~]# ssh-agent bash
[root@master ~]# ssh-add
六.ansible常用模块命令
ansible-doc 命令
[root@master ~]# ansible-doc -l ##获取全部模块信息
[root@master ~]# ansible-doc -s yum ##获取指定模块的使用帮助
command 模块
这个模块可以直接在远程主机上执行命令,并将结果返回本主机。
[root@master ~]# ansible webserver -m command -a 'date' ##查看指定节点时间
192.168.100.52 | CHANGED | rc=0 >>
cron 模块
day=
#日应该运行的工作( 1-31, , /2, )hour=
# 小时 ( 0-23, , /2, )minute=
#分钟( 0-59, , /2, )month=
# 月( 1-12, *, /2, )weekday=
# 周 ( 0-6 for Sunday-Saturday,, )job=
#指明运行的命令是什么name=
#定时任务描述reboot
# 任务在重启时运行,不建议使用,建议使用special_timespecial_time
#特殊的时间范围,参数:reboot(重启时),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时)state
#指定状态,present表示添加定时任务,也是默认设置,absent表示删除定时任务user
# 以哪个用户的身份执行
该模块适用于管理cron
计划任务,其使用的语法跟crontab
文件中的语法一致
[root@master ~]# ansible webserver -m cron -a 'minute="*/1" job="/usr/bin/echo hello >> /opt/info.txt" name="test cron job"' ##创建计划任务
[root@master ~]# ansible webserver -a 'crontab -l' ##查看计划任务
192.168.100.52 | CHANGED | rc=0 >>
#Ansible: test cron job
*/1 * * * * /usr/bin/echo hello >> /opt/info.txt
[root@master ~]# ansible webserver -m cron -a 'name="test cron job" state=absent' ##删除计划任务
[root@master ~]# ansible webserver -a 'crontab -l'
192.168.100.52 | CHANGED | rc=0 >>
user 模块
该模块主要是用来管理用户账号。
comment
# 用户的描述信息createhome
# 是否创建家目录force
# 在使用state=absent时, 行为与userdel –force一致.group
# 指定基本组groups
# 指定附加组,如果指定为(groups=)表示删除所有组home
# 指定用户家目录move_home
# 如果设置为home=时, 试图将用户主目录移动到指定的目录name
# 指定用户名non_unique
# 该选项允许改变非唯一的用户ID值password
# 指定用户密码remove
# 在使用state=absent时, 行为是与userdel –remove一致shell
# 指定默认shellstate
# 设置帐号状态,不指定为创建,指定值为absent表示删除system
# 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户uid
# 指定用户的uid
[root@master ~]# ansible webserver -m user -a 'name="test1"' ##创建用户test
[root@master ~]# ansible webserver -a 'tail /etc/passwd' ##查看创建用户
test1:x:1001:1001::/home/test1:/bin/bash
[root@master ~]# ansible webserver -m user -a 'name="test1" state=absent' ##删除用户
group 模块
该模块主要用于添加或删除组
gid=
#设置组的GID号name=
#指定组的名称state=
#指定组的状态,默认为创建,设置值为absent
为删除system=
#设置值为yes
,表示创建为系统组
[root@master ~]# ansible webserver -m group -a 'name=webserver gid=306 system=yes' ## 创建用过组并指定其gid
[root@master ~]# ansible webserver -m user -a 'name=test1 uid=306 system=yes group=webserver' ##添加一个用户并指定其uid
[root@master ~]# ansible webserver -a 'tail /etc/group' ##添加完成后查看
webserver:x:306:
[root@master ~]# ansible webserver -a 'tail /etc/passwd'
test1:x:306:306::/home/test1:/bin/bash
[root@master ~]# ansible webserver -a 'id test1'
192.168.100.52 | CHANGED | rc=0 >>
uid=306(test1) gid=306(webserver) 组=306(webserver)
copy 模块
这个模块用于将文件复制到远程主机,同时支持给定内容生成文件和修改权限等
src
#被复制到远程主机的本地文件。可以是绝对路径,也可以是相对路径。如果路径是一个目录,则会递归复制,用法类似于"rsync"content
#用于替换"src",可以直接指定文件的值dest
#必选项,将源文件复制到的远程主机的绝对路径backup
#当文件内容发生改变后,在覆盖之前把源文件备份,备份文件包含时间信息directory_mode
#递归设定目录的权限,默认为系统默认权限force
#当目标主机包含该文件,但内容不同时,设为"yes",表示强制覆盖;设为"no",表示目标主机的目标位置不存在该文件才复制。默认为"yes"others
#所有的 file 模块中的选项可以在这里使用
[root@master ~]# ansible webserver -m copy -a 'src=/etc/fstab dest=/opt/fstab.back owner=root mode=640' ##复制文件
[root@master ~]# ansible webserver -a 'ls -l /opt' ##查看复制文件
192.168.100.52 | CHANGED | rc=0 >>
总用量 8
-rw-r-----. 1 root root 541 1月 11 19:00 fstab.back
-rw-r--r--. 1 root root 30 1月 11 14:18 info.txt
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
[root@master ~]# ansible webserver -m copy -a 'content="hello" dest=/opt/fstab.back' ##给定内容生成文件
[root@master ~]# ansible webserver -a 'cat /opt/fstab.back'
192.168.100.52 | CHANGED | rc=0 >>
hello
file 模块
该模块主要用于设置文件的属性,比如创建文件、创建链接文件、删除文件等。
[root@master ~]# ansible webserver -m file -a 'owner=webserver group=webserver mode=644 path=/opt/fstab.back' ##修改文件的属主属组权限
[root@master ~]# ansible webserver -m file -a 'path=/opt/fstab.link src=/opt/fstab.back state=link' ##设置链接文件
[root@master ~]# ansible webserver -m file -a "path=/opt/fstab.back state=absent" ##删除用过文件
[root@master ~]# ansible webserver -m file -a 'path=/opt/temp state=directory mode=755' ##创建目录
主机连通性测试
[root@master ~]# ansible all -m ping
192.168.100.52 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.100.53 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
yum 模块
该模块主要用于软件的安装
name=
#所安装的包的名称state=
#present
--->安装,latest
--->安装最新的,absent
---> 卸载软件。update_cache
#强制更新yum的缓存conf_file
#指定远程yum安装时所依赖的配置文件(安装本地已有的包)。disable_pgp_check
#是否禁止GPG checking,只用于present
orlatest
。disablerepo
#临时禁止使用yum库。 只用于安装或更新时。enablerepo
#临时使用的yum库。只用于安装或更新时。
[root@master ~]# ansible webserver -m yum -a 'name=httpd' ##安装apache
[root@webserver ~]# rpm -qa httpd ##节点上查看
httpd-2.4.6-67.el7.centos.x86_64
[root@master ~]# ansible webserver -m yum -a 'name=httpd state=absent' ##卸载软件
service 模块
该模块用于服务程序的管理
arguments
#命令行提供额外的参数enabled
#设置开机启动。name=
#服务名称runlevel
#开机启动的级别,一般不用指定。sleep
#在重启服务的过程中,是否等待。如在服务关闭以后等待2秒再启动。(定义在剧本中。)state
#有四种状态,分别为:started
--->启动服务,stopped
--->停止服务,restarted
--->重启服务,reloaded
--->重载配置
[root@master ~]# ansible webserver -a 'systemctl status httpd' ##查看服务状态
192.168.100.52 | FAILED | rc=3 >>
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)non-zero return code
[root@master ~]# ansible webserver -m service -a 'enabled=true name=httpd state=started' #3设置开机自启,服务启动
[root@master ~]# ansible webserver -a 'systemctl status httpd'
192.168.100.52 | CHANGED | rc=0 >>
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2021-01-12 15:29:27 CST; 20s ago
shell 模块
shell模块可以在远程主机上调用shell解释器运行命令,支持shell的各种功能
- chdir: 指定工作目录,在执行对应的命令之前,会先进入到chdir参数指定的目录中
- creates:指定一个文件,当指定的文件存在时,就不执行对应命令。
- removes:使用此参数指定一个文件,当指定的文件不存在时,就不执行对应命令。
[root@master ~]# ansible webserver -m shell -a 'chdir=/opt/ echo hello > hello.txt' ##文件重定向导入
192.168.100.52 | CHANGED | rc=0 >>
[root@webserver ~]# cat /opt/hello.txt ##查看导入结果
hello
[root@master ~]# ansible webserver -m shell -a 'chdir=/opt ls'
192.168.100.52 | CHANGED | rc=0 >>
fstab.link
hello.txt
hell.txt
info.txt
rh
temp