ansible常用命令大全
ansible 默认提供了很多模块来供我们使用。在 Linux 中,我们可以通过 ansible-doc -l 命令查看到当前 ansible 都支持哪些模块,通过 ansible-doc -s 模块名 又可以查看该模块有哪些参数可以使用。
我们常用的几个模块:
copy file cron group user yum service script ping command raw get_url synchronize
ansible '*' -m command -a 'uptime'
'*':自己定义的主机 -m command:命令
# 指定节点上的权限,属主和数组为root
ansible '*' -m file -a "dest=/tmp/t.sh mode=755 owner=root group=root"
#指定节点上定义一个计划任务,每隔3分钟到主控端更新一次时间
ansible '*' -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 172.16.254.139"'
# 指定节点上创建一个组名为aaa,gid为2017的组
ansible all -m group -a 'gid=2017 name=a'
# 在节点上创建一个用户aaa,组为aaa
ansible all -m user -a 'name=aaa groups=aaa state=present'
删除用户示例
ansible all -m user -a 'name=aaa groups=aaa remove=yes'
# 在节点上安装httpd
ansible all -m yum -a "state=present name=httpd"
# 在节点上启动服务,并开机自启动
ansible all -m service -a 'name=httpd state=started enabled=yes'
# 检查主机连接
ansible '*' -m ping
# 执行远程命令
ansible '*' -m command -a 'uptime'
# 执行主控端脚本
ansible '*' -m script -a '/root/test.sh'
# 执行远程主机的脚本
ansible '*' -m shell -a 'ps aux|grep zabbix'
# 类似shell
ansible '*' -m raw -a "ps aux|grep zabbix|awk '{print \$2}'"
# 创建软链接
ansible '*' -m file -a "src=/etc/resolv.conf dest=/tmp/resolv.conf state=link"
# 删除软链接
ansible '*' -m file -a "path=/tmp/resolv.conf state=absent"
# 复制文件到远程服务器
ansible '*' -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644"
# 在节点上运行hostname
nsible all -m raw -a 'hostname|tee'
# 将指定url上的文件下载到/tmp下
ansible all -m get_url -a 'url=http://10.1.1.116/favicon.ico dest=/tmp'
ynchronize模块:
目的:将主控方/root/a目录推送到指定节点的/tmp目录下
命令:ansible all -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes'
执行效果:
delete=yes 使两边的内容一样(即以推送方为主)
compress=yes 开启压缩,默认为开启
--exclude=.git 忽略同步.git结尾的文件

由于模块,默认都是推送push。因此,如果你在使用拉取pull功能的时候,可以参考如下来实现
mode=pull 更改推送模式为拉取模式
目的:将10.1.1.113节点的/tmp/a目录拉取到主控节点的/root目录下
命令:ansible 10.1.1.113 -m synchronize -a 'mode=pull src=/tmp/a dest=/root/'
执行效果:

由于模块默认启用了archive参数,该参数默认开启了recursive, links, perms, times, owner,group和-D参数。如果你将该参数设置为no,那么你将停止很多参数,比如会导致如下目的递归失败,导致无法拉取

其它相关的参数解释:
|
1
2
3
|
dest_port=22 # 指定目的主机的ssh端口,ansible配置文件中的 ansible_ssh_port 变量优先级高于该 dest_port 变量rsync_path # 指定 rsync 命令来在远程服务器上运行。这个参考rsync命令的--rsync-path参数,--rsync-path=PATH # 指定远程服务器上的rsync命令所在路径信息rsync_timeout # 指定 rsync 操作的 IP 超时时间,和rsync命令的 --timeout 参数效果一样 |
ansible常用命令大全的更多相关文章
- db2常用命令大全
#显示这个DB2错误的解释信息(SQLSTATE 5位数字)db2 ? 42704 #显示这个SQLCODE的解释信息(SQLCODE 四位数字) db2 ? SQL0204N ##查看数据库指定配置 ...
- linux 常用命令大全
linux 常用命令大全 系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统 ...
- Ubuntu常用命令大全(转)
点评:Ubuntu常用命令大全,学习ubuntn系统的朋友可以收藏下,用ctrl+F查找即可 一.文件/文件夹管理 ls 列出当前目录文件(不包括隐含文件) ls -a 列出当前目录文件(包括隐含文件 ...
- 100个命令Linux常用命令大全
Linux常用命令大全100条: 1,echo "aa" > test.txt 和 echo "bb" >> test.txt//>将原 ...
- Linux常用命令大全(非常全!!!)
Linux常用命令大全(非常全!!!) 最近都在和Linux打交道,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制,当然,这也是很多人喜欢linux的原因,比较短 ...
- ansible常用命令
一.ansible常用命令 一.ansible命令的常用参数 ansible 默认提供了很多模块来供我们使用.在 Linux 中,我们可以通过 ansible-doc -l 命令查看到当前 ansib ...
- Linux常用命令大全(转)
(转)Linux常用命令大全(非常全!!!) 最近都在和Linux打交道,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制,当然,这也是很多人喜欢linux的原因, ...
- 18-10-09 Linux常用命令大全(非常全!!!)
Linux常用命令大全(非常全!!!) Linux常用命令大全(非常全!!!) 最近都在和Linux打交道,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制, ...
- Ubuntu常用命令大全[显示桌面]
Ubuntu常用命令大全 查看软件xxx安装内容 #dpkg -L xxx 查找软件 #apt-cache search 正则表达式 查找文件属于哪个包 #dpkg -S filename apt-f ...
随机推荐
- Lock 从来就没有成功过
package lime.thinkingInJava._021._005._003; import java.util.concurrent.ExecutorService; import java ...
- 从0移植uboot(六) _实现网络功能
为uboot添加网卡功能可以让uboot通过tftp下载内核, 方便我们的开发, 对于网卡功能的移植,我们依然在在一遍又一遍的实践这个uboot改造的套路. 找运行逻辑,即插入代码的位置. 根据运行逻 ...
- LeetCode - 703. Kth Largest Element in a Stream
Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...
- Microsoft office 2019 正式版镜像下载
http://www.xitongtiandi.net/soft_yy/4373.htmlMicrosoft office 2019 正式版镜像下载 http://www.xitongtiandi.n ...
- 31、cookie小test
请尽量使用JQuery进行代码编写,需求如下: 1. 页面初始化样式如图 2. 顶部input框中输入内容,按下回车enter键后,“正在进行” 列表中加入该条内容. 3. 顶部input框中输 ...
- FTPClient工具类
package com.vcredit.ddcash.server.commons.net; import com.vcredit.ddcash.server.commons.model.FtpPar ...
- 如何将PDF文件转Word,有什么方法
PDF文件怎样转换成Word呢?在现在的日常办公中PDF文件和Word文件都是办公必不可少的两种文件格式了.那么当我们在工作中需要对这两种文件进行转换时,我们应该怎样实现呢?下面我们就一起来看一下吧. ...
- redash学习记录
一.简介 一款开源的 BI 工具Redash 二.参考资料 一款开源的 BI 工具Redash 浅析数据查询与可视化工具--Redash
- Linux 安装mysql mariadb配置
CentOS 7.0 使用 yum 安装 MariaDB 与 MariaDB 的简单配置 1.安装MariaDB 安装命令 yum -y install mariadb mariadb-server ...
- Gym 101194A / UVALive 7897 - Number Theory Problem - [找规律水题][2016 EC-Final Problem A]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...