Ansible_常用模块

发布于 2024-03-05  40 次阅读


Ansible ad-hoc

什么是ad-hoc?
ad-hoc简而言之就是“临时命令”,执行完即结束,并不会保存

ad-hoc模式的使用场景:
比如在多台机器上查看某个进程是否启动,或拷贝指定文件到本地,等等

ad-hoc模式的命令使用:

image-20230519141755557

ad-hoc结果返回颜色

  • 绿色: 代表被管理端主机没有被修改
  • 黄色: 代表被管理端主机发现变更
  • 红色: 代表出现了故障,注意查看提示

ad-hoc常用模块

command             # 执行shell命令(不支持管道等特殊字符)
shell               # 执行shell命令
scripts             # 执行shell脚本
yum_repository      # 配置yum仓库
yum                 # 安装软件
copy                # 变更配置文件
file                # 建立目录或文件
service             # 启动与停止服务
mount               # 挂载设备
cron                # 定时任务
get_url             #下载软件
firewalld           #防火墙
selinux             #selinux

Ansible-doc帮助手册

[root@m01 ~]# ansible-doc -l        # 查看所有模块说明
[root@m01 ~]# ansible-doc copy      # 查看指定模块方法
[root@m01 ~]# ansible-doc -s copy   # 查看指定模块参数

Ansible命令模块

command:可以执行不复杂的系统命令(不支持管道符等特殊符号)

[root@m01 ~]# ansible web01 -m command -a 'df -h'
web01 | CHANGED | rc=0 >>
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   15G  1.8G   14G  12% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sda1               1014M  133M  882M  14% /boot
tmpfs                     98M     0   98M   0% /run/user/0
[root@m01 ~]# ansible web01 -m command -a 'ps -ef | grep nginx'
web01 | FAILED | rc=1 >>
error: garbage option

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).non-zero return code

shell:支持复杂的系统命令

# 如果需要一些管道操作,则使用shell
[root@m01 ~]# ansible web01 -m shell -a 'ps -ef | grep nginx'
web01 | CHANGED | rc=0 >>
root       9542   9541  0 15:09 pts/1    00:00:00 /bin/sh -c ps -ef | grep nginx
root       9544   9542  0 15:09 pts/1    00:00:00 grep nginx

script:执行在管理机上的脚本

# 编写脚本
[root@m01 ~]# vim /root/yum.sh
#!/usr/bin/bash
yum install -y vsftpd

#在本地运行模块,等同于在远程执行,不需要将脚本文件进行推送目标主机执行
[root@m01 ~]# ansible web_group -m script -a "/root/yum.sh"

Ansible软件管理模块

yum

name:指定软件名
state:指定状态
    - present/installed:安装软件
    - absent/removed:卸载软件
    - latest:安装最新版本
    - download_dir:指定rpm的下载目录
    - download_only:只下载不安装
        - yes/true
        - no/false (默认)
----------------------------------------------------------------------------------------------------
yum安装方式:
    -从yum仓库安装 yum install -y 软件名
    -从指定网站安装 yum install-y http://test.driverzeng.com/MySQL_plugins/Atlas2.2.1.el6.x86_64.rpm
    -本地安装 yum localinstall -y Atlas-2.2.1.el6.x86_64.rpm
#案列:安装apache
#yum仓库安装
 [root@m01 ~]# ansible web_group -m yum -a 'name=httpd state=present'
# 网站安装
[root@m01 ~]# ansible lb_group -m yum -a 'name=http://test.driverzeng.com/MySQL_plugins/Atlas-2.2.1.el6.x86_64.rpm state=present'
#本地安装
[root@m01 ~]# ansible lb_group -m yum -a 'name=/root/Atlas-2.2.1.el6.x86_64.rpm state=present'
#卸载apache
[root@m01 ~]# ansible lb_group -m yum -a 'name=httpd state=absent'
#只下载不安装apache
[root@m01 ~]# ansible lb_group -m yum -a 'name=httpd download_only=true download_dir=/tmp state=present'

yum_repository

