ansible常见模块
setup
filter : 用于进行条件过滤,如果设置,仅返回匹配过滤条件的信息
ansible hosts1 - m setup
ansible hosts1 - m setup - a "filter=ansible_all_ipv4_addresses"
command
例:在inventory文件中的所有主机中执行date命令,- m默认不指定时就是使用command模块
ansible all - m command - a "date"
cron
state值: present( 添加) absent( 移除)
例: 使用ansible 添加任务计划 * / 10 * * * * / bin/echo hello
添加:
ansible all - m cron - a 'minute="*/10" job="/bin/echo hello" name="test job" state=present'
检查:
ansible all - a 'crontab -l'
移除:
ansible all - m cron - a 'minute="*/10" job="/bin/echo hello" name="test job" state=absent'
user
name: 指定用户名
uid: 指定uid号
group : 指定组
添加用户
ansible all - m user - a 'name="user1" state=present'
删除用户
ansible all - m user - a 'name="user1" state=absent'
group
添加用户组
ansible all - m group - a 'name="mysql" gid=306 system=yes'
copy
src= :定义本地源文件
dest=:定义目标路劲(绝对路劲)
content= :取代src=, 表示用指定的内容生成为目标文件的内容,不能与src同时使用
owner: 文件属主
group : 文件属组
mode: 文件权限
backup: 是否备份
拷贝本地的/ etc/fstab 到远程的 / tmp 下权限为640 属主为root
ansible all - m copy - a 'src=/etc/fstab /dest=/tmp/fstab.ansible owner=root mode=640'
拷贝内容为“hello longge”到远程主机
ansible all - m copy - a 'content="hello longge" dest=/tmp/test.ansible'
file
path: 必须参数,指定要操作的文件或目录
state: directory目录 touch文件 link软链接 hard硬链接 absent删除
src: 创建链接时需要指定的源
force: 强制
owner: 文件属主
group : 文件属组
mode: 文件权限
recurse: 目录时递归修改文件属性
ping
ansible all - m ping
service:
state: started stopped reloaded restarted
enabled: 是否开机自启
ansible all - m service - a 'enabled=true name=httpd state=started'
shell
与command类似,但是可以执行带管道和变量的命令,同样无法做到运行多次结果一致
script
chdir 参数 : 此参数的作用就是指定一个远程主机中的目录,在执行对应的脚本之前,会先进入到 chdir 参数指定的目录中。
creates参数 :使用此参数指定一个远程主机中的文件,当指定的文件存在时,就不执行对应脚本,可参考 command 模块中的解释。
removes参数 :使用此参数指定一个远程主机中的文件,当指定的文件不存在时,就不执行对应脚本
ansible all - m script - a 'chdir=/opt /tmp/test.sh'
yum
state值: present( 安装) absent( 移除)
安装软件包
ansible all - m yum - a 'name=nginx'
卸载
ansible all - m yum - a 'name=nginx state=absent'
lineinfile
path: 文件路径
regexp: 正则匹配,如'^127\.0\.0\.1' ,'^Listen ' ,'^# port for http'
line: 需要更换成的行或者添加的行
insertbefore: 正则匹配该行并在改行前面添加一行,如'^www.*80/tcp' ,'^#Listen ' ,'aa(.*)'
insertafter: 正则匹配该行并在改行后面添加一行
state: absent删除 present添加
例:
bbb修改为bbbbbb
ansible all - m lineinfile - a "dest=/root/test.txt regexp='bbb' line='bbbbbbb'"
2. 2 在某一行前面插入一行
ansible all - m lineinfile - a "dest=/root/test.txt insertbefore='aa(.*)' line='eeee'"
2. 3 在某一行后面插入一行
ansible all - m lineinfile - a "dest=/root/test.txt insertafter='aa(.*)' line='eeee'"
2. 4 删除某一行
ansible all - m lineinfile - a "dest=/root/test.txt regexp='aa(.*)' state=absent"
2. 5 末尾加入一行
ansible all - m lineinfile - a "dest=/root/test.txt line='hehe'"
2. 6 替换或添加某一行
ansible all - m lineinfile - a "dest=/root/test.txt regexp='he(.*)' line='lllll' state=present"
replace
path: / etc/hosts
after: '开始位置'
before: '结束位置'
regexp: '匹配内容'
replace: '替换内容'
ansible host01 - m replace - a "path=/etc/fstab regexp='^10.1.2.254(.*)' replace='# 10.1.2.254\1' backup=yes"
ansible 192. 168. 1. 1 - m replace - a "path=/etc/ssh/sshd_config regexp='^allowusers(.*)' replace='allowusers\1 aaa' backup=yes"
ansible host02 - m replace - a "dest=/etc/ssh/sshd_config regexp='test01' replace=''"
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)