Ansible(二)常用模块

系列文章

Ansible 基础(一)Ansible 安装

hostname

hostname 模块用于修改主机名,文档:https://docs.ansible.com/ansible/2.9/modules/hostname_module.html

# 查看所有主机
[root@192 ~]# ansible --list-hosts all
  hosts (5):
    redis
    nginx
    mysql
    php1
    php2
# 将主机清单中的所有主机设置主机名
[root@192 ~]# ansible redis -m hostname -a 'name=redis.server'
[root@192 ~]# ansible nginx -m hostname -a 'name=nginx.server'
[root@192 ~]# ansible mysql -m hostname -a 'name=mysql.server'
[root@192 ~]# ansible php1 -m hostname -a 'name=php1.server'
[root@192 ~]# ansible php2 -m hostname -a 'name=php2.server'

file

file 模块用于对文件相关的操作(创建, 删除, 软硬链接等),文档:https://docs.ansible.com/ansible/2.9/modules/file_module.html

stat 值:
- absent		目录将被递归地删除,文件或符号链接将被解除链接。
- directory		如果目录不存在,则创建所有中间子目录。
- hard			创建或更改硬链接。
- link			创建或更改软链接。
- touch			如果路径不存在,将创建一个空文件,而现有文件或目录将接收更新的文件访问和修改时间(类似于 touch 命令行工作的方式)
# 创建一个目录
ansible PHP -m file -a 'state=directory path=/test'

# 创建一个文件
ansible PHP -m file -a 'state=touch path=/test/file'

# 递归修改 owner,group,mode
ansible PHP -m file -a 'path=/test recurse=yes owner=bin group=daemon mode=1777'

# 删除目录(连同目录里的所有文件)
ansible PHP -m file -a 'state=absent path=/test'

# 创建文件并指定owner,group,mode等
ansible PHP -m file -a 'state=touch path=/tmp/file owner=bin group=daemon mode=1777'

# 删除文件
ansible PHP -m file -a 'state=absent path=/tmp/file'

# 创建软链接文件
ansible PHP -m file -a 'state=link src=/etc/fstab path=/tmp/fstab'

# 创建硬链接文件
ansible PHP -m file -a 'state=hard src=/etc/fstab path=/tmp/fstab2'

copy

copy 模块用于对文件的远程拷贝操作(如把本地的文件拷贝到远程的机器上),文档:https://docs.ansible.com/ansible/2.9/modules/copy_module.html

# 将本地 hosts 文件拷贝到所有远程机器,默认覆盖
ansible all -m copy -a 'src=/etc/hosts dest=/etc/hosts'

# 使用force参数控制是否强制覆盖
# 如果目标文件已经存在,则不覆盖
ansible all -m copy -a 'src=/etc/hosts dest=/etc/hosts force=no'
# 如果目标文件已经存在,则会强制覆盖,默认覆盖
ansible all -m copy -a 'src=/etc/hosts dest=/etc/hosts force=yes'

# 使用 backup 参数控制是否备份文件
# backup=yes 表示如果拷贝的文件内容与原内容不一样,则会备份一份
ansible all -m copy -a 'src=/etc/hosts dest=/etc/hosts backup=yes'

# 使用 content 参数直接往远程文件里写内容(会覆盖原内容),如果目标文件不存在则创建
ansible all -m copy -a 'content="192.168.1.10\n" dest=/root/file'

copy 模块拷贝时要注意拷贝目录后面是否带 “/” 符号

# /etc/yum.repos.d 后面不带 / 符号,则表示把 /etc/yum.repos.d 整个目录拷贝到 /tmp/ 目录下
ansible all -m copy -a 'src=/etc/yum.repos.d dest=/tmp/'
# /etc/yum.repos.d/ 后面带 / 符号,则表示把 /etc/yum.repos.d/ 目录里的所有文件拷贝到 /tmp/ 目录下
ansible all -m copy -a 'src=/etc/yum.repos.d/ dest=/tmp/'

在管理端上配置好所有的 yum 源,然后拷贝到所有远程机器上

ansible all -m file -a "path=/etc/yum.repos.d state=absent"
ansible all -m copy -a "src=/etc/yum.repos.d dest=/etc/"

fetch

fetch 模块与 copy 模块类似,但作用相反。用于把远程机器的文件拷贝到本地。文档:https://docs.ansible.com/ansible/2.9/modules/fetch_module.html

ansible all -m fetch -a 'src=/etc/hosts dest=/tmp/'

user

user 模块用于管理用户账号和用户属性。文档:https://docs.ansible.com/ansible/2.9/modules/user_module.html

# 创建 aaa 用户,默认为普通用户,创建家目录
ansible PHP -m user -a 'name=aaa state=present'

# 创建 bbb 系统用户,并且登录shell环境为/sbin/nologin
ansible PHP -m user -a 'name=aaa state=present'

