主页 » DevOps » 自动化管理工具 ansible安装 和 ansible常用模块汇总

自动化管理工具 ansible安装 和 ansible常用模块汇总

正文

近期文章:ansible playbook 企业实战多个案例分享

运维自动化的好处

1、减少重复性的工作、节省时间,提高工作效率,偷懒必备

2、更少的错误风险

ansible 是什么

Ansible 是一种 IT 自动化工具。它可以配置系统,部署软件以及协调更高级的 IT 任务,
例如持续部署,滚动更新。Ansible 适用于管理企业 IT 基础设施,从具有少数主机的小规
模到数千个实例的企业环境。Ansible 也是一种简单的自动化语言,可以完美地描述 IT 应
用程序基础结构

ansible 的好处

简单易读:基于 YAML 文本编写,易于阅读,非专业的开发人员也可以编写。

功能强大:它可以同于管理配置,软件安装,流程自动化

无代理:不需要在客户端安装额外的 agent

跨平台支持:支持 linux,Windows,Unix 和网络设备

ansible 架构

ansible架构

ansible 安装

主机端(master):192.168.7.42
被控端(node):192.168.7.99、192.168.7.217、192.168.7.42

在主机端安装

yum install -y epel-release
yum install -y ansible
#ansible配置文件:
/etc/ansible/ansible.cfg
/etc/ansible/hosts
生成秘钥:
ssh-keygen -t rsa  #在/root/.ssh/目录下生成秘钥
#被控端:(复制主机端公钥到被控端)
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]  

或者

scp /root/.ssh/id_rsa.pub 192.168.7.99:/root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
本机也要操作
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

或者:
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

测试
ssh 192.168.7.99 'ifconfig'
ssh 192.168.7.217 'ifconfig'
测试ansible配置是否正常

配置ansible 主机清单

cat /etc/ansible/hosts  //主机清单
[testhosts]   //按组分
192.168.7.42
192.168.7.99
192.168.7.217
[lbtest]
192.168.7.[42-45]   表示42-45内的ip

ansible -i <host_pattern> [-f forks] [-m module_name] [-a args]
  -i:指定主机清单的路径,默认为/etc/ansible/hosts(可省略)
  -m module:默认为command
  -f forks :默认为5个主机同时执行
例如:ansible testhosts -m command -a 'service salt-minion start'

ansible testhosts -u root -k -m shell -a ‘ps axu|grep salt’
备注:
-u 指定用户名
-k 指定密码
-m 指定模块
-a 指定参数

command不支持管道,此时可以用shell

ansible command vs ansible shell

使用children

cat /etc/ansible/hosts
[tests:children]   //定义子项
test1
test2
test3
[test1]
192.168.7.42
[test2]
192.168.7.99
[test3]
192.168.7.217
ansible ping

ansible支持主机列表的正则匹配

*/all 全量
: 逻辑或
& 逻辑与
! 逻辑非
[] 切片
~ 以~开头

如:
ansible all -m ping  #所有默认inventory文件中的机器
ansible "*" -m ping  #同上
ansible 121.28.13.* -m  ping #所有122.28.13.X机器

ansible  web1:web2  -m  ping  #所有属于组web1或属于web2的机器
ansible  web1:!web2  -m  ping #属于组web1,但不属于web2的机器
ansible  web1&web2  -m  ping  #属于组web1又属于web2的机器

ansible webserver[0]  -m  ping    #属于组webserver的第1台机器
ansible webserver[0:5]  -m  ping  #属于组webserver的第1到4台机器
ansible "~(beta|web)\.example\.(com|org)"  -m ping

ansible 常用模块

官方模块文档:https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html

下面是总结的我常用的模块,时间长了有时候会忘,用的时候忘记了来翻译下

file:用于配置文件属性
script:用于在远程机器上执行本地脚本
yum:用于安装软件包
cron:配置计划任务
copy:复制文件到远程主机
command:在远程主机上执行命令,不支持管道
raw:类似于command模块,支持管道
user:配置用户
group:配置用户组
service:用于管理服务
ping:用于检测远程主机是否存活  如:ansible test1 -m ping
setup:查看远程主机的基本信息,获取到的主机信息,其中的KEY都可以在playbook中被当作变量引用  如: {{ ansible_all_ipv4_addresses }}
mount:配置挂载点
script 模块:用于在远程机器上执行本地脚本
script模块:在指定节点上执行/root/a.sh脚本(该脚本是在ansible控制节点上的)
fetch:从远程主机拉取文件到本地
archive模块用于压缩文件
unarchive模块用于解压文件
selinux:设置selinux防火墙
iptables:设置iptables防火墙
synchronize:使用rsync同步文件
get_url:主要用于从http、ftp、https服务器上下载文件(类似于wget)
filesystem:在块设备上创建文件系统

