ansible的模块使用说明
参考官方链接:
https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html#parallelism-and-shell-commands
- shell模块使用
提权操作:
[root@ansible-test ~]# ansible test1 -m shell -a "ls /root" -u zhouyuqiang -k --become -K
SSH password:
SUDO password[defaults to SSH password]:
test1 | CHANGED | rc=0 >>
anaconda-ks.cfg
GateOne-master
master.zip
- copy操作:
官方:
$ ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"
实际:
[root@ansible-test ~]# ansible web1 -m copy -a "src=/root/redis-5.0.3.tar.gz dest=/usr/local/src"
查询:
[root@ansible-test ~]# ansible web1 -m shell -a "ls /usr/local/src"
172.16.188.191 | CHANGED | rc=0 >>
redis-5.0.3.tar.gz
- file模块:
创建文件夹:
state 一定需要指定:
状态:
创建文件夹:directory
(递归)删除文件:absent
创建一个文件:touch
[root@ansible-test ansible-yaml]# ansible web -m file -a "dest=/etc/yum.repos.d/nginx.repo state=absent"
test2 | CHANGED => {
"changed": true,
"path": "/etc/yum.repos.d/nginx.repo",
"state": "absent"
}
test1 | CHANGED => {
"changed": true,
"path": "/etc/yum.repos.d/nginx.repo",
"state": "absent"
}
test3 | CHANGED => {
"changed": true,
"path": "/etc/yum.repos.d/nginx.repo",
"state": "absent"
}
- yum模块
state 一定需要
状态:
具体版本:present
最新版本:latest
name 是指定需要安装的包名
[root@ansible-test opt]# ansible web1 -m yum -a "name=memcached state=present"
卸载一个版本:
指定状态就可以:absent
[root@ansible-test opt]# ansible web1 -m yum -a "name=memcached state=absent"
- user模块
创建一个用户并设置密码:
[root@ansible-test ~]# ansible web1 -m user -a "name=user1 password=1"
查询:
[root@ansible-test ~]# ansible web1 -m user -a "name=user1 password=1"
删除用户:
指定状态即可:absent
[root@ansible-test ~]# ansible web1 -m user -a "name=user1 state=absent"
指定创建用户不能登录,一般是服务用户:
[root@ansible-test ~]# ansible web1 -m user -a "name=user1 password=123 shell=/sbin/nologin "
如下:
user1❌1002:1003::/home/user1:/sbin/nologin
- git 模块
需要指定用户:-uroot
repo指定git仓库地址
目标主机需要有git命令,没有进行安装
[root@ansible-test ~]# ansible web1 -m yum -a "name=git state=latest"
[root@ansible-test ~]# ansible web1 -vvvv -m git -a "repo=https://github.com/ansible/ansible.git dest=/opt/ansibleå-source" -uroot
- Managing Services启动服务模块
启动服务:
state=started
state=started
[root@ansible-test ~]# ansible web -m service -a "name=memcached state=started" -uroot
关闭服务:
state=stopped
[root@ansible-test ~]# ansible web -m service -a "name=memcached state=stopped" -uroot
重新启动:
state=restarted
[root@ansible-test ~]# ansible web -m service -a "name=memcached state=restarted" -uroot
加入开机启动:
enabled=true
[root@ansible-test ~]# ansible web -m service -a "name=memcached enabled=true" -uroot
- setup模块
[root@ansible-test ~]# ansible web -m setup
如:
"ansible_system_capabilities_enforced": "True",
"ansible_system_vendor": "VMware, Inc.",
"ansible_uptime_seconds": 82564,
"ansible_user_dir": "/root",
"ansible_user_gecos": "root",
"ansible_user_gid": 0,
"ansible_user_id": "root",
"ansible_user_shell": "/bin/bash",
"ansible_user_uid": 0,
"ansible_userspace_architecture": "x86_64",
"ansible_userspace_bits": "64",
"ansible_virtualization_role": "guest",
进行过滤操作:
"filter=ansible_user_shell"
[root@ansible-test ~]# ansible web -m setup -a "filter=ansible_user_shell"
test2 | SUCCESS => {
"ansible_facts": {
"ansible_user_shell": "/bin/bash"
[root@ansible-test ~]# ansible web -m setup -a "filter=ansible_all_ipv4_addresses"
test2 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.16.188.191"
]
},
"changed": false
}
可以通过统配符进行匹配:
如:filter=ipv4
[root@ansible-test ~]# ansible web -m setup -a "filter=ipv4"
test2 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.16.188.191"
],
"ansible_default_ipv4": {
"address": "172.16.188.191",
"alias": "eth0",
"broadcast": "172.16.188.255",
"gateway": "172.16.188.1",
- playbook的模块
yaml的语法,采用yaml语法,具体语法其实很简单,主要是空格区分目录。每个目录层级是2个空格,每个:后面一定要需要空格,否则语法报错
ansible的模块使用说明的更多相关文章
- Ansible 常见模块介绍
目录 Ansible 常见模块介绍 ping 模块 command 模块 cron 模块 user 模块 group 模块 copy 模块 file 模块 service 模块 shell 模块 sc ...
- ansible服务模块和组模块使用
本篇文章主要是介绍ansible服务模块和组模块的使用. 主要模块为ansible service module和ansible group moudle,下面的内容均是通过实践得到,可以直接运行相关 ...
- ansible常用模块用法
ansible常用模块用法 2015-07-21 10:25 24458人阅读 评论(1) 收藏 举报 分类: Linux(44) ansible 版权声明:本文为博主原创文章,未经博主允许不得 ...
- Linux之ansible 常用模块
目的 代码发布系统 代码发布:把本地的代码通过某些方式弄到线上,可以供别人访问 部署 前戏 ansible 批量在远程主机上执行命令 puppet ruby ansible saltstack pyt ...
- Ansible常用模块介绍及使用(week5_day1_part2)--技术流ken
Ansible模块 在上一篇博客<Ansible基础认识及安装使用详解(一)--技术流ken>中以及简单的介绍了一下ansible的模块.ansible是基于模块工作的,所以我们必须掌握几 ...
- Ansible常用模块命令
Ansible常用模块命令 一.安装ansible yum install epel-release yum install ansible 二.配置文件配置 vi /etc/ansible/ansi ...
- ansible 常用模块的使用
安装 yum -y install ansible 配置文件/etc/ansible/hosts 模块介绍与使用 ping模块 [root@node1 config]# ansible k8s -m ...
- Ansible 常用模块(一)
一.Ansible简介 Ansible是新出现的自动化运维工具,基于python开发,集合了众多运维工具(puppet(ruby).cfengine.chef.func.fabric.)的优点,实现了 ...
- ansible核心模块playbook介绍
ansible的playbook采用yaml语法,它简单地实现了json格式的事件描述.yaml之于json就像markdown之于html一样,极度简化了json的书写.在学习ansible pla ...
随机推荐
- awk、nawk、mawk、gawk的简答介绍
awk 是一种编程语言,用于在linux/unix下对文本和数据进行处理.数据可以来自标准输入.一个或多个文件,或其它命令的输出(即管道).它支持用户自定义函数和 动态正则表达式等先进功能,是linu ...
- react-router 嵌套路由 内层route找不到
今天在做嵌套路由的时候,没有报错,但是页面显示为空,搜索了一下资料,有两个原因: 1.exact精确匹配 <Route component={xxx} path="/" /& ...
- git 拉取分支代码 合分支
git config --global user.name"yourName" //用户名 yourName git config --global user.email" ...
- angular ng-file-upload
传送门:https://github.com/danialfarid/ng-file-upload#install <script src="angular(.min).js" ...
- linux (1): 启动
很早就接触过linux,但是都是一知半解的用,连皮毛都算不上只记得几个命令而已,故决定好好学习一下linux,当前大环境下如果对linux不熟悉或者说不会基本的使用的话,会遇到很多问题. 历史: (大 ...
- Thinkphp5多数据库切换
在项目开发中需要Thinkphp5读取多个数据库的数据,本文详细介绍Thinkphp5多数据库切换 一.在database.php配置默认数据库连接 'type' => 'm ...
- CSS单位【记录】
1.长度 2.角度 3.时间 4.分辨率 5.颜色 6.函数 7.生成内容 8.图像 9.数字 1.长度 <length>:数字和单位之间没有空格,0之后的长度单位是可选的 相对长度单位 ...
- 极简】如何在服务器上安装SSL证书?
本文适合任何人了解,图形化操作.下面以腾讯云为例,并且服务器(linux)也安装了宝塔面板. 1.登陆腾讯云账号进入控制台,找到SSL的产品 2.按要求申请并填写表单,记住私钥密码 3.提交后,待腾讯 ...
- xml可视化编辑器
——业内首创的在线可视化XML结构化数据编辑方法 Boxth Visual XML Web Editor (Boxth XWE) 是专为在线处理XML结构化数据而设计的在线(Web).可视化(WYSW ...
- 在“非软件企业”开发软件的困局 ZT
软件产品广泛服务于各行业,其开发具有高科技.高投入.高产出.高风险的特点.在项目开发和软件应用中,只有将人员能力的发挥与科学技术的使用应用市场的认识进行最佳的融合,才能发挥软件的效益.普通企业虽涉足软 ...