Ansible中常用模块
最佳答案 问答题库698位专家为你答疑解惑
目录
一、Ansible实现管理的方式
二、Ad-Hoc执行方式中如何获得帮助
三、Ansible命令运行方式及常用参数
四、Ansible的基本颜色代表信
五、Ansible中的常用模块
1、command模块
2、shell模块、script模块
3、copy模块、fetch模块
4、file模块
5、archive模块、unarchive模块
6、hostname模块、cron模块
一、Ansible实现管理的方式
Ad-Hoc 利用ansible命令直接完成管理,主要用于临时命令使用场景
playbook ansible脚本,主要用于大型项目场景,需要前期的规划
二、Ad-Hoc执行方式中如何获得帮助
ansible-doc 显示模块帮助的指令
格式
ansible-doc [参数] [模块...]常用参数
-l 列出可用模块
-s 显示指定模块的playbook片段
vim test.yml
cat test.yml
ansible-playbook test.yml
ansible-doc
ansible-doc -l | wc -l
ansible-doc -l | grrep shell
三、Ansible命令运行方式及常用参数
-i
指定hosts文件路径,默认在/etc/ansible/hosts
-m
指定使用的module名称,默认command模块-a
指定模块参数-k(小写)
提示输入ssh密码,并非基于ssh密钥认证-K(大写)
提示输入sudo密码-b
使用sudo执行命令-become-user=
指定sudo的用户-f,-forks=NUM
NUM默认是整数5,指定fork开启同步进程的个数-u
指定远程主机的执行用户-v
详细模式,如果执行成功,输出详细结果,-vv -vvv
更详细过程-C
预执行检测-T
执行命令的超时时间,默认10s--list
显示主机列表,也可以用--list-hosts
--version
显示版本
ansible --version
ansible all -m shell -a 'whoami' -C
ansible all -m shell -a 'whoami' -u test
ansible all -m shell -a 'whoami' -utest
ansible all -m shell -a 'whoami'
ansible all -m shell -a 'whoami' -b -become-user=yyl
ansible all -m shell -a 'whoami' -b -become-user=yyl -K
四、Ansible的基本颜色代表信
五、Ansible中的常用模块
1、command模块
在远程主机执行命令,此模块为默认模块
chdir
执行命令前先进入到指定目录cmd
运行命令指定creates
如果文件存在将不运行removes
如果文件存在将运行
ansible all -m command -a 'chdir=/mnt pwd'
ansible all -m command -a 'chdir=/mnt cmd=pwd'
ansible all -m command -a 'chdir=/mnt creates=/etc/passwd cat passwd'
ansible all -m command -a 'chdir=/mnt removes=/etc/passwd cat passwd'
ansible all -m command -a 'chdir=/mnt removes=/mnt/file cat passwd'
ansible all -m command -a 'chdir=/mnt touch file'
ansible all -m command -a 'chdir=/mnt creates=/mnt/file pwd'
ansible all -m command -a 'chdir=/mnt removes=/mnt/file pwd'
注意
Linux中的很多通配符在command模块中不支持
ansible all -m command -a "chdir=/mnt touch file{1..10}"
2、shell模块、script模块
(1)shell模块
shell模块与command模块类似
chdir
执行命令前先进入到指定目录cmd
运行命令指定creates
如果文件存在将不运行removes
如果文件存在将运行executable
指定执行环境,默认为sh
ansible all -m shell -a 'chdir=/mnt/ touch file{1..3}'
ansible all -m shell -a 'chdir=/mnt/ ls -ld /mnt'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'
ansible all -m shell -a 'ps ax | grep $$'
ansible all -m shell -a 'executable=/bin/bash ps ax | grep $$'
(2)script模块
在ansible主机中写好的脚本在受控主机中执行
vim test.sh
cat test.sh
ansible all -m script -a "test.sh"
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'
3、copy模块、fetch模块
(1)copy模块
从ansible主机复制文件到受控主机
src
源文件dest
目的地文件owner
指定目的地文件所有人group
指定目的地文件所有组mode
指定目的地文件权限backup=yes
当受控主机中存在文件时备份原文件content
指定文本内容直接在受控主机中生成文件
复制当前目录的test.sh
到受控主机的/mnt
下,文件所有人为yyl
,权限为755
ansible all -m copy -a 'src=test.sh dest=/mnt/ owner=yyl mode=755'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'
在ansible主机上修改文件内容,开启备份,再次发送 先修改一下文件
ansible all -m copy -a 'src=test.sh dest=/mnt/ owner=yyl mode=755 backup=yes'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'
直接输入文件内容,在受控主机上生成文件
ansible all -m copy -a "content='hello westos\nhello linux\n' dest=/mnt/test.sh owner=yyl mode=755"
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
(2)fetch模块
从受控主机把文件复制到ansible主机,但不支持目录
src
受控主机的源文件dest
本机目录flat
基本名称功能,单纯只要文件,不要路径的层层目录
ansible all -m fetch -a "src=/mnt/test.sh dest=/home/ale/.ansible"
ansible all -m fetch -a "src=/mnt/test.sh dest=/home/ale/.ansible/ flat=yes"
4、file模块
设置文件的属性
path
指定文件名称state
指定操作状态touch
:建立absent
:删除directory
:递归目录link
:建立软链接hard
:建立硬链接mode
设定权限owner
设定文件用户group
设定文件组src
源文件dest
目标文件recurse=yes
递归更改
ansible all -m shell -a 'rm -fr /mnt/*'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
创建test.sh文件
ansible all -m file -a 'path=/mnt/test.sh state=touch'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
删除
ansible all -m file -a 'path=/mnt/test.sh state=absent'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
创建目录
ansible all -m file -a 'path=/mnt/westos state=directory'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
ansible all -m file -a 'path=/mnt/westos/file1 state=touch'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
递归更改目录权限
ansible all -m file -a 'path=/mnt/westos state=directory mode=777 recurse=yes'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
软连接
ansible all -m file -a 'src=/mnt/file dest=/mnt/yyl state=link'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
硬链接
ansible all -m file -a 'src=/mnt/file dest=/mnt/yyl1 state=hard'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
5、archive模块、unarchive模块
(1)archive模块——压缩
path
打包目录名称dest
声称打包文件名称format
打包格式owner
指定文件所属人mode
指定文件权限
ansible all -m archive -a "path=/mnt dest=/mnt/mnt.tar.gz format=gz owner=yyl mode=700"
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
(2)unarchive模块——解压缩
copy
默认为yes
,从ansible主机复制文件到受控主机设定为
no
,从受控主机中寻找src
源文件remote_src
功能同copy且相反设定为
yes
表示包在受控主机设定为
no
表示包在ansible主机src
压缩文件的路径dest
受控主机目录mode
解压后文件的权限<copy=yes>
tar zcf ansible.tar.gz .
ls
ansible all -m unarchive -a 'src=./ansible.tar.gz dest=/mnt copy=yes'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
提前在vm1,2上压缩一个不被限制的gz包
ansible all -m unarchive -a 'src=/mnt/file.tar.gz dest=/mnt copy=no'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
6、hostname模块、cron模块
(1)hostname模块
管理主机名称
常用参数:name
:指定主机名称
ansible 192.168.67.112 -m hostname -a 'name=vm22'
ansible 192.168.67.112 -m shell -a 'hostname'
ansible 192.168.67.112 -m hostname -a 'name=vm1'
ansible 192.168.67.112 -m shell -a 'hostname'
99%的人还看了
相似问题
- go语言实现高性能自定义ip管理模块(ip黑名单)
- 【Django使用】4大模块50页md文档,第4篇:Django请求与响应和cookie与session
- 【python学习】基础篇-常用模块-pickle模块:序列化和反序列化
- #gStore-weekly | gAnswer源码解析 调用NE模块流程
- 【前端知识】Node——events模块的相关方法
- 使用Python的turtle模块绘制玫瑰花图案(含详细Python代码与注释)
- 【ES6标准入门】JavaScript中的模块Module语法的使用细节:export命令和imprt命令详细使用,超级详细!!!
- SpringBoot学习笔记-配置MySQL与实现注册登录模块(中)
- 一个奇怪的蓝牙模块分析记录
- 前端JS模块化对外暴露的三种方法
猜你感兴趣
版权申明
本文"Ansible中常用模块":http://eshow365.cn/6-28203-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!
- 上一篇: C# 图解教程 第5版 —— 第11章 结构
- 下一篇: GitLab定时备份