file模块

group:定义文件/目录的属组
owner:定义文件/目录的属主
mode:定义文件/目录的权限
path/name:必选项,定义文件/目录的路径
recurse:递归设置文件的属性,只对目录有效
state:定义文件状态
  - directory:如果目录不存在,创建目录
  - touch:如果文件不存在,创建一个新文件
  - absent:删除文件或目录
  - link:创建软连接
  - hard:创建硬链接(src指定要被链接的源文件路径,dest:被链接到的路径,只应用于state=link情况)
  - file:即使文件不存在,也不会被创建
示例:
    ansible test1 -m file -a "src=/etc/fstab dest=/tmp/fstab state=link"
    ansible test1 -m command  -a 'ls /tmp/ -lh'   
    ansible test -m file -a "name=/ane/soft  state=directory"
    ansible test1 -m file -a "path=/tmp/fstab state=absent" 
    ansible test1 -m file -a "path=/tmp/test state=touch"
    ansible test1 -m file -a 'path=/tmp/d1 state=directory owner=root group=root mode=755'   //创建目录
    ansible test1 -m command  -a 'path=/tmp/'
    ansible test1 -m file -a 'path=/tmp/d1 state=absent '   删除目录

command模块

creates:一个文件名,当该文件存在,则该命令不执行
free_form:要执行的linux指令
chdir:在执行指令之前,先切换到该指定的目录
ansible test2 -m command -a 'chdir=/ane   tar zcf 666.tar.gz 666.txt'
ansible test2 -m command -a 'ls -l /ane'
removes:一个文件名,当该文件不存在,则该选项不执行
示例:
 ansible test -a "/sbin/reboot"

yum模块

enablerepo:启用某个源
name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm包的路径
state:定义软件包状态
present:安装
absent:删除
latest:安装最新的
示例:
    ansible test -m yum -a 'name=httpd state=latest'
    ansible test -m yum -a 'name="@Development tools" state=present'
    ansible test -m yum -a 'name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present'
enablerepo:启用某个源

service模块

arguments:给命令行提供一些选项
enabled:是否开机启动  yes|no
name:必选项,服务名称
runlevel:运行级别
sleep:如果执行了restarted,在则stop和start之间沉睡几秒钟
state:对当前服务执行启动,停止、重启、重新加载等操作(started,stopped,restarted,reloaded)
示例:
ansible test -m service -a "name=httpd state=started enabled=yes"
ansible test -m service -a "name=foo pattern=/usr/bin/foo state=started"
ansible test -m service -a "name=network state=restarted args=eth0"

copy模块

src:源文件
dest:目标路径
backup:覆盖之前,是否备份原文件
owner:设定文件/目录的属主
group:设定文件/目录的属组
mode:设定文件/目录的权限
示例:
 ansible test -m copy -a "src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644"
ansible test -m copy -a "src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes"

cron模块

backup:对远程主机上的原任务计划内容修改之前做备份
day:日(1-31,*,*/2,……)
hour:小时(0-23,*,*/2,……)
minute:分钟(0-59,*,*/2,……)
month:月(1-12,*,*/2,……)
weekday:周(0-7,*,……)
job:要执行的任务,依赖于state=present
name:该任务的描述
special_time:指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly
state:确认该任务计划是创建还是删除(absent)
user:以哪个用户的身份执行
示例:
ansible test -m cron -a 'name="check dirs" hour="5,2" job="ls -alh > /dev/null"'
ansible test -m cron -a 'name="a job for reboot" special_time=reboot job="/some/job.sh"'

user模块

home:指定家目录,需要createhome为yes
groups:用户组
uid:用户UID
password:指定用户密码
name:用户名
createhome:是否创建家目录
system:是否创建为系统用户
remove:但state=absent时,删除家目录
state:创建或者删除
shell:指定用户shell环境

synchronize模块