#案列
- name: Add repository
  yum_repository:
    name: epel
    description: EPEL YUM repo
    baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
 -----------------------------------------------------------------------------------------------------
 #ansible的yum仓库写法和我们本地的yum源大同小异,只是name区别不同
 #本地格式写法
 [epel]  // yum仓库名称
name=Extra Packages for Enterprise Linux - $basearch    // yum仓库的描述
baseurl=http://mirrors.aliyun.com/epel/7/$basearch      // yum仓库地址
enabled=1       // 是否开启yum仓库 开启1 关闭0
gpgcheck=0  // 是否检查秘钥 检查1 不检查0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 // 秘钥的路径位置
#ansible格式写法
file:yum仓库的文件名
name:指定yum仓库名称(如果没有file,以name来命名文件名,如果有file,以file来命名文件名)
description:yum仓库描述
baseurl:yum仓库地址
enabled:是否开启yum仓库
    - yes 默认
    - no
gpgcheck:是否检查秘钥 检查1 不检查0
    - yes/true 检查
    - no/false 不检查
gpgkey:秘钥的路径
-----------------------------------------------------------------------------------------------------
#实践案列:
# 创建yum仓库
[root@web01 ~]# ansible all -m yum_repository -a 'name=jiujiu description="base" baseurl=http://www.baidu.com enabled=yes'
#查看是否创建成功
[root@lb01 ~]# cat /etc/yum.repos.d/jiujiu.repo
[jiujiu]
baseurl = http://www.baidu.com
enabled = 1
name = base
# 使用file动作,往配置文件中追加yum仓库
ansible all -m yum_repository -a 'file=jiujiu name=elpo description="elpo"
baseurl=http://www.baidu.com enabled=yes'
#查看
[root@lb01 ~]# cat /etc/yum.repos.d/jiujiu.repo
[jiujiu]
baseurl = http://www.baidu.com
enabled = 1
name = base

[elpo]
baseurl=http://www.baidu.com
enabled=1
name=elpo
## 删除yum仓库
[root@m01 ~]# ansible all -m yum_repository -a 'name=jiujiu state=absent'   
[root@m01 ~]# ansible all -m yum_repository -a 'file=jiujiu  name=elpo state=absent'

Ansible文件管理模块

copy:等同于cp scp chmod chown echo source

src:指定源文件路径
dest:指定目标路径
owner:指定属主
group:指定属组
mode:指定文件权限
backup:是否备份目标路径已存在的同名文件
    - yes
    - no 默认
remote_src:拷贝的文件是否在远端
    - yes
    - no 默认
content:指定字符串,写入到文件中
#下发文件
[root@m01 ~]# ansible all -m copy -a 'src=/root/lb.conf dest=/etc/nginx/conf.d'
# 备份重名文件   //必须是文件有所修改才能
 [root@m01 ~]# ansible all -m copy -a 'src=/root/lb.conf dest=/etc/nginx/conf.d backup=yes'
 # cp命令(源文件在远端目录)
 [root@m01 ~]# ansible all -m copy -a 'src=/root/lb.conf dest=/etc/nginx/conf.d remote_src=yes'
 ## 修改权限和属主属组
 [root@m01 ~]# ansible all -m copy -a 'src=/root/lb.conf dest=/etc/nginx/conf.d owner=nginx group=root mode=111'
 # content   //同echo追加相同
 [root@m01 ~]# ansible all -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid
=666,anongid=666)" dest=/etc/exports'

file: 等同于touch mkdir ln chmod chown rm

path:指定创建文件或目录的路径
owner:指定属主
group:指定属组
mode:指定权限
ln -s /etc/passwd /tmp/pass
src:软硬链接的源文件
dest:软硬链接的目标路径
state:
    - absent 删除文件或目录 rm
    - directory 创建目录 mkdir
    - file 修改文件或目录属性 chmod chown
    - hard 创建硬链接 ln
    - link 创建软连接 ln -s
    - touch 创建文件或修改文件时间戳 touch
