ansible模块应用

问答题

请总结今天所学的ansible模块,以及各个模块的作用!

ping

[if !supportLists]n [endif]ping模块用来检查目标主机是否在线

[if !supportLists]n [endif]例子:ansible webserver -m ping

yum

[if !supportLists]n [endif]yum模块用来在Centos系统上使用yum命令安装软件包

[if !supportLists]n [endif]选项:

[if !supportLists]u [endif]name: 指定安装包的名字

[if !supportLists]u [endif]state:latest 安装最新版 present 默认安装 installed 安装 absent 卸载

[if !supportLists]n [endif]例子:ansible webservers -m yum -a ‘name=httpd state=latest’

command

[if !supportLists]n [endif]command模块用来执行系统命令,但是不支持shell下的特殊符号 如:|  &&等

[if !supportLists]n [endif]例子:ansible webservers -m command  -a ‘echo xx

shell

[if !supportLists]n [endif]shell模块和command模块使用方法基本一致,但是他可以支持shell的特殊符号,如: |  && 等

[if !supportLists]n [endif]例子:ansible webservers -m shell  -a “cd /opt/ && touch lixiang”

service

[if !supportLists]n [endif]service模块用来管理centos上的服务的启动、关闭、重启和重载

[if !supportLists]n [endif]选项:

[if !supportLists]u [endif]name: 服务名字

[if !supportLists]u [endif]state:  started(启动)  stopped(停止) restarted(重启)  reloaded(重载)

[if !supportLists]u [endif]enabled: 默认是no,将服务设置为开机自启

file

[if !supportLists]n [endif]file模块用来创建文件、目录、链接文件

[if !supportLists]n [endif]选项:

[if !supportLists]u [endif]group:定义文件/目录的属组

[if !supportLists]u [endif]mode:定义文件/目录的权限

[if !supportLists]u [endif]owner:定义文件/目录的属主

[if !supportLists]u [endif]path:必选项,定义文件/目录的路径

[if !supportLists]u [endif]recurse:递归的设置文件的属性,只对目录有效

[if !supportLists]u [endif]src:要被链接的源文件的路径,只应用于state=link的情况

[if !supportLists]u [endif]dest:被链接到的路径,只应用于state=link的情况

[if !supportLists]u [endif]state:

[if !supportLists]l [endif]directory:如果目录不存在,创建目录

[if !supportLists]l [endif]file:即使文件不存在,也不会被创建

[if !supportLists]l [endif]link:创建软链接

[if !supportLists]l [endif]hard:创建硬链接

[if !supportLists]l [endif]touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间

[if !supportLists]l [endif]absent:删除目录、文件或者取消链接文件

user

[if !supportLists]n [endif]user模块用来创建用户

[if !supportLists]n [endif]选项:

[if !supportLists]u [endif]home:指定创建的家目录

[if !supportLists]u [endif]groups:指定用户组

[if !supportLists]u [endif]uid:指定UID

[if !supportLists]u [endif]password:设置密码,密码必须是密文

[if !supportLists]u [endif]name:创建的用户名字

[if !supportLists]u [endif]createhome:是否创建家目录(yes/no)

[if !supportLists]u [endif]state:  是创建还是删除。(present,absent),默认是创建

[if !supportLists]u [endif]shell:指定用户登录的shell环境

group

[if !supportLists]n [endif]group用来创建用户组

[if !supportLists]n [endif]选项

[if !supportLists]u [endif] gid:指定用的gid。 

[if !supportLists]u [endif]name:指定用户名。 

[if !supportLists]u [endif]state:是创建还是删除。(present,absent) 

copy

[if !supportLists]n [endif]copy模块用来复制文件至目标主机

[if !supportLists]n [endif]选项:

[if !supportLists]u [endif]src:文件在管理主机的据对路径或者相对路径

[if !supportLists]u [endif]dest:将文件复制到目标主机的路径

[if !supportLists]u [endif]bakcup:是否将目标主机的同名文件备份,默认为no

[if !supportLists]u [endif]directory_mode:递归授权

[if !supportLists]n [endif]例子:

ansible  webservers  -m copy  -a ‘src=/root/nginx.sh  dest=/opt/’

unarchive

[if !supportLists]n [endif]unarchive模块用来解压文件

[if !supportLists]n [endif]选项:

[if !supportLists]u [endif]copy:在解压文件之前,是否先将文件复制到远程主机,默认为yes。若为no,则要求目标主机上压缩包必须存在

[if !supportLists]u [endif]creates:指定一个文件名,当该文件存在时,则解压指令不执行

[if !supportLists]u [endif]dest:远程主机上的一个路径,即文件解压的绝对路径。

