ansible命令使用
ansible命令使用
查看每个服务器的主机名
|
1
|
$ ansible multi -a "hostname" |
使用一个线程执行命令,相当于顺序在每个服务器上运行(默认5个线程执行)
|
1
|
$ ansible multi -a "hostname" -f 1 |
查看你的环境情况:
查看磁盘使用情况
|
1
|
$ ansible multi -a "df -h" |
查看内存使用情况
|
1
|
$ ansible multi -a "free -m" |
查看时间是否准确
|
1
|
$ ansible multi -a "date" |
如果时间不一致,可以使用ntpdate 同步一下
$ ansoble multi -a "ntpdate cn.pool.ntp.org"
三:配置两台应用服务器
前提是安装好epel源和centos base源(可以使用阿里云的镜像源)
|
1
2
3
|
$ ansible app -m yum -a "name=MySQL-python state=present"$ ansible app -m yum -a "name=python-setuptools state=present"$ ansible app -m easy_install -a "name=django" |
测试django是否安装正确
|
1
2
3
4
5
6
|
root@~# ansible app -a "python -c 'import django; print django.get_version()'"10.0.0.131 | success | rc=0 >>1.1010.0.0.130 | success | rc=0 >>1.10 |
四:配置数据库服务器
|
1
2
|
$ ansible db -m yum -a "name=mysql-server state=present"$ ansible db -m service -a "name=mysqld state=started enabled=yes" |
配置数据库用户django,并且赋予权限
|
1
2
3
|
$ ansible db -m yum -a "name=MySQL-python state=present"$ ansible db -m mysql_user -a "name=django host=% password=12345 \priv=*.*:ALL state=present |
五:限制命令只在一个服务器上生效
|
1
|
$ ansible app -a "service ntpd restart" --limit "10.0.0.132" |
|
1
2
3
|
# Limit hosts with a simple pattern (asterisk is a wildcard).$ ansible app -a "service ntpd restart" --limit "*.4"#以4结尾的ip地址,将会执行命令 |
|
1
2
3
|
# Limit hosts with a regular expression (prefix with a tilde).$ ansible app -a "service ntpd restart" --limit ~".*\.4"#使用正则表达式匹配主机 |
六:管理系统用户和组
系统添加admin组
|
1
|
$ ansible app -m group -a "name=admin state=present" |
系统添加jwh566用户
|
1
|
$ ansible app -m user -a "name=jwh5566 group=admin createhome=yes" |
删除系统用户
|
1
|
$ ansible app -m user -a "name=jwh5566 state=absent remove=yes" |
七:管理文件和目录
获取文件的信息,权限,所有者等
|
1
|
$ ansible multi -m stat -a "path=/etc/environment" |
复制文件到服务器
|
1
|
$ ansible multi -m copy -a "src=/etc/hosts dest=/tmp/hosts" |
从服务器接收文件(接收到控制机)
|
1
|
$ ansible multi -m fetch -a "src=/etc/hosts dest=/tmp" |
创建目录
|
1
|
$ ansible multi -m file -a "dest=/tmp/test mode=644 state=directory" |
创建符号链接
|
1
2
|
$ ansible multi -m file -a "src=/src/symlink dest=/dest/symlink \owner=root group=root state=link" |
删除目录和文件
|
1
|
$ ansible multi -m file -a "dest=/tmp/test state=absent" |
八:运行后台任务
-B <seconds> 指定运行任务的最大时间
-P <seconds> 指定多久时间去一次服务器查看任务执行的状态
异步更新服务器(根据系统情况,可能需要很长时间)
|
1
2
3
4
5
6
7
|
$ ansible multi -B 3600 -a "yum -y update" background launch... 10.0.0.132 | success >> { "ansible_job_id": "763350539037", "results_file": "/root/.ansible_async/763350539037", "started": 1 |
如果说后台任务还在运行,使用下面的命令查看运行状态
|
1
|
$ ansible multi -m async_status -a "jid=763350539037" |
九:检查日志文件
|
1
|
$ ansible multi -a "tail /var/log/messages" |
如果需要grep,需要使用shell模块
|
1
2
3
4
5
6
7
8
9
10
|
root@~# ansible multi -m shell -a "tail /var/log/messages | \ grep ansible-command | wc -l"10.0.0.131 | success | rc=0 >>210.0.0.130 | success | rc=0 >>210.0.0.141 | success | rc=0 >>6 |
这个命令显示每台服务器分别执行了几次ansible命令
十:管理crontab 任务
|
1
2
|
$ ansible multi -m cron -a "name='daily-cron-all-servers' \hour=4 job='/path/to/daily-script.sh'" |
可以使用这个配置ntp 任务
删除crontab任务
|
1
|
$ ansible multi -m cron -a "name='daily-cron-all-servers' state=absent" |
ansible命令使用的更多相关文章
- ansible命令执行模块使用
ansible命令执行模块使用 1.命令执行模块-command 在远程节点上运行命令. 命令模块使用命令名称,接上空格-的分割符作为参数使用,但是不支持管道符和变量等,如果要使用这些,那么可以使用s ...
- ansible命令应用基础
ansible命令应用基础: Usage: ansible <host-pattern> [-f forks] [-m module_name][-a args] -f ...
- ansible命令应用示例
ansible命令应用示例 ping slave组 ansible slave -m ...
- 自动化运维工具——ansible命令使用(二)
一.Ansible系列命令使用 ansible命令执行过程 1 . 加载自己的配置文件 默认/etc/ansible/ansible.cfg 2 . 加载自己对应的模块文件,如command 3 . ...
- 3.3、Ansible命令参数详解
0.ansible 命令参数详解: [root@localhost ~]# ansible Usage: ansible <host-pattern> [options] Options: ...
- 3.1、Ansible命令简要说明及初步使用
1.Ansible命令 1.1 Ad-hoc说明 Ansible中有一个很重要的功能就是可以执行ad-hoc命令,它表示即时.临时的意思,即表示一次性的命令.与之相对的是ansible playboo ...
- Linux命令集锦:ansible命令
ansible 命令主要用于批量管理,来实现自动化管理.常用批量操作包括:主机分组管理.实时批量执行命令或脚本.实时批量分发文件或目录.定时同步文件等. 一.安装 ansible yum instal ...
- 关于ansible命令的执行过程
首先说明一下.每一个模块.都是有相应的.py文件的,可以通过rpm -ql ansible看到 要观察模块执行过程,可以在ansible命令执行时加上-v或-vvv,或者-vvvv看得更加详细 ans ...
- ansible命令
ansible 默认提供了很多模块来供我们使用.在 Linux 中,我们可以通过 ansible-doc -l 命令查看到当前 ansible 都支持哪些模块,通过 ansible-doc -s ...
随机推荐
- 跨平台TTS eSpeak Windows开发
转摘请说明出处:http://www.cnblogs.com/luochengor/p/3511165.html以及作者,谢谢. eSpeak是最为流行的开源跨平台的文本转语音程序.这两天进行了简单的 ...
- [MYSQL]时间毫秒数转换
java中常用bigint字段保存时间,通常将时间保存为一大串数字,每次取出需要在程序里转换,有时候程序里不方便,可以使用MYSQL自带的函数FROM_UNIXTIME(unix_timestamp, ...
- IOS-视频
一.简介 iOS提供了MPMoviePlayerController.MPMoviePlayerViewController两个类,可以用来轻松播放视频和网络流媒体\网络音频 提示:网络音频同样使用此 ...
- oracle objects - Materialized views and Synonyms
Materialized views - 物化视图,不实时查询表,定期更新,查询速度快 视图的更新频率我们可以在这看到:select * from dba_jobs , 一般在创建视图的时候完成的. ...
- 【zzulioj-1731】矩阵
题目描述 输入
- #define GPFCON (* (volatile unsigned long * )0x56000050 )
int a; int *p; p = &a; *p = 0x100; //a=0x100 p = (int *)0x56000050; *p =0x100; *( ( int * ) 0x56 ...
- 条款11:记得在operator=中处理自赋值的情况。
本来的版本是这样的: Widget & Widget::operator=(Widget rhs) { delete pb;//这里可能直接将rhs的pb删除了 pb = new (*rhs. ...
- 类Flask实现前后端交互之代码聊天室
前言 框架 项目目录及各自功能 流程图 后端 server backend exector 前端 ajax 页面更新 演示 简易应答模式 代理模式处理外部请求 后台日志 总结 前言 这两天老是做梦,全 ...
- 从无到有开发自己的Wordpress博客主题---运行环境准备
目前只做本地开发,只在本地搭建测试环境,最后会在服务器的CentOS中搭建正式环境,我使用的是Mac,最简单的办法就是安装MAMP. 1.下载并安装MAMP 下载地址:https://www.mamp ...
- 使用LNMP环境安装typecho博客的全程记录
虽然我是搞asp.net的 但是十分欣赏php,php有很多开源的博客程序 比如大名鼎鼎的Wordpress.还有各种独立博客大牛使用的z-blog,以及短小精悍的emblog. wordpress臃 ...