ansible常用模块解释

安装ansible并配置

yum install ansible -y
配置文件/etc/ansible
ansible.cfg  hosts  roles  yml
​
[root@gzz~66 ansible]# egrep -v "(^#|^$)" ansible.cfg  | head -6
[defaults]
inventory      = /etc/ansible/hosts
remote_port    = 22
host_key_checking = False
log_path = /var/log/ansible.log
module_name = command
​
[root@gzz~66 ansible]# egrep -v "(^#|^$)" hosts
[test]
192.168.129.166 nginx_port=10087 name=gz~166
192.168.129.199 nginx_port=10088 name=gz~199
192.168.129.99  nginx_port=10089 name=gz~99
​
设置公钥
[root@gzz~66 ansible]# ssh-keygen
[root@gzz~66 ansible]# ssh-copy-id root@192.168.129.166
[root@gzz~66 ansible]# ssh-copy-id root@192.168.129.199
[root@gzz~66 ansible]# ssh-copy-id root@192.168.129.99
​
测试
[root@gzz~66 ansible]# ansible test -m ping
192.168.129.166 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.129.99 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.129.199 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

ansible命令语法

命令格式ansibletest-mcommand-a"ls"
格式说明命令集群或者主机名称指定模块模块名称模块动作具体命令
--version显示版本
-m指定模块
-v显示过程,最多四个v,v越多越详细
--list显示主机列表
-k输入ssh连接密码(建议配一个公钥)
-C --check检查语法,不执行命令
-u指定远程操作执行的用户
-b代替旧版的sudo切换,默认为root
-K提示输入sudo时的口令
常用
ansible all --list列出所有主机
ansible-doc -s 模块名查看模块用法
主机名匹配支持正则及逻辑运算符
all所有主机都匹配
*通配符
或关系
:&与关系
:!非关系
使用非和综合逻辑时,'主机(集群)要用单引号括起来'"其他情况使用双引号括起来"

ping模块

通常用来检测网络是否连通
ansible test -m ping

command模块

可以使用大多数命令,但是不支持特殊符号,管道符什么的
ansible test -m command -a "touch 2022.txt"
ansible test -m command -a "sed -i '42s/80/90/g' /tmp/httpd.conf"

shell模块

和command模块相似,但是shell模块支持特殊符号
ansible test -m shell -a "netstat -tanulp| grep 22"
ansible test -m shell -a "echo 'hello' > /tmp/httpd.conf"
ansible test -m shell -a "echo 'world' >> /tmp/httpd.conf"
ansible test -m shell -a "echo 'nameserver 114.114.114.114' >> /etc/resolv.conf"

script模块

在被控机上执行本地的脚本
ansible test -m script -a "/tmp/a.sh"    
执行本地的脚本
​
在99的/tmp下创建了一个a.sh
[root@gzz~66 tmp]# ansible test -m script -a "creates=/tmp/a.sh /tmp/a.sh"
192.168.129.99 | SKIPPED
判断被控机本地文件或者目录是否存在,如果存在就跳过,如果不存在,就执行

copy模块

从主控端复制文件到被控端
默认直接覆盖
ansible test -m copy -a "src=/tmp/66.txt dest=/tmp owner=nebula group=nebula mode=777"
可不加owner,group,mode
​
指定内容直接生成目标文件
ansible test -m copy -a "content='happy' dest=/tmp/8.txt"

fetch模块

与copy模块相反,将被控机的文件拉到主控机上
​
ansible test -m fetch -a "src=/tmp/8.txt dest=/tmp"
存放到dest:/tmp下的以ip命名的文件夹

file模块

在被控机上创建文件和设置文件属性
​
创造(空文件)或者删除文件
ansible test -m file -a "path=/tmp/18.txt state=touch/absent"
​
创建文件是指定文件属性
ansible test -m file -a "path=/tmp/2023.txt owner=nebula group=nebula mode=755 state=touch/absent"
"
​
创建目录
ansible test -m file -a "path=/tmp/gaozhu state=directory owner=nebula group=nebula mode=777"
删除目录
ansible test -m file -a "path=/tmp/gaozhu state=absent"
​
递归修改
ansible test -m file -a "path=/tmp/gaozhu/ state=directory recurse=true owner=nebula group=nebula mode=777"
state=directory recurse=true
​
创建软链接
ansible test -m file -a "src=/tmp/kkk dest=/tmp/kkk-link state=link"
src和dest均是被控机上的文件路径
删除软链接
ansible test -m file -a "src=/tmp/kkk dest=/tmp/kkk-link state=absent"
链接文件被删除了,原文件还在

yum模块

删除安装软件
​
安装
ansible test  -m yum -a "name=httpd state=present"
删除
ansible test  -m yum -a "name=httpd state=absent"
安装最新版
ansible test  -m yum -a "name=httpd state=latest"

cron模块

