【ansible】基础常用模块

博客介绍了在/etc/ansible/hosts中添加组的操作,还列举了多个Ansible模块的使用,包括shell、file、copy等模块,涉及文件操作、包管理、服务管理、用户管理等方面。

修改/etc/ansible/hosts中添加组

[A]
192.168.122.60
192.168.122.68

查看模块用法

ansible-doc -s 【模块名】
ansible-doc -s copy
ansible-doc -s fetch

shell模块

file模块

在file模块中,path、name、dest三个参数等价
owner修改属主 group修改属组 mode修改权限
创建文件
ansible A -m file -a "path=/root/helo state=touch"
删除文件
ansible A -m file -a "path=/root/helo state=absent"
创建目录
ansible A -m file -a "path=/root/dir-helo state=directory"
删除目录
ansible A -m file -a "path=/root/dir-helo state=absent"
创建硬连接
ansible A -m file -a 'src=/etc/fstab dest=/root/hard-symbolic state=hard'
创建软连接
ansible A -m file -a 'src=/etc/fstab dest=/root/soft-symbolic state=link'

copy模块

ansible a -m copy -a "src=/tmp/test dest=/root mode=777 backup=yes"

fetch模块

ansible a -m fetch -a "src=/tmp/test.txt dest=/root"
                       远程的机子上文件    ansible命令安装机的目录

unarchive解包模块

archive打包模块

hostname模块

更改后重启后主机名也生效,/etc/hostname生效,但是/etc/hosts下的不更改仍未原来主机名
ansible A -m hostname -a "name=test"

cron计划任务模块

创建一个名为warning-wall的计划任务,内容为周1~5每分钟执行一次wall命令进行内容为helo:warning的广播警告,执行后目标机用crontab -e进行验证
ansible A -m cron -a 'minute=* weekday=1,2,3,4,5 job="/usr/sbin/wall helo:warning" name=warning-wall'
禁用计划任务,即将目标机的计划任务注释掉,job必须指定
ansible A -m cron -a 'disabled=true job="/usr/sbin/wall helo:warning" name=warning-wall'
启用计划任务
ansible A -m cron -a 'disabled=false job="/usr/sbin/wall helo:warning" name=warning-wall'

yum管理包模块

state(安装/卸载,present/absent)
disable_gpg_check=yes 忽略gpg检查
yum更新缓存问题,导致安装失败(主机一般用yum clean all解决),这里用参数update_cache=yes
安装vsftpd及httpd
####
ansible A -m yum -a 'name=vsftpd,httpd state=present update_cache=yes disable_gpg_check=yes'

service服务管理模块

将httpd服务启动并设为开机启动
ansible A -m service -a 'name=httpd state=started enabled=true'

user用户管理模块

创建用户为nginx,shell类型为nologin,系统账号,工作目录/var/nginx,附加组为root,bin
ansible A -m user -a 'name=nginx shell=/sbin/nologin system=yes home=/var/nginx groups=root,bin'
删除用户及其家目录
ansible A -m user -a 'name=nginx state=absent remove=yes'

group模块

ansible A -m group -a 'name=nginx system=yes gid=666'

 

### Ansible 常用模块概述 Ansible 提供了一系列内置模块用于自动化运维任务,每个模块都有特定的功能和使用方式。为了表示一个可执行的任务,在配置这些模块时需加上`-`号[^1]。 #### 文件操作类模块 ##### `file` 此模块可用于处理文件、目录以及链接等资源的状态。例如创建空文件: ```bash ansible web_servers -m file -a "path=/tmp/testfile1 state=touch" ``` 这条命令会在目标机器上的 `/tmp/` 路径下创建名为 `testfile1` 的空文件[^3]。 #### 执行远程命令模块 ##### `command` 该模块允许用户在远程节点上执行不带shell特性(如管道符、重定向)的简单命令。 ##### `shell` 与 command 类似但是支持更复杂的 Shell 特性,比如环境变量设置、输入输出重定向等功能。 可以通过如下命令查看系统中存在的所有可用模块列表并统计数量: ```bash ansible-doc -l | wc -l ``` 也可以通过grep查找特定名称的模块, 如查询包含"shell"字样的模块: ```bash ansible-doc -l | grep shell ``` #### 获取主机信息模块 ##### `setup` Setup 模块用来收集被控端系统的事实(facts),即有关操作系统的信息和其他属性数据。默认情况下每次连接都会自动调用 setup 收集facts,当面对大量主机时可能会降低效率,因此可以选择关闭这一行为以提高性能: ```yaml --- - hosts: all tasks: - name: Disable fact gathering to speed up playbooks with many targets. debug: msg: "Gathering of facts is disabled." gather_facts: no ``` #### 测试连通性的 ping 模块 Ping 是最基础也是最重要的测试工具之一,它能够验证网络可达性和基本通信能力。Ansible 中也有对应的ping模块来实现这一点。 #### 自定义逻辑控制模块 ##### `script` Script 可以让管理员上传本地脚本到远端服务器上去运行,并根据返回的结果决定下一步动作。下面的例子展示了如何仅当某个条件满足时才去安装软件包: ```bash [root@master-61 ~]# ansible nfs -m script -a "/root/install_nginx.sh" ``` 这段代码会尝试在匹配的目标机上调用位于根用户目录下的 install_nginx.sh 脚本来部署 Nginx Web服务程序[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值