第4天:Ansible模块

Ansible对远程服务器的实际操作实际是通过模块完成的,其工作原理如下:

  1)将模块拷贝到远程服务器

  2)执行模块定义的操嘴,完成对服务器的修改

  3)在远程服务器中删除模块

需要说明的是,Ansible中的模块是幂等的。也就是说,多次执行相同的操作,只有第一次会起作用。这也是在编写自定义Ansible模块的需要注意的地方。

Ansible提供了大量的模块,ansible-doc命令用于在命令行查看模块列表,也可以使用该工具在命令行获取模块帮助信息

ansible-doc -l

下面命令用户获取file模块的帮助信息

ansible-doc file
ansible-doc -l file

下面是一些比较基础,重要,同时也是使用频率比较高的模块

ping

ping模块是Ansible中最简单的模块,用来测试现有的SSH参数是否能够顺利连通远程服务器

ansible test -m ping

远程命令模块

command 是Ansible的默认模块,可以不指定模块名称直接运行Linux命令, 也可以显示地通过-m指定command模块

command在执行Linux命令时不能使用管道

ansible test -a ‘hostname‘
ansible test -m command -a ‘hostname‘ 

raw模块相当于使用SSH直接执行Linux命令,不会进入到Ansible的模块子系统中

ansible test -m raw -a ‘cat /etc/passwd|wc -l‘

shell

file

..

copy

...

user/group

...

apt

...

get_url

...

unarchive

...

git

...

stat

...

cron

...

service

...

sysctl

...

mount

...

synchronize

...

原文地址:https://www.cnblogs.com/sellsa/p/9952121.html

时间: 2024-08-01 04:27:15

第4天:Ansible模块的相关文章

ansible模块yum、services、setup

ansible模块 yum: [[email protected] ~]# ansible-doc -s yum less 436 Copyright (C) 1984-2009 Mark Nudelman less comes with NO WARRANTY, to the extent permitted by law. For information about the terms of redistribution, see the file named README in the l

ansible模块command、shell、raw、script

环境: ansible端: ip:192.168.100.129 hostname:node1.lansgg.com client端: ip:192.168.100.131 hostname:v2.lansgg.com ip:192.168.100.132 hostname:v3.lansgg.com [[email protected] ansible]# pwd /etc/ansible [[email protected] ansible]# cat hosts [testservers]

ansible模块批量管理

1.[copy]模块 ansible oldboy-m copy -a "src=/etc/hosts dest=/opt/" 加上backup=yes对客户端的文件进行备份 ansible oldboy -m copy -a "src=/etc/hostsdest=/opt/ backup=yes" 2.Shell模块 用之前需要将脚本先推送到客户端才能执行 1)推送 ansible oldboy-m copy -a "src=/server/scrip

ansible 模块

一.什么是ansible 什么是ansible?官方的title是"Ansibleis Simple IT Automation"->简单的自动化IT工具.ansible功能:自动化部署APP:自动化管理配置项:自动化的持续交付:自动化的(AWS)云服务管理.其本质就是在一个台或者几台服务器上,批量的执行命令. fabric和ansible有什么差别呢?简单来说fabric像是一个工具箱,提供了很多好用的工具,用来在Remote执行命令,而ansible则是提供了一套简单的流程,

ansible模块cron、copy、user、group

查询模块的参数: ansible-doc -s moduleName 如 cron 模块 [[email protected] ansible]# ansible-doc -s cron less 436 Copyright (C) 1984-2009 Mark Nudelman less comes with NO WARRANTY, to the extent permitted by law. For information about the terms of redistributio

ansible 模块学习

Ansible通过模块的方式来完成一些远程的管理工作.可以通过ansible-doc -l查看所有模块,可以使用ansible-doc -s module来查看某个模块的参数,也可以使用ansible-doc help module来查看该模块更详细的信息.默认的模块位置在/usr/share/ansible.下面列出一些常用的模块: 1. setup 可以用来查看远程主机的一些基本信息: ansible -i /etc/ansible/hosts test -m setup 2.ping 可以

ansible模块

1./etc/ansible/hosts 文件配置如下: [group01] //ubuntu16 jumper ansible_ssh_port=6666 ansible_ssh_host=172.16.1.206 172.16.1.207 [group01] //centos7 172.16.1.201 2.远程重启服务 sudo ansible group01 –m service –a "name=ssh state=restarted" 3.查看远程主机名 sudo ansi

ansible模块-lineinfile

lineinfile模块详解 lineinfile模块类似linux工具中的sed工具,但是网上的文章一般都只有简单的实例,复杂点的例子都没有 下面是我根据实际操作总结出来的lineinfile模块的常见例子,分享给大家参考 目录 ansible-doc lineinfile官方文档(英文)--看不懂跳过直接看例子 1.需求:替换匹配的目标值... 2.需求:在匹配值之前增加行... 3.需求:在匹配值之后增加行... 4.需求:匹配到就替换行,未匹配到就新增行... ansible-doc l

ansible模块介绍

命令模块: 1:command模块在远程节点上执行命令: command模块后面紧跟要执行的命令,命令的参数以空格隔开.指定的命令会在所选的节点上执行.命令并不是通过shell执行的,所以并不能使用$HOME等环境变量和一些操作符(<,>,|,&).shell模块可以使用. 1>chdir 在运行命令之前,先切换到指定的目录. [[email protected] ansible]# ansible testhosts -m command -a "ls -l chdi