主要用来管理系统中的定时任务
name任务名称
cron_file替换客户端改用户的任务计划的文件
minute
hour hourly
day daily
month monthly
weekday weekly
job计划执行的命令
backup是否备份之间的任务计划
user新建定时任务计划的用户
state指定任务计划present,absent
reboot重启时
annuall每年
添加定时任务是名字不可以重复
ansible test -m cron -a "minute=15 name=touchfik job='touch /tmp/`date`.txt'"
​
删除定时任务
ansible test -m cron -a "name=touchfik state=absent"
删除名字为空的定时任务
ansible test -m cron -a "name=None state=absent"
​
注释定时任务
ansible test -m cron -a "minute=15  name=jkkk job='touch /tmp/`date`.txt'"
ansible test -m cron -a "minute=15  name=jkkk job='touch /tmp/`date`.txt' disabled=yes"
disabled=yes
​
取消注释
ansible test -m cron -a "minute=15  name=jkkk job='touch /tmp/`date`.txt' disabled=no"
disabled=no
​
通过ansible在自己任意一台主机上创建计划任务,任务名称为”crontab day test”,
任务每3天执行一次,于执行当天的1点1分开始执行,任务内容为输出 test 字符。
ansible test -m cron -a "minute=1 hour=1 day=*/3 name='crontab day test' job='echo test'"

service模块

enabled 开机启动yes|no
name服务名称
runlevel运行级别
state
started启动
stopped关闭
restarted重启
reloaded重新加载
实现远程管理服务
​
启动
ansible test -m service -a "name=httpd state=started"
关闭
ansible test -m service -a "name=httpd state=stopped"
​
实现开机自启动
ansible test -m service -a "name=httpd state=started enabled=yes"
ansible test -m shell -a "systemctl list-unit-files| grep httpd"
​
systemctl list-unit-files | grep httpd 通过此命令查看看机自启服务

systemd模块

也是远程管理服务模块用法和service模块相似
​
ansible test -m systemd -a "name=httpd state=started"
ansible test -m systemd -a "name=httpd state=stopped"
ansible test -m systemd -a "name=httpd state=restarted"
ansible test -m systemd -a "name=httpd state=reloaded"

user模块

关键参数
group属组
groups附加组
home设置家目录
remove删除用户并删除家目录
shell设置用户登录shell
system系统用户
uid用户的id
state创造或者删除
ansible test -m user -a "name=user1 shell=/sbin/nologin home=/opt/user1 uid=3000 group=root groups=root"
​
创造系统用户
ansible test -m user -a "name=user2 system=yes"
​
删除用户
ansible test -m user -a "name=user1 state=absent"
删除用户并删除其家目录
ansible test -m user -a "name=user3 state=absent remove=yes"

group模块

gid组id
name组名
system系统组
state创造或者删除
ansible test -m group -a "name=group1 gid=10000"
ansible test -m group -a "name=group2 system=yes"
ansible test -m group -a "name=group1 state=absent"

mount模块

path挂载到本地的目录
src对端目录
fstype文件系统类型 nfs ext4 ext3
state
present只写入/etc/fstab不挂载
mounted即写入文件,也挂载
absent卸载设备,并清理文件
unmounted只卸载不清理文件
建议挂载的时候使用mounted卸载的时候使用absent
使用mount模块搭建nfs服务
​
首先在主机上面创建共享目录并修改权限
[root@gzz~66 data]# mkdir -pv /data/share
[root@gzz~66 data]# chmod -R 755 /data/share/
​
然后再主机上安装nfs和rpcbind服务
[root@gzz~66 data]# yum install nfs-utils rpcbind -y
​
修改配置文件增加从机信息
[root@gzz~66 data]# vi /etc/exports
 1 /data/share/ 192.168.129.199(rw,no_root_squash,no_all_squash,sync)
 2 /data/share/ 192.168.129.166(rw,no_root_squash,no_all_squash,sync)
​
然后按顺序启动服务
[root@gzz~66 data]# systemctl start rpcbind
[root@gzz~66 data]# systemctl start nfs
​
查看端口是否生效
[root@gzz~66 data]# showmount -e localhost
Export list for localhost:
/data/share 192.168.129.166,192.168.129.199
​
​
在从机上建立共享文件夹
[root@gzz~66 data]# ansible test -m file -a "path=/tmp/share state=directory"
​
从机上安装服务
[root@gzz~66 data]# ansible test -m yum -a "name=rpcbind state=present"
[root@gzz~66 data]# ansible test -m yum -a "name=nfs-utils state=present"
​
从机上启动服务  只需要启动rpcbind
[root@gzz~66 data]# ansible test -m service -a "name=rpcbind state=started"
​
​
查看端口
[root@gzz~66 data]# ansible test -m shell -a "showmount -e 192.168.129.66"
​
挂载
[root@gzz~66 data]# ansible test -m mount -a "src=192.168.129.66:/data/share path=/tmp/share fstype=nfs state=mounted"
​

setup模块

ansible_all_ipv4_addresses仅显示ipv4的信息
ansible_devices仅显示磁盘设备信息
ansible_distribution显示是什么系统
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_memory_mb详细显示内存情况
ansible_swaptotal_mb显示总的swap内存
ansible_mounts显示系统磁盘挂载情况
ansible_processor显示cpu个数(具体显示每个cpu的型号)
ansible_processor_vcpus显示cpu个数(只显示总的个数)

用户获取远程主机的一些信息
[root@gzz~66 share]# ansible test -m setup
直接指定setup可以执行,但是返回信息太多,所以我们可以使用filter进行过滤,得到我们想要的信息

获取远程主机的ip信息
ansible test -m setup -a "filter=ansible_all_ipv4_addresses"
获取远程主机的内存信息
ansible test -m setup -a "filter=ansible_memory_mb"
获取远程主机的主机名
ansible test -m setup -a "filter=ansible_fqdn"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值