侧边栏壁纸
博主头像
运维匠-运维工程师知识分享经验和最佳实践博主等级

生活百般滋味,人生需要笑对

  • 累计撰写 60 篇文章
  • 累计创建 3 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Ansible

运维匠
2023-07-15 / 0 评论 / 0 点赞 / 8 阅读 / 61011 字
温馨提示:
本文最后更新于 2024-07-15,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

运维自动化工具Ansible(一)

Ansible介绍和架构

Ansible发展史

Ansible 的名称来自科幻小说《安德的游戏》中跨越时空的即时通信工具,使用它可以在相距数光年的距离,远程实时控制前线的舰队战斗
2012-03-09,发布0.0.1版,2015-10-17,Red Hat宣布1.5亿美元收购
官网:
官方文档:

Ansible 功能

  • 批量执行远程命令,可以对远程的多台主机同时进行命令的执行
  • 批量安装和配置软件服务,可以对远程的多台主机进行自动化的方式配置和管理各种服务
  • 编排高级的企业级复杂的IT架构任务, Ansible的Playbook和role可以轻松实现大型的IT复杂架构
  • 提供自动化运维工具的开发API, 有很多运维工具,如jumpserver就是基于 ansible 实现自动化管工功能

Ansible 特点

优点

  • 功能丰富的模块:提供了多达数千个的各种功能的模块,完成特定任务只需调用特定模块即可,还
  • 支持自定义模块,可使用任何编程语言写模块
  • 使用和部署简单: 无需安装专用代理软件,基于python和SSH(默认已安装)实现
  • 安全: 基于OpenSSH实现安全通讯无需专用协议
  • 幂等性:一个任务执行1遍和执行n遍效果一样,不因重复执行带来意外情况,此特性和模块有关
  • 支持playbook编排任务,YAML格式,编排任务,支持丰富的数据结构
  • 较强大的多层解决方案 Role
  • Python语言实现, 基于Paramiko(python对ssh的实现),PyYAML,Jinja2(模板语言)三个关键模块
  • 属于红帽(IBM)公司产品,背景强大,未来发展前景光明

缺点

  • 如果管理的主机较多时,执行效率不如saltstack高
  • 当前还不支持像MySQL数据库一样的事务回滚

Ansible 架构

Ansible 组成

组合INVENTORY、API、MODULES、PLUGINS的绿框,为ansible命令工具,其为核心执行工具

1676984392121

  • INVENTORY:Ansible管理主机的清单文件,默认为 /etc/ansible/hosts
  • MODULES:Ansible执行命令的功能模块,多数为内置核心模块,也可自定义
  • PLUGINS:模块功能的补充,如连接类型插件、循环插件、变量插件、过滤插件等,该功能不常用
  • API:供第三方程序调用的应用程序编程接口

Ansible 命令执行来源

  • USER 普通用户,即SYSTEM ADMINISTRATOR
  • PLAYBOOKS:任务剧本(任务集),编排定义Ansible任务集的配置文件,由Ansible顺序依次执行,通常是JSON格式的YML文件
  • CMDB(配置管理数据库) API 调用
  • PUBLIC/PRIVATE CLOUD API调用
  • USER-> Ansible Playbook -> Ansibile

注意事项

  • 执行ansible的主机一般称为管理端, 主控端,中控,master或堡垒机
  • 主控端Python版本需要2.6或以上
  • 被控端Python版本小于2.4,需要安装python-simplejson
  • 被控端如开启SELinux需要安装libselinux-python
  • windows 不能做为主控端,只能做为被控制端

Ansible 安装和常见模块

Ansible 安装

ansible的安装方法有多种
官方文档

https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html
https://docs.ansible.com/ansible/latest/installation_guide/index.html

下载

https://releases.ansible.com/ansible/

pip 下载

https://pypi.org/project/ansible/

包安装方式

#CentOS 的EPEL源的rpm包安装
[root@centos ~]#yum install ansible
#ubuntu 安装
[root@ubuntu ~]#apt -y install ansible

pip安装

pip 是安装Python包的管理器,类似 yum
范例: 在rocky8上通过pip3安装ansible

