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 ...
随机推荐
- 如何查看电脑硬盘是gpt分区还是MBR分区
首先我们右键点击 我的电脑 ,然后在弹出的快捷菜单中我们点击 管理,如下图: 然后在 计算机管理 对话框中,我们可以看到在左侧的存储下有一个 磁盘管理,这里我们点击 磁盘管理,如下图: 点击 磁盘管理 ...
- 五 web爬虫,scrapy模块,解决重复ur——自动递归url
一般抓取过的url不重复抓取,那么就需要记录url,判断当前URL如果在记录里说明已经抓取过了,如果不存在说明没抓取过 记录url可以是缓存,或者数据库,如果保存数据库按照以下方式: id URL加密 ...
- 硬链接、软链接和inode
一.inode 在Linux中,“一切皆文件”. 唯一标识文件的是inode而非文件名,文件名仅是为了方便人们的记忆和使用,系统或程序通过 inode 寻找正确的文件数据块. 什么是数据块呢?文件储存 ...
- 2017.2.9日总结--外网IP和内网IP
Linux自己不能通过自己的外网访问自己必须通过内网访问自己
- L132
Major Opioid Maker to Pay for Overdose-Antidote Development A company whose prescription opioid mark ...
- main(int argc, char *argv[])详解
argc是命令行总的参数个数 argv[]是argc个参数,其中第0个参数是程序的全名,以后的参数 命令行后面跟的用户输入的参数,比如: int main(int ar ...
- 任务调度 Spring Task 4(二 )
注解和配置文件两种 第一种:配置文件方式 第一步:编写作业类 即普通的pojo,如下: import org.springframework.stereotype.Service; @Service ...
- 在ubuntu14.4里编译UBOOT出错
出错信息如下: OBJCOPY examples/standalone/hello_world.bin LDS u-boot.lds LD u-boot./scripts/dtc ...
- 人生苦短之我用Python篇(遍历、函数、类)
#遍历 info = {'key1':'value1','key2':'value2','key3':'value3'} #方式一 for i in info: print(i,info[i]) #方 ...
- 使用WPScan破解wordpress站点密码
我这里使用的Kali Linux,它默认安装了WPScan. 在使用WPScan之前,先更新它的漏洞数据库: # wpscan –update 扫描wordpress用户 wpscan -–url [ ...