#使用rsync同步文件,其参数如下:
archive: 归档,相当于同时开启recursive(递归)、links、perms、times、owner、group、-D选项都为yes ,默认该项为开启
checksum: 跳过检测sum值,默认关闭
compress:是否开启压缩
copy_links:复制链接文件,默认为no ,注意后面还有一个links参数
delete: 删除不存在的文件,默认no
dest:目录路径
dest_port:默认目录主机上的端口 ,默认是22,走的ssh协议
dirs:传速目录不进行递归,默认为no,即进行目录递归
rsync_opts:rsync参数部分
set_remote_user:主要用于/etc/ansible/hosts中定义或默认使用的用户与rsync使用的用户不同的情况
mode: push或pull 模块,push模的话,一般用于从本机向远程主机上传文件,pull 模式用于从远程主机上取文件
使用示例:
    src=some/relative/path dest=/some/absolute/path rsync_path="sudo rsync"
    src=some/relative/path dest=/some/absolute/path archive=no links=yes
    src=some/relative/path dest=/some/absolute/path checksum=yes times=no
    src=/tmp/helloworld dest=/var/www/helloword rsync_opts=--no-motd,--exclude=.git mode=pull

filesystem模块

在块设备上创建文件系统
选项: 
dev:目标块设备
force:在一个已有文件系统 的设备上强制创建
fstype:文件系统的类型
opts:传递给mkfs命令的选项
示例:
    ansible test -m filesystem -a 'fstype=ext2 dev=/dev/sdb1 force=yes'
    ansible test -m filesystem -a 'fstype=ext4 dev=/dev/sdb1 opts="-cc"'

mount模块

配置挂载点
选项: 
dump
fstype:必选项,挂载文件的类型 
name:必选项,挂载点 
opts:传递给mount命令的参数
src:必选项,要挂载的文件 
state:必选项 
present:只处理fstab中的配置 
absent:删除挂载点 
mounted:自动创建挂载点并挂载之 
umounted:卸载
示例:
    name=/mnt/dvd src=/dev/sr0 fstype=iso9660 opts=ro state=present
    name=/srv/disk src='LABEL=SOME_LABEL' state=present
    name=/home src='UUID=b3e48f45-f933-4c8e-a700-22a159ec9077' opts=noatime state=present
    ansible test -a 'dd if=/dev/zero of=/disk.img bs=4k count=1024'
    ansible test -a 'losetup /dev/loop0 /disk.img'
    ansible test -m filesystem 'fstype=ext4 force=yes opts=-F dev=/dev/loop0'
    ansible test -m mount 'name=/mnt src=/dev/loop0 fstype=ext4 state=mounted opts=rw'

get_url 模块

#该模块主要用于从http、ftp、https服务器上下载文件(类似于wget),主要有如下选项:
sha256sum:下载完成后进行sha256 check;
timeout:下载超时时间,默认10s
url:下载的URL
url_password、url_username:主要用于需要用户名密码进行验证的情况
use_proxy:是事使用代理,代理需事先在环境变更中定义
示例:
    get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf mode=0440
    get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf sha256sum=b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c

archive模块

用于归档文件
path: /ane/soft下面是多路径的用法
  - /ane/soft
  - /ane/script/*
dest:
format: bz2、gz、tar、xz、zip
remove=yes 删除文件,默认为no
exclude_path 排除特定的目录
  - /path/to/foo/bar
  - /path/to/foo/baz
示例如下
- archive: 
  path=/ane/soft/nginx-1.8.1  
  dest=/ane/soft/nginx.tgz
  format=tar
  #remove=yes
- archive:
  path=/ane/script
  dest=/ane/script

unarchive模块

用于解压文件,模块包含如下选项:
copy:在解压文件之前,是否先将文件复制到远程主机,默认为yes。若为no,则要求目标主机上压缩包必须存在。
creates:指定一个文件名,当该文件存在时,则解压指令不执行
dest:远程主机上的一个路径,即文件解压的路径 
group:解压后的目录或文件的属组
list_files:如果为yes,则会列出压缩包里的文件,默认为no,2.0版本新增的选项
mode:解决后文件的权限
src:如果copy为yes,则需要指定压缩文件的源路径 
owner:解压后文件或目录的属主

示例如下:
- unarchive: src=foo.tgz dest=/var/lib/foo
- unarchive: src=/tmp/foo.zip dest=/usr/local/bin copy=no
- unarchive: src=https://example.com/example.zip dest=/usr/local/bin copy=no

selinux模块

开启selinux
 selinux: policy=targeted  state=enforcing
禁用selinux
 selinux: state:disabled

下面是线上机器关闭了selinux

selinux 模块

分享:
Telegram
Twitter
Facebook

发表评论

了解 zhpengfei.com 的更多信息

立即订阅以继续阅读并访问完整档案。

Continue reading

了解 zhpengfei.com 的更多信息

立即订阅以继续阅读并访问完整档案。

Continue reading

解锁流媒体/ChatGPT

智能分流/仅代理海外网站

IEPL优质线路|全天4K秒开