# 创建 bbb 系统用户,并且登录 shell 环境为 /sbin/nologin
ansible PHP -m user -a 'name=bbb state=present system=yes  shell="/sbin/nologin"'

# 创建 ccc 用户, 使用 uid 参数指定 uid, 使用 password 参数传密码
ansible PHP -m user -a 'name=ccc uid=2000 state=present password="$1$DpcyhW2G$Kb/y1f.lyLI4MpRlHU9oq0"'

# 创建一个普通用户叫 hadoop,并产生空密码密钥对
ansible PHP -m user -a 'name=hadoop generate_ssh_key=yes'

# 删除 aaa 用户,但家目录默认没有删除
ansible PHP -m user -a 'name=aaa state=absent'

# 删除 bbb 用户,使用 remove=yes 参数让其删除用户的同时也删除家目录
ansible PHP -m user -a 'name=bbb state=absent remove=yes'

group

group模块用于管理用户组和用户组属性。文档:https://docs.ansible.com/ansible/2.9/modules/group_module.html

# 创建组
ansible group1 -m group -a 'name=groupa gid=3000 state=present'

# 删除组(如果有用户的 gid 为此组,则删除不了)
ansible group1 -m group -a 'name=groupa state=absent'

cron

cron 模块用于管理周期性时间任务。文档:https://docs.ansible.com/ansible/2.9/modules/cron_module.html

创建一个 cron 任务,不指定 user 的话,默认就是 root(因为我这里是用 root 操作的)。
如果 minute,hour,day,month,week 不指定的话,默认都为 *

# 创建 cron 任务
ansible group1 -m cron -a 'name="test cron1" user=root job="touch /tmp/111" minute=*/2' 

# 删除 cron 任务
ansible group1 -m cron -a 'name="test cron1" state=absent'

yum_repository

yum_repository 模块用于配置 yum 仓库。文档:https://docs.ansible.com/ansible/2.9/modules/yum_repository_module.html

# 增加一个 /etc/yum.repos.d/local.repo 配置文件
ansible group1 -m yum_repository -a "name=local description=localyum baseurl=file:///mnt/ enabled=yes gpgcheck=no"
# 注意:此模块只帮助配置yum仓库,但如果仓库里没有软件包,安装一样会失败。所以可以手动去挂载光驱到/mnt目录
# mount /dev/cdrom /mnt

# 删除 /etc/yum.repos.d/local.repo 配置文件
ansible group1 -m yum_repository -a "name=local state=absent" 

yum

yum 模块用于使用 yum 命令来实现软件包的安装与卸载。文档:https://docs.ansible.com/ansible/2.9/modules/yum_module.html

# 使用 yum 安装 vsftpd 软件
ansible group1 -m yum -a 'name=vsftpd state=present'

# 使用 yum 安装 httpd,httpd-devel 软件,state=latest 表示安装最新版本
ansible group1 -m yum -a 'name=httpd,httpd-devel state=latest' 

# 使用 yum 卸载 httpd,httpd-devel 软件
ansible group1 -m yum -a 'name=httpd,httpd-devel state=absent' 

service

service 模块用于控制服务的启动,关闭,开机自启动等。文档:https://docs.ansible.com/ansible/2.9/modules/service_module.html

# 启动 vsftpd 服务,并设为开机自动启动
ansible group1 -m service -a 'name=vsftpd state=started enabled=on'

# 关闭 vsftpd 服务,并设为开机不自动启动
ansible group1 -m service -a 'name=vsftpd state=stopped enabled=false'

script

script 模块用于在远程机器上执行本地脚本。文档:https://docs.ansible.com/ansible/2.9/modules/script_module.html

在管理端上准备一个脚本

#!/bin/bash
yum install mariadb-server -y  &> /dev/null
systemctl start mariadb
systemctl enable mariadb
mysql << EOF
create database abc;
quit
EOF
# 在 group1 的远程机器里都执行 master 上的 /tmp/1.sh 脚本(此脚本不用给执行权限)
ansible group1 -m script -a '/tmp/1.sh'

command 与 shell

两个模块都是用于执行 linux 命令的,shell 模块与 command 模块差不多(command 模块不能执行一些类似 $HOME > < | 等符号,但 shell 可以)

https://docs.ansible.com/ansible/2.9/modules/command_module.html
https://docs.ansible.com/ansible/2.9/modules/shell_module.html

ansible -m command group1 -a "useradd user2"
ansible -m command group1 -a "id user2"
ansible -m shell group1 -a "cat /etc/passwd |wc -l"
ansible -m shell  group1 -a "cd $HOME;pwd"

firewalld

此模块允许在正在运行的或永久的防火墙规则中添加或删除服务和端口(TCP或UDP)。

https://docs.ansible.com/ansible/2.9/modules/firewalld_module.html

ansible node1 -m firewalld -a "service=https permanent=yes state=enabled"

重启

ansible node1 -m service -a 'name=firewalld state=restarted'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值