-----------------------------------------------------------------------------------------------------
# 创建文件
ansible web02 -m file -a 'path=/root/123.txt owner=root group=nginx mode=622 state=touch'
# 创建目录
[root@m01 ~]# ansible web02 -m file -a 'path=/root/123 state=directory'
# 删除文件或目录
[root@m01 ~]# ansible web02 -m file -a 'path=/root/123 state=absent'
# 创建软硬链接
[root@m01 ~]# ansible web02 -m file -a 'src=/etc/passwd dest=/tmp/pass state=link'  //软链接
[root@m01 ~]# ansible web02 -m file -a 'src=/etc/passwd dest=/opt/pass state=hard'  //硬链接
# 修改权限(file 前提:该文件必须存在)
[root@m01 ~]# ansible web02 -m file -a 'path=/tmp/passwd owner=nginx group=nginx'

get_url:软件下载模块 wget curl

url:指定下载地址
dest:指定目标路径
owner:指定属主
group:指定属组
mode:指定权限
checksum:加密验证
    - md5
    - sha256
#下载
ansible web02 -m get_url -a 'url=https://downloads.mysql.com/archives/get/p/23/file/mysql-5.6.38.tar.gz dest=/root'

Ansible服务管理模块

service

#服务启停
name:指定服务名
state:
    - started 启动服务 systemctl start nginx
    - restarted 重启服务 systemctl restart nginx
    - reloaded 重新加载服务 systemctl reload nginx
    - stopped 停止服务 systemctl stop nginx
    - enabled:添加开机自启 systemctl enable   nginx
        - yes
        - no 默认

systemd

服务启停
name:指定服务名
state:
    - started 启动服务 systemctl start nginx
    - restarted 重启服务 systemctl restart nginx
    - reloaded 重新加载服务 systemctl reload nginx
    - stopped 停止服务 systemctl stop nginx
    - enabled:添加开机自启 systemctl enable nginx
        - yes
        - no 默认
daemon_reload:更新systemd启动脚本
    - yes
    - no 默认
masked:禁止服务启动
    - yes
    - no 默认

Ansible用户管理模块

group

#同Linux创建组大同小异 groupadd
name:指定组名
gid:指定组id
 state:
    - present:创建
    - absent:删除
#案列:创建一个www的组
ansible all -m group -a 'name=www gid=666'

user

#同Linux创建用户大同小异 useradd
-u:指定用户uid
-g:指定用户组或组id
-s:指定用户登录的shell
-M:不创建家目录
-m:创建家目录(迁移家目录)
-G:指定用户的附加组
-a:追加附加组
-c:指定用户注释信息
-d:迁移家目录,指定家目录的位置
-r:创建系统uid用户
-----------------------------------------------------------------------------------------------------
name:指定用户名
comment:指定用户注释信息。同useradd -c
uid:指定用户的uid,同useradd -u
group:指定用户组或组id,同useradd -g
shell:-s 指定用户的登录shell,同useradd -s
groups:-G 指定用户的附加组,同useradd -G
append:-a 是否追加附加组,同useradd -a
    - yes
    - no
state:
    - present
    - absent
remove:递归删除,将用户的家目录和相关文件一并删除,同userdel -r
    - yes
    - no
move_home:迁移家目录,同useradd -d
    - yes
    - no
create_home:是否创建家目录
    - yes:    同useradd -m
    - no:     同useradd -M
#案列:创建一个www的用户
ansible all -m user -a 'name=www uid=666 group=666 shell=/sbin/nologin create_home=no'

Ansible定时任务模块

#定时任务写法
# 注释信息
* * * * * 命令 &>/dev/null
分 时 日 月 周
------------------------------------------------------------------------------------------------------
name:指定定时任务的注释信息
minute:分钟
hour:小时
day:天
month:月
weekday:周
job:定时任务的命令
state:
- present
- absent
#案列:定时任务同步时间
## 安装ntpdate
[root@m01 ~]# ansible web01 -m yum -a 'name=ntpdate state=present'
# 创建定时任务
[root@m01 ~]# ansible web01 -m cron -a'name="sync time" minute=*/5 job="/sbin/ntpdate time1.aliyun.com &>/dev/null" state=present'
#修改定时任务(指定定时任务名字)
[root@m01 ~]# ansible all -m cron -a 'name="sync time" minute=*/1 hour=*/2 job="/sbin/ntpdate time1.aliyun.com &>/dev/null" state=present'
# 删除定时任务
[root@m01 ~]# ansible all -m cron -a 'name="sync time" state=absent'

