目录
- Ansible
- ansible概述
- ansible特性
- 注意事项
- ansible安装和入门
- ansible安装
- ansible相关配置文件
- ansible相关工具基本使用
- Ansible常用模块
- command模块
- shell模块
- script模块
- copy模块
- fetch模块
- file模块
- archive模块
- unarchive模块
- hostname模块
- cron模块
- yum模块
- service模块
- group模块
- user模块
- lineinfile模块
- replace模块
- setup模块
Ansible
ansible概述
Ansible 是近年来越来越火的一款开源运维自动化工具,通过Ansible可以实现运维自动化,提高运维工程师的工作效率,减少人为失误。
Ansible 通过本身集成的非常丰富的模块可以实现各种管理任务,其自带模块超过上千个。更为重要的是,它操作非常简单,即使小白也可以轻松上手,但它提供的功能又非常丰富,在运维领域,几乎可以做任何事。
ansible特性
- 模块化:调用特定的模块,完成特定的任务,丰富的内置模块,几乎可以满足一切要求
- 支持自定义模块,可以使用任何编程语言写模块
- 基于python语言实现
- 安全,基于OpenSSH
- 支持playbook编排任务,yaml格式,支持丰富的数据结构
- 幂等性:执行一次和执行n次结果是一样的
注意事项
- 执行ansible的主机一般称为主控端,中控,master或堡垒机
- 主控端python版本需要2.6或以上
- 被控端python版本小于2.4需要安装python-simplejson
- 被控端如果开启SELinux需要安装libselinux-python
- Windows不能作为主控端
ansible安装和入门
ansible安装
- epel源的rpm包安装
yum install epel-release -y
yum install -y ansible
- 编译安装
yum install -y python-jinja2 PyYAML python-paramiko python-babel python-crypto
tar xf ansible-1.5.4.tar.gz
cd ansible-1.5.4
python setup.py build
python setup.py install
mkdir /etc/ansible
cp -r examples/* /etc/ansible
- git安装
git clone https://github.com/ansible/ansible.git
cd ./ansible
source ./hacking/env-setup
- pip安装
yum install -y python-pip python-devel
yum install -y gcc glibc-devel zibl-devel rpm-build openssl-devel
pip install --upgrade pip
pip install ansible --upgrade
ansible相关配置文件
- /etc/ansible/ansible.cfg:主配置文件,配置ansible工作特性
- /etc/ansible/hosts:被管理端主机清单
- /etc/ansible/roles:存放角色的目录
主配置文件的几个重要参数:
参数 | 详解 |
inventory = /etc/ansible/hosts | 被管理端主机清单 |
library = /usr/share/my_modules/ | 库文件存放目录 |
remote_tmp = ~/.ansible/tmp | 临时py命令文件存放在被管理主机目录 |
local_tmp = ~/.ansible/tmp | 本地存放py命令文件目录 |
forks = 5 | 默认并发数 |
sudo_user = root | 默认sudo用户 |
ask_sudo_pass = True | 每次执行ansible命令是否询问sudo密码 |
ask_pass = True | 是否询问ssh密码 |
remote_port = 22 | ssh端口号 |
host_key_checking = False | 检测对应服务器的host_key,建议取消注释 |
log_path = /var/log/ansible.log | 日志文件路径,建议启用 |
module_name = command | 默认的模块 |
ansible的hosts几种常见的写法:
[nginx]
192.168.0.181
[httpd]
192.168.0.178
[web]
192.168.0.[178:181]
[ssh]
192.168.0.178:17777
[hostname]
db-[99:101]-node.example.com
利用ansible实现管理的主要方式:
- Ad-Hoc:利用ansible命令,主要用于临时命令使用场景
- Ansible-playbook:主要用于长期规划好的,大型项目场景,需要有前期的规划过程
ansible相关工具基本使用
- ansible:主程序,临时命令执行工具
格式:
ansible <host-pattern> [-m module_name] [-a args]
ansible all -m ping #验证被管理端连通性
ansible --version #显示ansible版本信息
ansible -v #显示ansible执行过程 -vv,-vvv显示的更详细
ansible -k #提示输入ssh连接密码,默认key验证
ansible --check #预执行,检查语法,预执行时候可能会报错,但在实际执行过程中不一定会报错
ansible -T,--timeput=TIMEOUT #执行命令超时时间,默认10s
ansible -u,--user=REMOTE_USER #执行远程执行的用户
ansible -k #提示输入sudo时的口令
范例:
ansible all -m ping
# *:通配符
ansible "*" -m ping
ansible 192.168.0.* -m ping
# 或关系
ansible "nginx:ssh" -m ping
# 逻辑与
ansible "web:&nginx" -m ping
# 逻辑非
ansible 'web:!nginx' -m ping
# 正则表达式
ansible "~(ng|htt)*" -m ping
注:以上匹配到的主机都是从ansible的hosts文件中匹配的
- ansible-doc: 此工具用来显示模块帮助
格式:
ansible-doc [options] [module]
-l,--list #列出可用模块
-s,--snippet #显示指定模块的playbook片段
范例:
#列出所有模块
ansible-doc -l
#查看指定模块帮助用法
ansible-doc ping
#查看指定模块playbook用法
ansible-doc -s ping
- ansible-galaxy:下载/上传优秀代码或Roles模块的官网平台
会连接到https://galaxy.ansible.com/下载相应的roles(一堆playbook)
格式:
ansible-galaxy install [ROLESNAME]
范例:
#下载roles
ansible-galaxy collection install newswangerd.collection_demo
#列出已下载的roles
ansible-galaxy list
- ansible-pull:此工具会推送ansible的命令至远程,效率无限提升,对运维要求较高
- ansible-playbook:定制自动化任务,执行剧本工具
范例:
ansible-palybook hello.yml
cat hello.yml
---
- hosts: web
remote_user: root
tasks:
- name: hello world
command: /usr/bin/wall hello world
- ansible-vault:文件加密工具
格式:
ansible-vault [create|decrypt|edit|encrypt|rekey|view]
范例:
ansible-vault encrypt hello.yml #加密
ansible-vault decrypt hello.yml #解密
ansible-vault view hello.yml #查看
ansible-vault edit hello.yml #编辑加密文件
ansible-vault rekey hello.yml #修改密码
ansible-vault create new.yml #创建新文件
- ansible-console:基于console界面与用户交互的执行工具
Ansible常用模块
常用模块帮助文档地址:https://docs.ansible.com/ansible/2.9/modules/modules_by_category.html
command模块
功能: 在远程主机执行命令,此为默认模块,可忽略-m选项
注意:command模块不支持一些特殊符号列如:“$ENVNAME,*,>,|”等
范例:
ansible web -m command -a "ls /opt"
ansible web -m command -a "chdir=/opt ls"
shell模块
功能:与command模块功能相似,另外可以使用特殊符号
范例:
ansible web -m shell -a 'echo $HOSTNAME'
ansible web -m shell -a 'chdir=/opt echo 1 > test.txt'
ansible web -m shell -a 'chdir=/opt ls'
修改ansible默认模块为shell:
[root@k8s-2 ~]# vim /etc/ansible/ansible.cfg
module_name = shell
script模块
功能:在远程主机上执行管理机上的脚本
范例:
vim /data/run.sh
mkdir /opt/test.txt
ansible web -m script -a '/data/run.sh'
copy模块
功能:从管理主机上传输文件到被管理主机
范例:
#将本地文件发送到被控主机上,可指定文件名称,属主和权限等
touch /data/copy.text
ansible web -m copy -a "src=/data/copy.text dest=/opt/copy.text1 owner=root mode=600 backup=yes"
#在被控主机创建文件,并指定文件内容
ansible web -m copy -a 'content=hello dest=/opt/content1.text owner=root mode=600'
#复制目录下文件到被控主机,不包括bak目录本身
ansible web -m copy -a 'src=/data/bak/ dest=/backup'
fetch模块
功能:从被控主机获取文件到管理主机上,不支持目录
范例:
ansible web -m fetch -a "src=/etc/redhat-release dest=/data/os"
file模块
功能:创建、删除文件等
范例:
#创建文件
ansible web -m file -a 'path=/data/file.text state=touch owner=mysql group=root mode=755'
#删除文件
ansible web -m file -a 'path=/data/file.text state=absent'
#创建目录
ansible web -m file -a 'path=/data/file state=directory owner=mysql group=mysql'
#创建软连接
ansible web -m file -a ‘src=/data/testfile dest=/data/testfile-link state=link’
archive模块
功能:打压缩包
范例:
# 将被控主机文件打包,并指定存放在远程主机的路径
ansible web -m archive -a 'path=/var/log dest=/opt/log.tar.bz2 format=bz2'
unarchive模块
功能:解压缩包
两种实现方法:
- 将ansible主机上的压缩包传到远程主机后解压缩到指定目录,设置“copy=yes”
- 将远程主机上的摸个压缩包解压到指定路径下,设置“copy=no”
常见参数:
- copy:默认为yes,当copy为yes时拷贝的文件是从ansible主机复制到远程主机上,如果设置为no,会在远程主机上寻找src指定路径上的文件
- remote_src:和copy功能一样且互斥,yes表示压缩文件在远程主机上,no表示文件在ansible主机上
- src:压缩文件所在路径,可以是ansible主机上,也可以是被控主机上,如果文件在被控主机上则需要将copy设置为no
- dest:设置文件解压后存放在被控主机的路径
- mode:设置解压后的文件权限
范例:
#将本地文件解压至被控主机
ansible web -m unarchive -a 'src=/data/foo.zip dest=/opt/software/foo'
#解压被控主机的文件
ansible web -m unarchive -a 'src=/tmp/foo.zip dest=/opt/software/foo copy=no mode=0777'
#从网络上下载并解压
ansible web -m unarchive -a 'src=https://example.com/example.zip dest=/data copy=no'
hostname模块
功能:管理主机名称
范例:
ansible nginx -m hostname -a 'name=nginx'
cron模块
功能:定时任务
支持时间:minute、hour、day、month、weekday
范例:
#每天下午两点半执行备份脚本(脚本必须在被控主机存在)
ansible db -m cron -a 'minute=30 hout=2 name="backup mysql" job=/root/mysql_backup.sh'
#停止定时任务
ansible db -m cron -a 'name="backup mysql" disabled=yes'
#启用定时任务
ansible db -m cron -a 'name="backup mysql" disabled=no'
#删除计划任务
ansible db -m cron -a 'name="backup mysql" state=absent'
yum模块
功能:管理软件包的安装与删除,只支持RHEL、CentOS、fedora,不支持Ubuntu
范例:
#安装服务
ansible web -m yum -a 'name=httpd state=present'
#卸载服务
ansible web -m yum -a 'name=httpd state=absent'
service模块
功能:管理服务的启动和停止
范例
#启动服务,并允许开机自启
ansible web -m service -a 'name=httpd state=start enable=true'
#重启服务
ansible web -m service -a 'name=httpd state=restarted'
#停止服务
ansible web -m service -a 'name=httpd state=stopped'
group模块
功能:管理组
范例:
#创建一个nginx的系统组
ansible web -m group -a 'name=nginx gid=88 system=yes'
#删除组
ansible web -m group -a ‘name=nginx state=absent’
user模块
功能:管理用户
范例:
#创建用户
ansible web -m user -a 'name=nginx uid=1024 home=/home/nginx group=nginx'
ansible web -m user -a 'name=http uid=2048 group=nginx groups="root,docker" shell=/sbin/nologin system=yes create_home=no'
#删除用户
ansible web -m user -a 'name=nginx state=absent remove=yes'
lineinfile模块
ansible在使用sed进行替换时,经常会遇到需要转义的问题,而且ansible在遇到特殊符号进行替换时,存在问题,无法正常进行替换。
功能: 相当于sed修改文件内容
范例:
#禁用selinux
ansible web -a lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=disabled"
#删除以‘#’开头的行
ansible all -m lineinfile -a 'dest=/etc/fstab state=absent regexp="^#"'
replace模块
功能:与sed类似
范例:
#添加注释行
ansible web -m replace -a "path=/etc/fstab regexp='^(UUID.*) replace='#\1'"
#取消注释
ansible web -m replace -a "path=/etc/fstab regexp='^#(.*) replace='\1'"
setup模块
功能:来收集主机的系统信息,这些facts信息可以直接以变量的形式使用,但是如果主机较多会影响执行速度,可以使用“gather_facts:no”来禁止ansible收集facts信息
范例:
ansible web -m setup
ansible web -m setup -a 'filter=ansible_nodename'