[if !supportLists]u [endif]group:解压后的目录或文件的属组

[if !supportLists]u [endif]mode:解压后文件的权限

[if !supportLists]u [endif]src:如果copy为yes,则需要指定压缩文件的源路径

[if !supportLists]u [endif]owner:解压后文件或目录的属主

[if !supportLists]n [endif]例子:

ansible  webservers -m unarchive -a ‘src=/root/nginx.tar.gz  dest=/opt/ group=www ower=www mode=777 ’

get_url

[if !supportLists]n [endif]get_url模块,该模块主要用于从http、ftp、https服务器上下载文件(类似于wget

[if !supportLists]n [endif]选项:

[if !supportLists]u [endif]url: 指定要下载的文件的URL地址

[if !supportLists]n [endif]例子:

ansible webservers -m get_url -a ‘url= http://nginx.org/download/nginx-1.15.7.tar.gz’

synchronize

[if !supportLists]n [endif]使用rsync同步文件,将主控方目录推送到指定节点的目录下,使用此模块需要先安装rsync

[if !supportLists]n [endif]delete: 删除不存在的文件,delete=yes 使两边的内容一样(即以推送方为主),默认no

[if !supportLists]n [endif]src: 要同步到目的地的源主机上的路径; 路径可以是绝对的或相对的。如果路径使用”/”来结尾,则只复制目录里的内容,如果没有使用”/”来结尾,则包含目录在内的整个内容全部复制

[if !supportLists]n [endif]dest:目的地主机上将与源同步的路径; 路径可以是绝对的或相对的。

[if !supportLists]n [endif]dest_port:默认目录主机上的端口 ,默认是22,走的ssh协议。

[if !supportLists]n [endif]mode: push或pull,默认push,一般用于从本机向远程主机上传文件,pull 模式用于从远程主机上取文件。

[if !supportLists]n [endif]rsync_opts:通过传递数组来指定其他rsync选项。

fetch

[if !supportLists]n [endif]fetch模块它用于从远程机器获取文件,并将其本地存储在由主机名组织的文件树中。

[if !supportLists]n [endif]选项:

[if !supportLists]u [endif]src:远程系统上要获取的文件。 这必须是一个文件,而不是一个目录。 后续版本可能会支持递归提取。

[if !supportLists]u [endif]dest:保存文件的目录

setup

[if !supportLists]n [endif]setup 模块用于收集远程主机的一些基本信息。

[if !supportLists]n [endif]选项:

[if !supportLists]u [endif]filter参数:用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息。

[if !supportLists]u [endif]常用的过滤关键词:

[if !supportLists]l [endif]ansible_all_ipv4_addresses:仅显示ipv4的信息

[if !supportLists]l [endif]ansible_devices:仅显示磁盘设备信息

[if !supportLists]l [endif]ansible_distribution:显示是什么系统,例:centos,suse等

[if !supportLists]l [endif]ansible_distribution_major_version:显示是系统主版本

[if !supportLists]l [endif]ansible_distribution_version:仅显示系统版本

[if !supportLists]l [endif]ansible_machine:显示系统类型,例:32位,还是64位

[if !supportLists]l [endif]ansible_eth0:仅显示eth0的信息

[if !supportLists]l [endif]ansible_hostname:仅显示主机名

[if !supportLists]l [endif]ansible_kernel:仅显示内核版本

[if !supportLists]l [endif]ansible_lvm:显示lvm相关信息

[if !supportLists]l [endif]ansible_memtotal_mb:显示系统总内存

[if !supportLists]l [endif]ansible_memfree_mb:显示可用系统内存

[if !supportLists]l [endif]ansible_memory_mb:详细显示内存情况

[if !supportLists]l [endif]ansible_swaptotal_mb:显示总的swap内存

[if !supportLists]l [endif]ansible_swapfree_mb:显示swap内存的可用内存

[if !supportLists]l [endif]ansible_mounts:显示系统磁盘挂载情况

[if !supportLists]l [endif]ansible_processor:显示cpu个数(具体显示每个cpu的型号)

[if !supportLists]l [endif]ansible_processor_vcpus:显示cpu个数(只显示总的个数)

[if !supportLists]l [endif]ansible_python_version:显示python版本

[if !supportLists]u [endif]例子:

获取目标主机的ipv4地址

ansible webservers -m setup -a 'filter=ansible_all_ipv4_addresses'

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,997评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,603评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,359评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,309评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,346评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,258评论 1 300
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,122评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,970评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,403评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,596评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,769评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,464评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,075评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,705评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,848评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,831评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,678评论 2 354