centos ansible常用命令
ansible在日常运维中经常使用,特别是批量执行多台服务器的时候,有效减小重复的操作成本,以下从安装到使用仅讲解工作中常用的几种方式,模块很多功能很强大,但不做全面讨论。
ansible安装
在centos服务器中安装ansible很简单,只需两条命令:
yum install epel-release
yum -y install ansible
ansible --version
ansible 2.9.16
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /bin/ansible
python version = 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
ansible配置
配置hosts,添加需要被管理的主机
[root@test01 ~]# cat /etc/ansible/hosts
[webservers]
10.124.59.82
10.124.59.83
[dbservers]
10.124.59.208
10.124.59.209
[ftp]
10.124.59.210
生成密钥
[root@test01 ~]# ssh-keygen
使用ssh-copy-id命令来复制ansible公钥到各个节点
[root@test01 ~]# ssh-copy-id root@10.124.59.82
[root@test01 ~]# ssh-copy-id root@10.124.59.83
[root@test01 ~]# ssh-copy-id root@10.124.59.208
[root@test01 ~]# ssh-copy-id root@10.124.59.209
[root@test01 ~]# ssh-copy-id root@10.124.59.210
执行ping命令测试
[root@test01 ~]# ansible all -m ping
10.124.59.210 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.124.59.209 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.124.59.82 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.124.59.83 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.124.59.208 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
success说明安装配置成功。
ansible常用模块与使用
实际使用过程中,会有几个模块经常用到,下面列出如下:
- ping模块
测试主机是否是通的,用法很简单,不涉及参数
以上已经举例,这里不再赘述。
- command模块
ansible管理工具使用-m选项来指定使用模块,默认使用command模块,
即-m选项省略时会运行此模块,用于在被管理主机上运行命令。
远程执行命令,但不支持管道。它是默认命令可不指明模块。
- shell模块
远程执行命令,与command的不同在于可以使用管道。
- copy:拷贝文件到远程主机
用法:
src :本地文件路径,可以是绝对和相对
dest= :不可省,如果src是目录,则dest也是目录。只能是绝对路径
group :指明文件属组
mode :指明权限
owner :指明所有者
content :直接写出内容,并将其复制给远程主机
示例:
复制本地文件到远端主机
[root@test01 ~]# ansible all -m copy -a "src=/tmp/filebeat.yml dest=/tmp/ owner=ansible mode=600"
10.124.59.209 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "203e1c00bc853d638e8c00eaa4015be17ae26900",
"dest": "/tmp/filebeat.yml",
"gid": 1016,
"group": "mgadmin",
"md5sum": "fb66b0662ccea6dd9148d50ed2cdbdb3",
"mode": "0600",
"owner": "ansible",
"size": 10386,
"src": "/home/ansible/.ansible/tmp/ansible-tmp-1628185005.83-13684-201530439549721/source",
"state": "file",
"uid": 1020
}
10.124.59.210 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "203e1c00bc853d638e8c00eaa4015be17ae26900",
"dest": "/tmp/filebeat.yml",
"gid": 1016,
"group": "mgadmin",
"md5sum": "fb66b0662ccea6dd9148d50ed2cdbdb3",
"mode": "0600",
"owner": "ansible",
"size": 10386,
"src": "/home/ansible/.ansible/tmp/ansible-tmp-1628185005.82-13680-7844302613885/source",
"state": "file",
"uid": 1020
}
...省略
- fetch:从远端主机获取文件到本地
用法:
src=远端主机上的文件。
dest=保存文件的目录
示例
[root@test01 tmp]# ansible 10.124.59.83 -m fetch -a "dest=/tmp src=/tmp/filebeat.yml"
10.124.59.83 | CHANGED => {
"changed": true,
"checksum": "203e1c00bc853d638e8c00eaa4015be17ae26900",
"dest": "/tmp/10.124.59.83/tmp/filebeat.yml",
"md5sum": "fb66b0662ccea6dd9148d50ed2cdbdb3",
"remote_checksum": "203e1c00bc853d638e8c00eaa4015be17ae26900",
"remote_md5sum": null
}
注意:获取的文件存放路径为dest_dir/IP|address/src_file
以上就是几个经常使用的命令,另外一些少用的模块,需要的时候到官网或使用ansible-doc
查看帮助即可。
- 官方文档:https://docs.ansible.com/
- ansible-doc查看模块帮助信息的工具
Ansible-doc用来查询ansible模块文档的说明,类似于man命令,针对每个模块都有详细的用法说明及应用案例介绍,语法如下:
ansible-doc [options] [module……]
-l用来列出可使用的模块,
-s用来列出某个模块的描述信息和使用示例。
[root@test01 tmp]# ansible-doc -s command
- name: Execute commands on targets
command:
argv: # Passes the command as a list rather than a string. Use `argv' to avoid quoting values that would otherwise be interpreted incorrectly (for example "user name"). Only the string or the
list form can be provided, not both. One or the other must be provided.
chdir: # Change into this directory before running the command.
cmd: # The command to run.
creates: # A filename or (since 2.0) glob pattern. If it already exists, this step *won't* be run.
free_form: # The command module takes a free form command to run. There is no actual parameter named 'free form'.
removes: # A filename or (since 2.0) glob pattern. If it already exists, this step *will* be run.
stdin: # Set the stdin of the command directly to the specified value.
stdin_add_newline: # If set to `yes', append a newline to stdin data.
strip_empty_ends: # Strip empty lines from the end of stdout/stderr in result.
warn: # Enable or disable task warnings.
---- 钢铁 648403020@qq.com 2021.08.06
参考文献
http://blog.itpub.net/29785807/viewspace-2700983/
https://www.huaweicloud.com/articles/cd442ec1b8aca5208f04555385362147.html
centos ansible常用命令的更多相关文章
- CentOS最常用命令及快捷键整理
CentOS最常用命令及快捷键整理 整理了Linux常用命令及快捷键. 常用命令: 文件和目录: # cd /home 进入 '/home' 目录 # ...
- centos的常用命令
公司服务器主要是centos,第一篇就从centos的常用命令开始吧. 转载自:http://www.cnblogs.com/zitsing/archive/2012/05/02/2479009.ht ...
- ansible常用命令
一.ansible常用命令 一.ansible命令的常用参数 ansible 默认提供了很多模块来供我们使用.在 Linux 中,我们可以通过 ansible-doc -l 命令查看到当前 ansib ...
- CentOS 7 常用命令大全
CentOS7 常用命令集合 这两天一直在对CentOS 7.2进行初体验,各种学习命令肿么用,不过其实大多和DOS是一样的,只是命令的表达上可能有点儿不一样,毕竟这些都不是一家出来的嘛~ 废话不多说 ...
- (转帖)CentOS最常用命令及快捷键整理
原文:http://www.centoscn.com/CentOS/help/2014/0306/2493.html 最近开始学Linux,在VMware Player中安装了CentOS 6.4.为 ...
- linux rhel unix centos FreeBSD 常用命令
一:使用CentOS常用命令查看cpu more /proc/cpuinfo | grep "model name" grep "model name" /pr ...
- CentOS最常用命令
快捷键.常用命令: 文件和目录:# cd /home 进入 '/home' 目录# cd .. 返回上一级目录# cd ../.. 返回上两级目录# cd - 返回上次所在目录# cp file1 f ...
- CentOS 7 常用命令大全(转)
博主最近疯狂迷恋上linux的centos 7 系统,特意从网上找了一篇centos 7的命令大全来学习,下面我分享下这个博客. 转载自:https://blog.csdn.net/o0darknes ...
- CentOS 7常用命令
常用命令 文件与目录操作 命令 解析 cd /home 进入 ‘/home’ 目录 cd .. 返回上一级目录 cd ../.. 返回上两级目录 cd - 返回上次所在目录 cp file1 file ...
随机推荐
- 关于 Windows 下 Qt 开发,这个问题必须要搞清楚!
小伙伴们,大家好,小北师兄又来喂饭啦,从上次写完<一个例子让你秒懂 Qt Creator 编译原理>后,师兄对于 Qt 的一些环境配置有了更深的理解,这对师兄进行 Qt 的后续学习起到了很 ...
- Pycharm破解版_安装从失败到成功
前言: 入门学习的时候一直用的是社区版的,现在想换个专业版的玩玩. 本文使用的环境说明: win10系统 安装过pycharm社区版,已卸载 已安装python 3.8.5 (建议看完整篇文章后再自行 ...
- 【LeetCode每日一题 Day 1】1. 两数之和
大家好,我是编程熊,今天是LeetCode每日一题的第一天,今天的你比昨天更加优秀啦! 题意 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target ...
- Mongo集群搭建
1.集群角色及架构 在搭建集群之前,需要首先了解几个概念:路由,分片.副本集.配置服务器等 mongos,数据库集群请求的入口,所有的请求都通过mongos进行协调,不需要在应用程序添加一个路由选择器 ...
- CentOS8安装GNOME3桌面并设置开机启动图形界面
本篇文章介绍如何在CentOS8 Linux操作系统中安装GNOME3桌面环境和GDM(GNOME Display Manager)现实环境管理器. 环境 CentOS8 Minimal 安装GNOM ...
- explicit 关键字 禁止隐式转换
explicit可以抑制内置类型隐式转换,所以在类的构造函数中,使用explicit关键字,防止不必要的隐式转换
- 1.5Java、万维网以及其他
要点提示:Java是一种功能强大和多用途的编程语言,可用于开发运行在移动设备.台式计算机以及服务器端的软件.
- AcWing 829. 模拟队列
实现一个队列,队列初始为空,支持四种操作: (1) "push x" – 向队尾插入一个数x: (2) "pop" – 从队头弹出一个数: (3) " ...
- ADC采集电流相关知识
1.AD电流采样电路,是把电路中的电流用采样元件转换为电压信号,然后用ADC量化转换为相应的数字信号.需要你在被采集端串联一个采样电阻,然后采集采样电阻两端的电压,这样就可以把电流输出变换为电压输出. ...
- 分享一份550多个Linux命令的文档,按照命令首字母索引排序
输入一个命令,让我给你一个关于它的完美解释! 众所周知,Linux命令是IT人必须掌握的一个技能,有了它,我们可以部署和维护各种各样的服务和应用.但是,大部分的Linux命令我们不一定记得住,而别是各 ...