[root@rocky8 ~]#yum -y install python39 rust
[root@rocky8 ~]#pip3 install ansible
[root@rocky8 ~]#ansible --version
ansible [core 2.12.6]
config file = None
configured module search path = ['/root/.ansible/plugins/modules',
'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.9/site-packages/ansible
ansible collection location =
/root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.9.6 (default, Nov 9 2021, 13:31:27) [GCC 8.5.0 20210514
(Red Hat 8.5.0-3)]
jinja version = 3.1.2
libyaml = True
[root@rocky8 ~]#ansible-doc -l 2> /dev/null|wc -l
6763

范例: 安装python3.8 支持ansible2.12以上版本

[root@rocky8 ~]#yum -y install python38 python38-pip
[root@rocky8 ~]#pip3 install --upgrade pip -i https://pypi.douban.com/simple
[root@rocky8 ~]#pip3 install ansible -i https://pypi.douban.com/simple/
[root@rocky8 ~]#ansible --version
ansible [core 2.12.6]
config file = None
configured module search path = ['/root/.ansible/plugins/modules',
'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.8/site-
packages/ansible
ansible collection location =
/root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible
python version = 3.8.8 (default, Nov 9 2021, 13:31:34) [GCC 8.5.0 20210514
(Red Hat 8.5.0-3)]
jinja version = 3.1.2
libyaml = True

范例: 安装默认的python3.6版本会有警报提示

[root@rocky8 ~]#yum -y install python3
[root@rocky8 ~]#pip3 install --upgrade pip -i https://pypi.douban.com/simple
[root@rocky8 ~]#pip3 install ansible -i https://pypi.douban.com/simple/
[root@rocky8 ~]#ansible --version
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the
controller starting with Ansible 2.12. Current version: 3.6.8 (default, Nov
9 2021, 14:44:26) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]. This feature will be
removed from ansible-core in version 2.12. Deprecation warnings
can be disabled by setting deprecation_warnings=False in ansible.cfg.
/usr/local/lib/python3.6/site-packages/ansible/parsing/vault/__init__.py:44:
CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python
core team. Therefore, support for it is deprecated in cryptography and will be
removed in a future release.
from cryptography.exceptions import InvalidSignature
ansible [core 2.11.12]
config file = None
configured module search path = ['/root/.ansible/plugins/modules',
'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.6/site-
packages/ansible
ansible collection location =
/root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible
python version = 3.6.8 (default, Nov 9 2021, 14:44:26) [GCC 8.5.0 20210514
(Red Hat 8.5.0-3)]
jinja version = 3.0.3
libyaml = True
[root@rocky8 ~]#ansible-doc -l 2> /dev/null|wc -l
6141

1676985923345

范例

[root@centos7 ~]#yum -y install python-pip
[root@centos7 ~]#pip install --upgrade pip
[root@centos7 ~]#pip install ansible --upgrade
[root@centos7 ~]#ansible --version
/usr/lib64/python2.7/site-packages/cryptography/__init__.py:39:
CryptographyDeprecationWarning: Python 2 is no longer supported by the Python
core team. Support for it is now deprecated in cryptography, and will be removed
in a future release.
CryptographyDeprecationWarning,
ansible 2.9.12
config file = None
configured module search path = [u'/root/.ansible/plugins/modules',
u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Apr 2 2020, 13:16:51) [GCC 4.8.5 20150623
(Red Hat 4.8.5-39)]
[root@centos7 ~]#ll /opt/etc/ansible/ansible.cfg
-rw-r--r-- 1 wang bin 19980 Aug 11 21:34 /opt/etc/ansible/ansible.cfg

确认安装

[root@ansible ~]#ansible --version
ansible 2.9.5
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules',
'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Nov 21 2019, 19:31:34) [GCC 8.3.1 20190507
(Red Hat 8.3.1-4)]

Ansible 相关文件

Ansible 配置文件列表

  • /etc/ansible/ansible.cfg 主配置文件,配置ansible工作特性,也可以在项目的目录中创建此文件,当前目录下如果也有ansible.cfg,则此文件优先生效,建议每个项目目录下,创建独有的ansible.cfg文
  • /etc/ansible/hosts 主机清单
  • /etc/ansible/roles/ 存放角色的目录

Ansible 主配置文件

Ansible 的配置文件可以放在多个不同地方,优先级从高到低顺序如下

ANSIBLE_CONFIG #环境变量,目录下的文件必须存在才能生效
./ansible.cfg #当前目录下的ansible.cfg,一般一个项目对应一个专用配置文件,推荐使用
~/.ansible.cfg #当前用户家目录下的.ansible.cfg
/etc/ansible/ansible.cfg #系统默认配置文件

Ansible 的默认配置文件 /etc/ansible/ansible.cfg ,其中大部分的配置内容无需进行修改

[defaults]
#inventory = /etc/ansible/hosts #主机列表配置文件
#library = /usr/share/my_modules/ #库文件存放目录
#remote_tmp = $HOME/.ansible/tmp #临时py命令文件存放在远程主机目录
#local_tmp = $HOME/.ansible/tmp #本机的临时命令执行目录
#forks = 5 #默认并发数
#sudo_user = root #默认sudo 用户
#ask_sudo_pass = True #每次执行ansible命令是否询问ssh密码
#ask_pass = True
#remote_port = 22
#host_key_checking = False #检查对应服务器的host_key,建议取消此行注释,实现第一次连
接自动信任目标主机
#log_path=/var/log/ansible.log #日志文件,建议启用
#module_name = command #默认模块,可以修改为shell模块
[privilege_escalation] #普通用户提权配置
#become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False

范例: 通过环境变量ANSIBLE_CONFIG指定ansible配置文件路径

[root@rocky8 ~]#cd /data/ansible/
[root@rocky8 ansible]#cat ansbile.cfg
[defaults]
inventory = ./hosts
[root@rocky8 ansible]#cat hosts
[ubuntu]
10.0.0.100
[centos]
10.0.0.7
10.0.0.8
#定义变量
[root@rocky8 ansible]#export ANSIBLE_CONFIG=./ansbile.cfg
[root@rocky8 ansible]#ansible --version
ansible [core 2.12.6]
config file = /data/ansible/ansbile.cfg
configured module search path = ['/root/.ansible/plugins/modules',
'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.9/site-packages/ansible
ansible collection location =
/root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible

python version = 3.9.6 (default, Nov 9 2021, 13:31:27) [GCC 8.5.0 20210514
(Red Hat 8.5.0-3)]
jinja version = 3.1.2
libyaml = True
[root@rocky8 ansible]#ansible --list-hosts all
hosts (3):
10.0.0.100
10.0.0.7
10.0.0.8

范例: 创建ansible 指定项目专用的配置文件

[root@ubuntu2004 ~]#ansible --version
ansible 2.9.6
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules',
'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.10 (default, Mar 15 2022, 12:22:08) [GCC 9.4.0]
[root@ubuntu2004 ~]#mkdir /data/ansible -p
[root@ubuntu2004 ~]#cd /data/ansible/
[root@ubuntu2004 ansible]#touch ansible.cfg
[root@ubuntu2004 ansible]#ansible --version
ansible 2.9.6
config file = /data/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules',
'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.10 (default, Mar 15 2022, 12:22:08) [GCC 9.4.0]
[root@ubuntu2004 ansible]#cd
[root@ubuntu2004 ~]#ansible --version
ansible 2.9.6
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules',
'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.10 (default, Mar 15 2022, 12:22:08) [GCC 9.4.0]

范例: 当前目录下的ansible的配置文件优先生效

[root@ansible ~]#ansible --version
ansible 2.9.17
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules',
'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121
(Red Hat 8.3.1-5)]
[root@ansible ~]#cp /etc/ansible/ansible.cfg .

[root@ansible ~]#ansible --version
ansible 2.9.17
config file = /root/ansible.cfg #注意配置文件路径
configured module search path = ['/root/.ansible/plugins/modules',
'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121
(Red Hat 8.3.1-5)]
[root@ansible ~]#

Inventory 主机清单文件

ansible的主要功用在于批量主机操作,为了便捷地使用其中的部分主机,可以在inventory 主机清单文件中将其分组组织
默认的inventory file为 /etc/ansible/hosts
inventory file可以有多个,且也可以通过Dynamic Inventory来动态生成
注意:

  • 生产建议在每个项目目录下创建项目独立的hosts文件
  • 通过项目目录下的ansible.cfg文件中的 inventory = ./hosts实现

官方文档:

https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html

主机清单文件格式
inventory文件遵循INI文件风格,中括号中的字符为组名。可以将同一个主机同时归并到多个不同的组中,此外,当如若目标主机使用了非默认的SSH端口,还可以在主机名称之后使用冒号加端口号来标明,如果主机名称遵循相似的命名模式,还可以使用列表的方式标识各主机
Inventory 参数说明

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

范例:

ntp.wang.org
[webservers]
www1.wang.org:2222
www2.wang.org
[dbservers]
db1.wang.org
db2.wang.org
db3.wang.org
#或者
db[1:3].wang.org

范例: 组嵌套

[webservers]
www[1:100].example.com
[dbservers]
db-[a:f].example.com
[appservers]
10.0.0.[1:100]
#定义testsrvs组中包括两个其它分组,实现组嵌套
[testsrvs:children]
webservers
dbservers

范例: 基于用户名和密码的ssh连接主机清单

[test]
10.0.0.8 ansible_connection=local #指定本地连接,无需ssh配置

#每个主机分别指定用户和密码,ansible_connection=ssh 需要StrictHostKeyChecking no 或者host_key_checking = False
10.0.0.7 ansible_connection=ssh ansible_ssh_port=2222 ansible_ssh_user=wangansible_ssh_password=123456
10.0.0.6 ansible_ssh_user=root ansible_ssh_password=123456
#对每个分组的所有主机统一定义用户和密码,执行ansible命令时显示别名,如web01
[websrvs]
web01 ansible_ssh_host=10.0.0.101
web02 ansible_ssh_host=10.0.0.102
[websrvs:vars]
ansible_ssh_password=magedu
some_host ansible_ssh_port=2222 ansible_ssh_user=manager
aws_host ansible_ssh_private_key_file=/home/example/.ssh/aws.pem
freebsd_host ansible_python_interpreter=/usr/local/bin/python
ruby_module_host ansible_ruby_interpreter=/usr/bin/ruby.1.9.3

Ansible相关工具

  • /usr/bin/ansible 主程序,临时命令执行工具

  • /usr/bin/ansible-doc 查看配置文档,模块功能查看工具,相当于man

  • /usr/bin/ansible-playbook 定制自动化任务,编排剧本工具,相当于脚本

  • /usr/bin/ansible-pull 远程执行命令的工具

  • /usr/bin/ansible-vault 文件加密工具

  • /usr/bin/ansible-console 基于Console界面与用户交互的执行工具

  • /usr/bin/ansible-galaxy 下载/上传优秀代码或Roles模块的官网平台

利用ansible实现管理的主要方式:

  • Ansible Ad-Hoc 即利用ansible命令,主要用于临时命令使用场景

  • Ansible playbook 主要用于长期规划好的,大型项目的场景,需要有前期的规划过程

ansible 使用前准备
ansible 相关工具大多数是通过ssh协议,实现对远程主机的配置管理、应用部署、任务执行等功能
建议:使用此工具前,先配置ansible主控端能基于密钥认证的方式联系各个被管理节点
范例:利用sshpass批量实现基于key验证脚本1

[root@centos8 ~]#vim /etc/ssh/ssh_config
#修改下面一行
StrictHostKeyChecking no
[root@centos8 ~]#cat hosts.list
192.168.32.178
192.168.32.179
[root@centos8 ~]#vim push_ssh_key.sh
#!/bin/bash
rpm -ql shpass &> /dev/null || yum -y install sshpass
[ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P ''
export SSHPASS=123456
while read IP;do
sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP


done < hosts.list

范例: 实现基于key验证的脚本2

[root@centos8 ~]#cat ssh_key.sh

#!/bin/bash
PLIST="
192.168.32.178
192.168.32.179"
rpm -q sshpass &> /dev/null || yum -y install sshpass
[ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P ''
export SSHPASS=123456
for IP in $IPLIST;do
       { sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP; } &

done
wait

此工具用来显示模块帮助,相当于man
格式

ansible-doc [options] [module...]
-l, --list #列出可用模块
-s, --snippet #显示指定模块的playbook片段

范例: 查看帮助

[root@rocky ~]# ansible-doc --help
usage: ansible-doc [-h] [--version] [-v] [-M MODULE_PATH] [--playbook-dir BASEDIR]
                   [-t {become,cache,callback,cliconf,connection,httpapi,inventory,lookup,netconf,shell,vars,module,strategy,role,keyword}]
                   [-j] [-r ROLES_PATH] [-e ENTRY_POINT | -s | -F | -l | --metadata-dump] [--no-fail-on-errors]
                   [plugin ...]

plugin documentation tool

positional arguments:
  plugin                Plugin

范例:

#列出所有模块
ansible-doc -l
#查看指定模块帮助用法
ansible-doc ping
#查看指定模块帮助用法
ansible-doc -s ping

范例: 查看指定的插件

[root@rocky ~]# ansible-doc -t connection -l
local        execute on controller                                                                                                 
paramiko_ssh Run tasks via python ssh (paramiko)                                                                                   
psrp         Run tasks over Microsoft PowerShell Remoting Protocol                                                                 
ssh          connect via SSH client binary                                                                                         
winrm        Run tasks over Microsoft's WinRM                                                                                      
[root@rocky ~]# 
[root@rocky ~]# ansible-doc -t lookup -l
config              Lookup current Ansible configuration values                                                                    
csvfile             read data from a TSV or CSV file                                                                               
dict                returns key/value pair items from dictionaries                                                                 
env                 Read the value of environment variables                                                                        
file                read file contents                                                                                             
fileglob            list files matching a pattern                                                                                  
first_found         return first file found from list                                                                              
indexed_items       rewrites lists to return 'indexed items'                                                                       
ini                 read data from an ini file                                                                                     
inventory_hostnames list of inventory hosts matching a host pattern                                                                
items               list of items                                                                                                  
lines               read lines from command                                                                                        
list                simply returns what it is given                                                                                
nested              composes a list with nested elements of other lists                                                            
password            retrieve or generate a random password, stored in a file                                                       
pipe                read output from a command                                                                                     
random_choice       return random element from list                                                                                
sequence            generate a list based on a number sequence                                                                     
subelements         traverse nested key from a list of dictionaries                                                                
template            retrieve contents of file after templating with Jinja2                                                         
together            merges lists into synchronized list                                                                            
unvault             read vaulted file(s) contents                                                                                  
url                 return contents from URL                                                                                       
varnames            Lookup matching variable names                                                                                 
vars                Lookup templated value of variables                                                                            
[root@rocky ~]# 

ansible

Ansible Ad-Hoc 介绍

Ansible Ad-Hoc 的执行方式的主要工具就是 ansible
特点: 一次性的执行,不会保存执行命令信息,只适合临时性或测试性的任务

ansible 命令用法

格式:

ansible <host-pattern> [-m module_name] [-a args]

选项说明:

--version #显示版本
-m module #指定模块,默认为command
-v #详细过程 -vv -vvv更详细
--list-hosts #显示主机列表,可简写 --list
-C, --check #检查,并不执行
-T, --timeout=TIMEOUT #执行命令的超时时间,默认10s
-k, --ask-pass #提示输入ssh连接密码,默认Key验证
-u, --user=REMOTE_USER #执行远程执行的用户,默认root
-b, --become #代替旧版的sudo实现通过sudo机制实现提升权限
--become-user=USERNAME #指定sudo的runas用户,默认为root
-K, --ask-become-pass #提示输入sudo时的口令
-f FORKS, --forks FORKS #指定并发同时执行ansible任务的主机数
-i INVENTORY, --inventory INVENTORY #指定主机清单文件

范例:

#以wang用户执行ping存活检测
ansible all -m ping -u wang -k
#以wang sudo至root执行ping存活检测
ansible all -m ping -u wang -k -b
#以wang sudo至mage用户执行ping存活检测
ansible all -m ping -u wang -k -b --become-user=mage
#以wang sudo至root用户执行ls
ansible all -m command -u wang -a 'ls /root' -b --become-user=root -k -K

范例: 并发执行控制

#分别执行下面两条命令观察结果
[root@ansible ~]#ansible all -a 'sleep 5' -f1
[root@ansible ~]#ansible all -a 'sleep 5' -f10

范例: 使用普能用户进行远程管理

#在所有控制端和被控制端创建用户和密码
[root@rocky8 ~]#useradd wang
[root@rocky8 ~]#echo wang:123456 | chpasswd
#在所有被控制端对用户sudo授权
[root@rocky8 ~]#visudo
wang ALL=(ALL) NOPASSWD: ALL
[root@rocky8 ~]#visudo -c
/etc/sudoers: parsed OK
#实现从控制端到被控制端的基于key验证
[root@ansible ~]#su - wang
wang@ansible:~$ssh-keygen -f ~/.ssh/id_rsa -P ''
wang@ansible:~$$ssh-copy-id wang@'10.0.0.8'
#使用普通用户测试连接,默认连接权限不足失败
wang@ansible:~$ ansible 10.0.0.8 -m shell -a 'ls /root'
10.0.0.8 | FAILED | rc=2 >>
ls: cannot open directory '/root': Permission deniednon-zero return code
#使用普通用户通过-b选项连接实现sudo提权后连接成功
wang@ansible:~$ ansible 10.0.0.8 -m shell -a 'ls /root' -b --become-user root
10.0.0.8 | CHANGED | rc=0 >>
anaconda-ks.cfg
#修改配置文件指定sudo机制
[root@ansible ~]#vim /etc/ansible/ansible.cfg
#取消下面行前面的注释
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
#再次测试
[root@ansible ~]#su - wang
wang@ansible:~$ ansible 10.0.0.8 -m shell -a 'ls /root'
10.0.0.8 | CHANGED | rc=0 >>
anaconda-ks.cfg

范例: 使用普通用户连接远程主机执行代替另一个用户身份执行操作

[root@centos8 ~]#useradd wang
[root@centos8 ~]#echo wang:123456 | chpasswd
#先在被控制端能过sudo对普通用户授权
[root@centos8 ~]#grep wang /etc/sudoers
wang ALL=(ALL) NOPASSWD: ALL
#以wang的用户连接用户,并利用sudo代表mage执行whoami命令
[root@ansible ~]#ansible 10.0.0.8 -m shell -a 'whoami' -u wang -k -b --become-
user=mage
SSH password: #输入远程主机wang用户ssh连接密码
10.0.0.8 | CHANGED | rc=0 >>
mage

ansible的Host-pattern

用于匹配被控制的主机的列表
All :表示所有Inventory中的所有主机
范例

ansible all -m ping

*:通配符

ansible "*" -m ping
ansible 192.168.1.* -m ping
ansible "srvs" -m ping
ansible "10.0.0.6 10.0.0.7" -m ping

或关系

ansible "websrvs:appsrvs" -m ping
ansible "192.168.1.10:192.168.1.20" -m ping

逻辑与

#在websrvs组并且在dbsrvs组中的主机
ansible "websrvs:&dbsrvs" -m ping

逻辑非

#在所有主机,但不在websrvs组和dbsrvs组中的主机
#注意:此处为单引号
ansible 'all:!dbsrvs:!websrvs' -m ping

综合逻辑

ansible 'websrvs:dbsrvs:&appsrvs:!ftpsrvs' -m ping

正则表达式

ansible "websrvs:dbsrvs" -m ping
ansible "~(web|db).*\.magedu\.com" -m ping
ansible 命令的执行过程
  1. 加载自己的配置文件,默认/etc/ansible/ansible.cfg
  2. 查找主机清单中对应的主机或主机组
  3. 加载自己对应的模块文件,如:command
  4. 通过ansible将模块或命令生成对应的临时py文件,并将该文件传输至远程服务器的对应执行用户
    $HOME/.ansible/tmp/ansible-tmp-数字/XXX.PY文件
  5. 给文件+x执行
  6. 执行并返回结果
  7. 删除临时py文件,退出
ansible 命令的执行状态
[root@centos8 ~]#grep -A 14 '\[colors\]' /etc/ansible/ansible.cfg
[colors]
#highlight = white
#verbose = blue
#warn = bright purple
#error = red
#debug = dark gray
#deprecate = purple
#skip = cyan
#unreachable = red
#ok = green
#changed = yellow
#diff_add = green
#diff_remove = red
#diff_lines = cyan
  • 绿色:执行成功并且对目标主机不需要做改变的操作
  • 黄色:执行成功并且对目标主机做变更
  • 红色:执行失败

ansible-console

此工具可交互执行命令,支持tab,ansible 2.0+新增
提示符格式:

执行用户@当前操作的主机组 (当前组的主机数量)[f:并发数]$

常用子命令:

  • 设置并发数: forks n 例如: forks 10
  • 切换组: cd 主机组 例如: cd web
  • 列出当前组主机列表: list
  • 列出所有的内置命令: ?或help

范例

[root@ansible ~]#ansible-console
Welcome to the ansible console.
Type help or ? to list commands.
root@all (3)[f:5]$ ping
10.0.0.7 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.0.0.6 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.0.0.8 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
root@all (3)[f:5]$ list
10.0.0.8
10.0.0.7
10.0.0.6
root@all (3)[f:5]$ cd websrvs
root@websrvs (2)[f:5]$ list
10.0.0.7
10.0.0.8
root@websrvs (2)[f:5]$ forks 10
root@websrvs (2)[f:10]$ cd appsrvs
root@appsrvs (2)[f:5]$ yum name=httpd state=present
root@appsrvs (2)[f:5]$ service name=httpd state=started

ansible-playbook

此工具用于执行编写好的 playbook 任务
范例:

ansible-playbook hello.yml
cat hello.yml
---
#hello world yml file
- hosts: websrvs
  remote_user: root
  gather_facts: no
  tasks:
  - name: hello world
    command: /usr/bin/wall hello world

ansible-vault

此工具可以用于加密解密yml文件
格式:

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 #创建新文件
#执行加密的playbook,交互式输入密码
chmod 600 hello.yml
ansible-playbook --ask-vault-pass hello.yml
#从pass.txt文件中读取密码
ansible-playbook --vault-password-file pass.txt hello.yml
#从配置文件中取得密码
#vi /etc/ansible/ansible.cfg
[defaults]
ault-password-file=pass.txt
#可以直接执行加密文件
ansible-playbook hello.yml

ansible-galaxy

Galaxy 是一个免费网站, 类似于github网站, 网站上发布了很多的共享的roles角色。
Ansible 提供了ansible-galaxy命令行工具连接 url 网站下载相应的roles, 进行init(初始化、search( 查拘、install(安装、 remove(移除)等操作。

1677156776774

范例:

#搜索项目
[root@ansible ~]#ansible-galaxy search lamp
#列出所有已安装的galaxy
ansible-galaxy list
#安装galaxy,默认下载到~/.ansible/roles下
ansible-galaxy install geerlingguy.mysql
ansible-galaxy install geerlingguy.redis
#删除galaxy
ansible-galaxy remove geerlingguy.redis

Ansible常用模块

2015年12月只270多个模块
2016年12年26日ansible 1.9.2 有540个模块
2018年01月12日ansible 2.3.8 有1378个模块
2018年05月28日ansible 2.5.3 有1562个模块
2018年07月15日ansible 2.6.3 有1852个模块
2018年11月19日ansible 2.7.2 有2080个模块
2020年03月02日ansible 2.9.5 有3387个模块
2021年12月22日ansible 2.11.8 有6141个模块
2022年06月04日ansible 2.12.6 有6763个模块
虽然模块众多,但最常用的模块也就2,30个而已,针对特定业务只需要熟悉10几个模块即可
常用模块帮助文档参考:

https://docs.ansible.com/ansible/2.9/modules/modules_by_category.html
https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html
https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html
https://docs.ansible.com/ansible/latest/modules/modules_by_category.html

Command 模块

功能:在远程主机执行命令,此为默认模块,可忽略 -m 选项
注意:此命令不支持 $VARNAME < > | ; & 等,可用shell模块实现
注意:此模块不具有幂等性
常见选项

chdir=dir #执行命令前,先切换至目录dir
creates=file #当file不存在时才会执行
removes=file #当file存在时才会执行

范例:

[root@ansible ~]#ansible websrvs -m command -a 'chdir=/etc cat centos-release'
10.0.0.7 | CHANGED | rc=0 >>
CentOS Linux release 7.7.1908 (Core)
10.0.0.8 | CHANGED | rc=0 >>
CentOS Linux release 8.1.1911 (Core)
[root@ansible ~]#ansible websrvs -m command -a 'chdir=/etc creates=/data/f1.txt cat centos-release'
10.0.0.7 | CHANGED | rc=0 >>
CentOS Linux release 7.7.1908 (Core)
10.0.0.8 | SUCCESS | rc=0 >>
skipped, since /data/f1.txt exists
[root@ansible ~]#ansible websrvs -m command -a 'chdir=/etc removes=/data/f1.txt cat centos-release'
10.0.0.7 | SUCCESS | rc=0 >>
skipped, since /data/f1.txt does not exist
10.0.0.8 | CHANGED | rc=0 >>
CentOS Linux release 8.1.1911 (Core)
ansible websrvs -m command -a 'service vsftpd start'
ansible websrvs -m command -a 'echo magedu |passwd --stdin wang'
ansible websrvs -m command -a 'rm -rf /data/'
ansible websrvs -m command -a 'echo hello > /data/hello.log'

ansible websrvs -m command -a "echo $HOSTNAME"

Shell 模块

功能:和command相似,用shell执行命令,支持各种符号,比如:*,$, > , 相当于增强版的command模块
注意:此模块不具有幂等性,建议能不能就用此模块,最好使用专用模块
常见选项

chdir=dir #执行命令前,先切换至目录dir
creates=file #当file不存在时才会执行
removes=file #当file存在时才会执行

范例:

[root@ansible ~]#ansible websrvs -m shell -a "echo $HOSTNAME"
10.0.0.7 | CHANGED | rc=0 >>
ansible
10.0.0.8 | CHANGED | rc=0 >>
ansible
[root@ansible ~]#ansible websrvs -m shell -a 'echo $HOSTNAME'
10.0.0.7 | CHANGED | rc=0 >>
centos7.wangxiaochun.com
10.0.0.8 | CHANGED | rc=0 >>
centos8.localdomain
[root@ansible ~]#ansible websrvs -m shell -a 'echo centos | passwd --stdin wang'
10.0.0.7 | CHANGED | rc=0 >>
Changing password for user wang.
passwd: all authentication tokens updated successfully.
10.0.0.8 | CHANGED | rc=0 >>
Changing password for user wang.
passwd: all authentication tokens updated successfully.
[root@ansible ~]#ansible websrvs -m shell -a 'ls -l /etc/shadow'
10.0.0.7 | CHANGED | rc=0 >>
---------- 1 root root 889 Mar 2 14:34 /etc/shadow
10.0.0.8 | CHANGED | rc=0 >>
---------- 1 root root 944 Mar 2 14:34 /etc/shadow
[root@ansible ~]#ansible websrvs -m shell -a 'echo hello > /data/hello.log'
10.0.0.7 | CHANGED | rc=0 >>
10.0.0.8 | CHANGED | rc=0 >>
[root@ansible ~]#ansible websrvs -m shell -a 'cat /data/hello.log'
10.0.0.7 | CHANGED | rc=0 >>
hello
10.0.0.8 | CHANGED | rc=0 >>
hello

注意:调用bash执行命令 类似 cat /tmp/test.md | awk -F'|' '{print 1,2}' &> /tmp/example.txt 这些复杂命令,即使使用shell也可能会失败,解决办法:写到脚本时,copy到远程,执行,再把需要的结果拉回执行命令的机器
范例:将shell模块代替command,设为模块

[root@ansible ~]#vim /etc/ansible/ansible.cfg
#修改下面一行
module_name = shell

Script 模块

功能:在远程主机上运行ansible服务器上的脚本(无需执行权限)
注意:此模块不具有幂等性
常见选项

chdir=dir #执行命令前,先切换至目录dir
cmd #指定ansible主机的命令
creates=file #当file不存在时才会执行
removes=file #当file存在时才会执行

范例:

ansible websrvs -m script -a /data/test.sh

Copy 模块

功能:复制ansible服务器主控端或远程的本机的文件到远程主机
注意: src=file 如果是没指明路径,则为当前目录或当前目录下的files目录下的file文件
常见选项

src #控制端的源文件路径
dest #被控端的文件路径
owner #属主
group #属组
mode #权限
backup #是否备份
validate #验证成功才会执行copy
remote_src #no是默认值,表示src文件在ansible主机,yes表示src文件在远程主机

范例:

#如目标存在,默认覆盖,此处指定先备
ansible websrvs -m copy -a "src=/root/test1.sh dest=/tmp/test2.sh owner=wang mode=600 backup=yes"
#指定内容,直接生成目标文件
ansible websrvs -m copy -a "content='wang 123456\nxiao 654321\n' dest=/etc/rsync.pas owner=root group=root mode=0600"
#复制/etc目录自身,注意/etc/后面没有/
ansible websrvs -m copy -a "src=/etc dest=/backup"
#复制/etc/下的文件,不包括/etc/目录自身,注意/etc/后面有/
ansible websrvs -m copy -a "src=/etc/ dest=/backup"
#复制/etc/suders,并校验语法
ansible websrvs -m copy -a "src=/etc/suders dest=/etc/sudoers.edit remote_src=yes validate=/usr/sbin/visudo -csf %s"

Get_url 模块

功能: 用于将文件从http、https或ftp下载到被管理机节点上
常用参数如下:

url #下载文件的URL,支持HTTP,HTTPS或FTP协议
dest #下载到目标路径(绝对路径),如果目标是一个目录,就用原文件名,如果目标设置了名称就用目标
设置的名称
owner #指定属主
group #指定属组
mode #指定权限
force #如果yes,dest不是目录,将每次下载文件,如果内容改变替换文件。如果no,则只有在目标不存
在时才会下载
checksum #对目标文件在下载后计算摘要,以确保其完整性
#示例: checksum="sha256:D98291AC[...]B6DC7B97",
checksum="sha256:http://example.com/path/sha256sum.txt"
url_username #用于HTTP基本认证的用户名。 对于允许空密码的站点,此参数可以不使用`url_password'
url_password #用于HTTP基本认证的密码。 如果未指定`url_username'参数,则不会使用`url_password'参数
validate_certs #如果“no”,SSL证书将不会被验证。 适用于自签名证书在私有网站上使用
timeout #URL请求的超时时间,秒为单位

范例: 下载并MD5验证

[root@ansible ~]#ansible websrvs -m get_url -a 'url=http://nginx.org/download/nginx-1.18.0.tar.gz dest=/usr/local/src/nginx.tar.gz checksum="md5:b2d33d24d89b8b1f87ff5d251aa27eb8"'

Fetch 模块

功能:从远程主机提取文件至ansible的主控端,copy相反,目前不支持目录
常见选项

src #被控制端的源文件路径,只支持文件
dest #ansible控制端的目录路径

范例:

ansible websrvs -m fetch -a 'src=/root/test.sh dest=/data/scripts'

范例:

[root@ansible ~]#ansible all -m fetch -a 'src=/etc/redhat-release
dest=/data/os'
[root@ansible ~]#tree /data/os/
/data/os/
├── 10.0.0.6
│ └── etc
│ └── redhat-release
├── 10.0.0.7
│ └── etc
│ └── redhat-release
└── 10.0.0.8
└── etc
└── redhat-release
6 directories, 3 files

File 模块

功能:设置文件属性,创建文件,目录和软链接等
常见选项

path #在被控端创建的路径
owner #属主
group #属组
mode #权限
state #状态
=touch #创建文件
=directory #创建目录
=link #软链接
=hard #硬链接
recurse #yes表示递归授权

范例:

#创建空文件
ansible all -m file -a 'path=/data/test.txt state=touch'
ansible all -m file -a 'path=/data/test.txt state=absent'
ansible all -m file -a "path=/root/test.sh owner=wang mode=755"
#创建目录
ansible all -m file -a "path=/data/mysql state=directory owner=mysql group=mysql"
#创建软链接
ansible all -m file -a 'src=/data/testfile path|dest|name=/data/testfile-link state=link'
#创建目录
ansible all -m file -a 'path=/data/testdir state=directory'
#递归修改目录属性,但不递归至子目录
ansible all -m file -a "path=/data/mysql state=directory owner=mysql group=mysql"
#递归修改目录及子目录的属性
ansible all -m file -a "path=/data/mysql state=directory owner=mysql group=mysql recurse=yes"

stat 模块

功能:检查文件或文件系统的状态
注意:对于Windows目标,请改用win_stat模块

常见选项

path #文件/对象的完整路径(必须)

常用的返回值判断:

exists: 判断是否存在
isuid: 调用用户的ID与所有者ID是否匹配

范例:

[root@ansible ~]#ansible 127.0.0.1 -m stat -a 'path=/etc/passwd'
127.0.0.1 | SUCCESS => {
"changed": false,
"stat": {
"atime": 1614601466.7493012,
"attr_flags": "",
"attributes": [],
"block_size": 4096,
"blocks": 8,
"charset": "us-ascii",
"checksum": "8f7a9a996d24de98bf1eab4a047f8e89e9c708cf",
"ctime": 1614334259.4498665,
"dev": 2050,
"device_type": 0,
"executable": false,
"exists": true,
"gid": 0,
"gr_name": "root",
"inode": 134691833,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mimetype": "text/plain",
"mode": "0000",
"mtime": 1614334259.4498665,
"nlink": 1,
"path": "/etc/passwd",
"pw_name": "root",
"readable": true,
"rgrp": false,
"roth": false,
"rusr": false,
"size": 1030,
"uid": 0,
"version": "671641160",
"wgrp": false,
"woth": false,
"writeable": true,
"wusr": false,
"xgrp": false,
"xoth": false,
"xusr": false
}
}

案例:

- name: install | Check if file is already configured.
  stat: path={{ nginx_file_path }}
  connection: local
  register: nginx_file_result
- name: install | Download nginx file
  get_url: url={{ nginx_file_url }} dest={{ software_files_path }}
  validate_certs=no
  connection: local
  when:,not. nginx_file_result.stat.exists

范例:

[root@ansible ansible]#cat stat.yml
---
- hosts: websrvs
  tasks:
  - name: check file
    stat: path=/data/mysql
    register: st
  - name: debug
    debug:
      msg: "/data/mysql is not exist"
    when: not st.stat.exists
[root@ansible ansible]#ansible-playbook stat.yml
PLAY [websrvs]
********************************************************************************
***************************************
TASK [Gathering Facts]
********************************************************************************
*******************************
ok: [10.0.0.7]
ok: [10.0.0.8]
TASK [check file]
********************************************************************************
************************************
ok: [10.0.0.7]
ok: [10.0.0.8]
TASK [debug]
********************************************************************************
*****************************************
ok: [10.0.0.7] => {
"msg": "/data/mysql is not exist"
}
ok: [10.0.0.8] => {
"msg": "/data/mysql is not exist"
}
PLAY RECAP
********************************************************************************
*******************************************
10.0.0.7 : ok=3 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
10.0.0.8 : ok=3 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0

unarchive 模块

功能:解包解压缩
实现有两种用法:

  • 将ansible主机上的压缩包传到远程主机后解压缩至特定目录,设置remote_src=no,此为默认值,可省略
  • 将远程本主机上或非ansible的其它主机的某个压缩包解压缩到远程主机本机的指定路径下,需要设置remote_src=yes

常见参数:

remote_src #和copy功能一样且选项互斥,yes表示源文件在远程被控主机或其它非ansible的其它主机上,no表示文件在ansible主机上,默认值为no, 此选项代替copy选项
copy #默认为yes,当copy=yes,拷贝的文件是从ansible主机复制到远程主机上,如果设置为copy=no,会在远程主机上寻找src源文件,此选项已废弃
src #源路径,可以是ansible主机上的路径,也可以是远程主机(被管理端或者第三方主机)上的路径,如果是远程主机上的路径,则需要设置remote_src=yes
dest #远程主机上的目标路径
mode #设置解压缩后的文件权限
creates=/path/file #当绝对路径/path/file不存在时才会执行

范例:

ansible all -m unarchive -a 'src=/data/foo.tgz dest=/var/lib/foo owner=wang group=bin'

ansible all -m unarchive -a 'src=/tmp/foo.zip dest=/data  mode=0777'

ansible all -m unarchive -a 'src=https://example.com/example.zip dest=/data '

ansible websrvs -m unarchive -a 'src=https://releases.ansible.com/ansible/ansible-2.1.6.0-0.1.rc1.tar.gz dest=/data/ owner=root remote_src=yes'

ansible websrvs -m unarchive -a 'src=http://nginx.org/download/nginx- 1.18.0.tar.gz dest=/usr/local/src/ remote_src=yes''

Archive 模块

功能:打包压缩保存在被管理节点

常见选项

path #压缩的文件或目录
dest #压缩后的文件
format #压缩格式,支持gz,bz2,xz,tar,zip

范例:

ansible websrvs -m archive -a 'path=/var/log/ dest=/data/log.tar.bz2 format=bz2 owner=wang mode=0600'

Hostname 模块

功能:管理主机名
常见选项

name #修改后的主机名称

范例:

ansible node1 -m hostname -a "name=websrv"
ansible 10.0.0.18 -m hostname -a 'name=node18.wang.org'

Cron 模块

功能:计划任务
支持时间:minute,hour,day,month,weekday
常见选项

name #描述脚本的作用
minute #分钟
hour #小时
weekday #周
user #任务由哪个用户运行;默认root
job #任务

范例:

#备份数据库脚本
[root@centos8 ~]#cat /root/mysql_backup.sh
#!/bin/bash
mysqldump -A -F --single-transaction --master-data=2 -q -uroot |gzip > /data/mysql_`date +%F_%T`.sql.gz
#创建任务
ansible 10.0.0.8 -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql" job=/root/mysql_backup.sh'

ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate ntp.aliyun.com &>/dev/null' name=Synctime"

#禁用计划任务
ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1 &>/dev/null' name=Synctime disabled=yes"

#启用计划任务
ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1 &> /dev/null' name=Synctime disabled=no"

#删除任务
ansible websrvs -m cron -a "name='backup mysql' state=absent"
ansible websrvs -m cron -a 'state=absent name=Synctime'

Yum 和 Apt 模块

功能:管理软件包
yum 管理软件包,只支持RHEL,CentOS,fedora,不支持Ubuntu其它版本
apt 模块管理 Debian 相关版本的软件包
yum常见选项

name #软件包名称
state #状态
=present #安装,此为默认值
=absent #删除
=latest #最新版
list #列出指定包
enablerepo #启用哪个仓库安装
disablerepo #不使用哪些仓库的包
exclude #排除指定的包
validate #是否检验,默认为yes

范例:

[root@ansible ~]#ansible websrvs -m yum -a 'name=httpd state=present'
#安装zabbix agent rpm包

[root@ansible ~]#ansible websrvs -m yum -a 'name=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/8/x86_64/zabbix-agent2-5.0.24-1.el8.x86_64.rpm state=present validate_certs=no'

#启用epel源进行安装
[root@ansible ~]#ansible websrvs -m yum -a 'name=nginx state=present enablerepo=epel'

#升级除kernel和foo开头以外的所有包
[root@ansible ~]#ansible websrvs -m yum -a 'name=* state=lastest exclude=kernel*,foo*'

#删除
[root@ansible ~]#ansible websrvs -m yum -a 'name=httpd state=absent'
[root@ansible ~]#ansible websrvs -m yum -a 'name=sl,cowsay'

yum_repository 模块

功能: 此模块实现yum的仓库配置管理
常见选项

name #仓库id
description #仓库描述名称,对应配置文件中的name=
baseurl #仓库的地址
gpgcheck #验证开启
gpgkey #仓库公钥路径
state=absen  #删除

范例:

ansible websrvs -m yum_repository -a 'name=ansible_nginx description="nginx repo" baseurl="http://nginx.org/packages/centos/$releasever/$basearch/" gpgcheck=yes gpgkey="https://nginx.org/keys/nginx_signing.key"'

[root@rocky8 ~]#cat /etc/yum.repos.d/ansible_nginx.repo
[ansible_nginx]
baseurl = http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck = 1
gpgkey = https://nginx.org/keys/nginx_signing.key
name = nginx repo

Service 模块

此模块和sytemd功能相似,选项很多相同
功能:管理服务
常见选项

name #服务名称
state #服务状态
=started #启动
=stopped #停止
=restarted #重启
=reloaded #重载
enabled #开启自启动
daemon_reload #加载新的配置文件,适用于systemd模块

范例:

ansible all -m service -a 'name=httpd state=started enabled=yes'
ansible all -m service -a 'name=httpd state=stopped'
ansible all -m service -a 'name=httpd state=reloaded'
ansible all -m shell -a "sed -i 's/^Listen 80/Listen 8080/'
/etc/httpd/conf/httpd.conf"
ansible all -m service -a 'name=httpd state=restarted'
#重启动指定网卡服务
ansible all -m service -a 'name=network state=absent args=eth0'

User 模块

功能:管理用户
常见选项

name #创建的名称
uid #指定uid
group #指定基本组
shell #登录shell类型默认/bin/bash
create_home #是否创建家目录
password #设定对应的密码,必须是加密后的字符串才行,否则不生效
system #yes表示系统用户
groups #附加组
append #追加附加组使用,yes表示增加新的附加组
state #absen删除
remove #yes表示删除用户时将家目录一起删除
generate_ssh_key #创建私钥
ssh_keyu_bits #私钥位数
ssh_key_file #私钥文件路径

范例:

#创建用户
ansible all -m user -a 'name=user1 comment="test user" uid=2048 home=/app/user1group=root'
ansible all -m user -a 'name=nginx comment=nginx uid=88 group=nginxgroups="root,daemon" shell=/sbin/nologin system=yes create_home=nohome=/data/nginx non_unique=yes'
#remove=yes表示删除用户及家目录等数据,默认remove=no
ansible all -m user -a 'name=nginx state=absent remove=yes'
#生成123456加密的密码
ansible localhost -m debug -a "msg={{ '123456'| password_hash('sha512','salt')}}" localhost | SUCCESS => { "msg": "$6$salt$MktMKPZJ6t59GfxcJU20DwcwQzfMvOlHFVZiOVD71w."
}
#用上面创建的密码创建用户
ansible websrvs -m user -a 'name=www group=www system=yes shell=/sbin/nlogin password="$6$salt$MktMKPZJ6t59GfxcJU20DwcwQzfMvOlHFVZiOVD71w."'
#创建用户test,并生成4096bit的私钥
ansible websrvs -m user -a 'name=test generate_ssh_key=yes ssh_key_bits=4096 ssh_key_file=.ssh/id_rsa'

Group 模块

功能:管理组
常见选项

name #指定组名称
gid #指定gid
state
=present #创建,默认
=absent #删除

范例:

#创建组
ansible websrvs -m group -a 'name=nginx gid=88 system=yes'
#删除组
ansible websrvs -m group -a 'name=nginx state=absent'

Lineinfile 模块

ansible在使用sed进行替换时,经常会遇到需要转义的问题,而且ansible在遇到特殊符号进行替换时,
会存在问题,无法正常进行替换 。

ansible自身提供了两个模块:lineinfile模块和replace模块,可以方便的进行替换一般在ansible当中去修改某个文件的单行进行替换的时候需要使用lineinfile模块
功能:相当于sed,主要用于修改一行的文件内容
常见选项

path #被控端文件的路径
regexp #正则匹配语法格式,表示被替换的内容
line #替换为的内容
state #absent表示删除
insertafter #插入到替换内容前面,如和regexp同时存在,只在没找到与regexp匹配时才使用
insertafter
insertbefore #插入到替换内容后面,如和regexp同时存在,只在没找到与regexp匹配时才使用
insertafter
backrefs #支持后面引用,yes和no
backup #修改前先备份
create #如果文件不存在,则创建,默认不存在会出错
mode #指定权限
owner #指定用户
group #指定组
#注意
regexp参数 :使用正则表达式匹配对应的行,当替换文本时,如果有多行文本都能被匹配,则只有最后面被
匹配到的那行文本才会被替换,当删除文本时,如果有多行文本都能被匹配,这么这些行都会被删除。

注意: 如果想进行多行匹配进行替换需要使用replace模块
范例:

#修改监听端口
ansible websrvs -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='^Listen' line='Listen 8080'"

#修改SELinux
ansible all -m lineinfile -a "path=/etc/selinux/config regexp='^SELINUX='line='SELINUX=disabled'"

#添加网关
ansible webservers -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 line="GATEWAY=10.0.0.254"'

#给主机增加一个网关,但需要增加到NAME=下面
ansible webservers -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 insertafter="^NAME=" line="GATEWAY=10.0.0.254"'
#效果如下
cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
NAME=eth0
GATEWAY=10.0.0.254
#给主机增加一个网关,但需要增加到NAME=上面
ansible webservers -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-
eth0 insertbefore="^NAME=" line="GATEWAY=10.0.0.254"'
#效果如下
cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
GATEWAY=10.0.0.254
NAME=eth0
#删除网关
ansible webservers -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 regexp="^GATEWAY" state=absent'
#删除#开头的行
ansible all -m lineinfile -a 'dest=/etc/fstab state=absent regexp="^#"'

Replace 模块

该模块有点类似于sed命令,主要也是基于正则进行匹配和替换,建议使用
功能: 多行修改替换
常见选项

path #被控端文件的路径
regexp #正则匹配语法格式,表示被替换的内容
replace #替换为的内容
after #插入到替换内容前面,
before #插入到替换内容后面
backup #修改前先备份
mode #指定权限
owner #指定用户
group #指定组

范例

ansible all -m replace -a "path=/etc/fstab regexp='^(UUID.*)' replace='#\1'"
ansible all -m replace -a "path=/etc/fstab regexp='^#(UUID.*)' replace='\1'"

SELinux 模块

功能: 该模块管理 SELInux 策略
常见选项

policy #指定SELINUXTYPE=targeted
state #指定SELINUX=disabled

范例

[root@rocky ansible-apps]# ansible 192.168.32.132 -m selinux -a 'state=disabled'
192.168.32.132 | FAILED! => {
    "msg": "The module selinux was redirected to ansible.posix.selinux, which could not be loaded."
}

# ansible版本2.13.3出现如下错误
 "msg": "The module selinux was redirected to ansible.posix.selinux, which could not be loaded."
 
 # 解决方法
 [root@rocky ansible-apps]# ansible-galaxy collection install ansible.posix


# 再次执行,显示成功
[root@rocky ansible-apps]# ansible 192.168.32.132 -m selinux -a 'state=disabled'
[WARNING]: SELinux state temporarily changed from 'enforcing' to 'permissive'. State change will take effect next reboot.
192.168.32.132 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "configfile": "/etc/selinux/config",
    "msg": "Config SELinux state changed from 'enforcing' to 'disabled'",
    "policy": "targeted",
    "reboot_required": true,
    "state": "disabled"
}

reboot 模块

功能: 重启
常见选项

msg #重启提示
pre_reboot_delay #重启前延迟时间的秒数
post_reboot_delay #重启后延迟时间的秒数后,再验证系统正常启动
reboot_timeout #重启后延迟时间再执行测试成功与否的命令
test_command #执行测试成功与否的命令

范例:

[root@ansible ~]#ansible websrvs -m reboot -a 'msg="host will be reboot"'

mount 模块

功能: 挂载和卸载文件系统
常见选项

src #源设备路径,或网络地址
path #挂载至本地哪个路径下
fstype #设备类型; nfs
opts #挂载的选项
state #挂载还是卸载
=present #永久挂载,但没有立即生效
=absent #卸载临时挂载,并删除永久挂载
=mounted #临时挂载
=unmounted #临时卸载

范例:

#修改fstab文件永久挂载,但不立即生效
mount websrvs -m mount -a 'src="UUID=b3e48f45-f933-4c8e-a700-22a159ec9077" path=/home fstype=xfs opts=noatime state=present'
#临时取消挂载
mount websrvs -m mount -a 'path=/home fstype=xfs opts=noatime state=unmounted'
#永久挂载,并立即生效
ansible websrvs -m mount -a 'src=10.0.0.8:/data/wordpress path=/var/www/html/wp- content/uploads opts="_netdev" state=mounted'
#永久卸载,并立即生效
ansible websrvs -m mount -a 'src=10.0.0.8:/data/wordpress path=/var/www/html/wp- content/uploads state=absent'

Setup 模块

功能: setup 模块来收集主机的系统信息,这些 facts 信息可以直接以变量的形式使用,但是如果主机
较多,会影响执行速度
可以使用 gather_facts: no 来禁止 Ansible 收集 facts 信息
常见选项

filter #指定过滤条件

范例:

ansible all -m setup
ansible all -m setup -a "filter=ansible_nodename"
ansible all -m setup -a "filter=ansible_hostname"  #  主机名称
ansible all -m setup -a "filter=ansible_domain"
ansible all -m setup -a "filter=ansible_memtotal_mb"
ansible all -m setup -a "filter=ansible_memory_mb"
ansible all -m setup -a "filter=ansible_memfree_mb"
ansible all -m setup -a "filter=ansible_os_family"
ansible all -m setup -a "filter=ansible_distribution"
ansible all -m setup -a "filter=ansible_distribution_major_version"
ansible all -m setup -a "filter=ansible_distribution_version"
ansible all -m setup -a "filter=ansible_processor_vcpus"
ansible all -m setup -a "filter=ansible_all_ipv4_addresses"
ansible all -m setup -a "filter=ansible_architecture"
ansible all -m setup -a "filter=ansible_uptime_seconds"
ansible all -m setup -a "filter=ansible_processor*"
ansible all -m setup -a 'filter=ansible_env'

范例:

[root@ansible ~]#ansible all -m setup -a 'filter=ansible_python_version'
10.0.0.7 | SUCCESS => {
"ansible_facts": {
"ansible_python_version": "2.7.5",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
10.0.0.6 | SUCCESS => {
"ansible_facts": {
"ansible_python_version": "2.6.6",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
10.0.0.8 | SUCCESS => {
"ansible_facts": {
"ansible_python_version": "3.6.8",
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false
}
[root@ansible ~]#

范例:取IP地址

#取所有IP
ansible 10.0.0.101 -m setup -a 'filter=ansible_all_ipv4_addresses'
10.0.0.101 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.0.1",
"192.168.0.2",
"192.168.64.238",
"192.168.13.36",
"10.0.0.101",
"172.16.1.0",
"172.17.0.1"
]
},
"changed": false
}
#取默认IP
ansible all -m setup -a 'filter="ansible_default_ipv4"'
10.0.0.101 | SUCCESS => {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "10.0.0.101",
"alias": "eth0",
"broadcast": "10.0.0.255",
"gateway": "10.0.0.2",
"interface": "eth0",
"macaddress": "00:0c:29:e8:c7:9b",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "10.0.0.0",
"type": "ether"
}
},
"changed": false
}

debug 模块

功能: 此模块可以用于输出信息,并且通过 msg 定制输出的信息内容,功能类似于echo命令
注意: msg后面的变量有时需要加 " " 引起来
常见选项

msg #指定命令输出的信息
var #指定变量名,和msg互斥
verbosity #详细度

范例: debug 模块默认输出Hello world

[root@ansible ~]#ansible 10.0.0.18 -m debug
10.0.0.18 | SUCCESS => {
"msg": "Hello world!"
}
[root@ansible ansible]#cat debug.yml
---
- hosts: websrvs
tasks:
- name: output Hello world
debug:
#默认没有指定msg,默认输出"Hello world!"
[root@ansible ansible]#ansible-playbook debug.yml
.....
TASK [output variables]
********************************************************************************
******************************
ok: [10.0.0.7] => {
"msg": "Hello world!"
}
ok: [10.0.0.8] => {
"msg": "Hello world!"
}
PLAY RECAP
********************************************************************************
*******************************************
10.0.0.7 : ok=2 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
10.0.0.8 : ok=2 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0

范例: 利用debug 模块输出变量

[root@centos8 ~]#cat debug.yaml
---
- hosts: websrvs
tasks:
- name: output variables
debug:
msg: Host "{{ ansible_nodename }}" Ip "{{ ansible_default_ipv4.address
}}"
[root@centos8 ~]#ansible-playbook debug.yaml
PLAY [websrvs]
********************************************************************************
***************************************
TASK [Gathering Facts]
********************************************************************************
*******************************
ok: [10.0.0.7]
ok: [10.0.0.8]
TASK [output variables]
********************************************************************************
******************************
ok: [10.0.0.7] => {
"msg": "Host \"centos7.wangxiaochun.com\" Ip \"10.0.0.7\""
}
ok: [10.0.0.8] => {
"msg": "Host \"centos8.wangxiaochun.com\" Ip \"10.0.0.8\""
}
PLAY RECAP
********************************************************************************
*******************************************
10.0.0.7 : ok=2 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
10.0.0.8 : ok=2 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0

范例: 显示字符串特定字符

# cat debug.yml
- hosts: all
  gather_facts: no
  vars:
    a: "12345"
  tasks:
  - debug:
    msg:
      - "{{a[0]}}"
      - "{{a[1]}}"
      - "{{a[2]}}"
#定义了一个字符串变量a,如果想要获取a字符串的第3个字符,则可以使用”a[2]”获取,索引从0开始,执行上例playbook,debug的输出信息如下:
TASK [debug] *************************
ok: [test1] => {
"msg": "1"
"msg": "2"
"msg": "3"
}

sysctl 模块

功能: 修改内核参数
常见选项

name #内核参数
value #指定值
state #是否保存在sysctl.conf文件中,默认present
sysctl_set #使用sysctl -w 验证值生效

范例:

ansible websrvs -m sysctl -a 'name=net.ipv4.ip_forward value=1 state=present'

范例: 内核参数优化

- name: Change Port Range
  sysctl:
    name: net.ipv4.ip_local_port_range
  value: '1024 65000'
  sysctl_set: yes
- name: Enabled Forward
  sysctl:
  name: net.ipv4.ip_forward
  value: '1'
  sysctl_set: yes
- name: Enabled tcp_reuse
  sysctl:
    name: net.ipv4.tcp_tw_reuse
    value: '1'
    sysctl_set: yes
- name: Chanage tcp tw_buckets
  sysctl:
    name: net.ipv4.tcp_max_tw_buckets
    value: '5000'
    sysctl_set: yes
- name: Chanage tcp_syncookies
  sysctl:
    name: net.ipv4.tcp_syncookies
    value: '1'
    sysctl_set: yes
- name: Chanage tcp max_syn_backlog
  sysctl:
    name: net.ipv4.tcp_max_syn_backlog
    value: '8192'
    sysctl_set: yes
- name: Chanage tcp Established Maxconn
  sysctl:
    name: net.core.somaxconn
    value: '32768'
    sysctl_set: yes
    state: present
- name: Chanage tcp_syn_retries
  sysctl:
    name: net.ipv4.tcp_syn_retries
    value: '2'
    sysctl_set: yes
    state: present
- name: Chanage net.ipv4.tcp_synack_retries
  sysctl:
    name: net.ipv4.tcp_synack_retries
    value: '2'
    sysctl_set: yes
    state: presen

pam_limits

功能: 管理资源限制
范例

- name: Change Limit /etc/security/limit.conf
  pam_limits:
  domain: "*"
    limit_type: "{{ item.limit_type }}"
    limit_item: "{{ item.limit_item }}"
    value: "{{ item.value }}"
  loop:
    - { limit_type: 'soft', limit_item: 'nofile',value: '100000' }
    - { limit_type: 'hard', limit_item: 'nofile',value: '10000' }

apt_repository 模块

功能: 此模块实现apt的仓库配置管理
常见选项

repo #仓库信息
state #添加或删除
update_cache #是否apt update,默认yes
filename #仓库文件,默认放在/etc/apt/sources.list.d/file.list

范例:

ansible ubuntu-servers -m apt_repository -a 'repo="deb
http://archive.canonical.com/ubuntu focal partner" filename=google-chrome'
[root@ubuntu2004 ~]#cat /etc/apt/sources.list.d/google-chrome.list
deb http://archive.canonical.com/ubuntu focal partner

apt_key 模块

功能: 添加和删除apt key
常见选项

url #key路径
state #添加或删除

范例: 生成ceph仓库配置

#先导入key,注意先后顺序
ansible ubuntu-servers -m apt_key -a
'url=https://download.ceph.com/keys/release.asc state=present'
#再生成apt配置,如果不导入key此步会出错
ansible ubuntu-servers -m apt_repository -a 'repo="deb
http://mirror.tuna.tsinghua.edu.cn/ceph/debian-pacific focal main"
filename=ansible_ceph'
#验证结果
[root@ubuntu2004 ~]#cat /etc/apt/sources.list.d/ansible_ceph.list
deb http://mirror.tuna.tsinghua.edu.cn/ceph/debian-pacific focal main

其它模块

ansible 还提供了很多针对各种应用的模块,比如

nginx_status_info
nginx_status_facts
mysql_db #需要安装MySQL-python包
mysql_user #需要安装MySQL-python包
redis
mongodb*
postgresql*
haproxy
git
0

评论区