Ansible磁盘挂载模块

mount

path:挂载目录
src:挂载源(挂载点nfs)
fstype:file system type指定文件系统
state:
    - mounted // 挂载,挂载磁盘并写入/etc/fstab文件中
    - present // 挂载,只写入/etc/fstab文件中
    - unmounted // 卸载,仅卸载,不清空/etc/fstab文件
    - absent // 卸载,卸载并清空/etc/fstab文件
# 挂载 mounted 对应卸载 absent
[root@m01 ~]# ansible web_group -m mount -a 'path=/var/www/html/user_data src=172.16.1.31:/data fstype=nfs state=mounted'
# 卸载 umount /var/www/html/user_data
ansible web_group -m mount -a 'path=/var/www/html/user_data state=unmounted'

Ansible防火墙模块

selinux

state:
    - enfocing 打开
    - diabled 关闭
    - permissive  临时关闭
# 关闭selinux
[root@m01 ~]# ansible all -m selinux -a 'state=disabled'

firewalld

# 开启防火墙
[root@m01 ~]# ansible all -m service -a 'name=firewalld state=started'
service:指定开启的服务
state:
    - enabled 开启
    - disabled 禁止
## 开启防火墙指定服务(临时开启,重启后失效)
[root@m01 ~]# ansible web_group -m firewalld -a 'service=http state=enabled'
# 开启防火墙指定服务(永久开启,需要重启防火墙)
[root@m01 ~]# ansible web_group -m firewalld -a 'service=https permanent=yes state=enabled'
# 放行端口
[root@m01 ~]# ansible web_group -m firewalld -a 'port=80/tcp state=enabled'

Ansible获取主机信息模块

#看到每台主机中所有变量
[root@m01 ~]# ansible web01 -m setup -a
#查看主机名
[root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_hostname'
web01 | SUCCESS => {
    "ansible_facts": {
        "ansible_hostname": "web01", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

ansible_all_ipv4_addresses:仅显示ipv4的信息。
ansible_devices:仅显示磁盘设备信息。
ansible_distribution:显示是什么系统,例:centos,suse等。
ansible_distribution_major_version:显示是系统主版本。
ansible_distribution_version:仅显示系统版本。
ansible_machine:显示系统类型,例:32位,还是64位。
ansible_eth0:仅显示eth0的信息。
ansible_hostname:仅显示主机名。
ansible_kernel:仅显示内核版本。
ansible_lvm:显示lvm相关信息。
ansible_memtotal_mb:显示系统总内存。
ansible_memfree_mb:显示可用系统内存。
ansible_memory_mb:详细显示内存情况。
ansible_swaptotal_mb:显示总的swap内存。
ansible_swapfree_mb:显示swap内存的可用内存。
ansible_mounts:显示系统磁盘挂载情况。
ansible_processor:显示cpu个数(具体显示每个cpu的型号)。
ansible_processor_vcpus:显示cpu个数(只显示总的个数)。

Ansible解压模块:

unarchive
    src:要解压的源文件
    dest:解压到的目标路径
    remote_src:要解压的包在远端的服务器上
        - yes
        - no
    owner:属主
    group:属组
    mode:权限
#案列
ansible nfs -m unarchive -a 'src=/root/sersync.tar.gz dest=/code  owner=www group=www mode
= 0666 remote_src=yes'

数据库模块

# 数据库用户管理模块 
#在mysql数据库里面创建:grant all on wordpress.* to wp_user@'172.16.1.%' identified by '123'
mysql_user
    name:数据库用户名 wp_user
    host:数据库主机ip 172.16.1.%
    password:数据库密码 123
    priv:指定权限privileges 'wordpress.*:ALL'
    state:
        - present
        - absent
# 数据库库管理模块
mysql_db
    name:指定库名
    state:
        - present 创建数据库
        - absent 删除数据库
        - import 导入数据
        - dump 导出数据
    target:指定导入/导出的数据文件
# 数据库主从集群管理模块
mysql_replication
管理 MySQL 服务器复制、副本、主要状态、获取和更改主要主机