Ansible 小手册系列 七(Ad-hoc)
ad hoc——临时的,在ansible中是指需要快速执行,并且不需要保存的命令。说白了就是执行简单的命令—一条命令。
定义主机清单
cat /etc/ansible/hosts
[web]
192.168.77.129 ansible_ssh_user=root ansible_ssh_pass=123456
执行shell
# 获取web组里得eth0接口信息
ansible web -a "ifconfig eth0"
执行ifconfig eth0 命令,ansible模块 默认是command,它不会通过shell进行处理,所以像$ HOME和像“<”,“>”,“|”,“;” 和“&”将不工作(如果您需要这些功能,请使用shell模块)。
# 以shell解释器执行脚本
ansible web -m shell -a "ifconfig eth0|grep addr" # 以raw模块执行脚本
ansible web -m raw -a "ifconfig eth0|grep addr" # 将本地脚本传送到远程节点上运行
ansible web -m script -a ip.sh
传输文件
# 拷贝本地的/etc/hosts 文件到web组所有主机的/tmp/hosts(空目录除外)
ansible web -m copy -a "src=/etc/hosts dest=/tmp/hosts" # 拷贝本地的ntp文件到目的地址,设置其用户及权限,如果目标地址存在相同的文件,则备份源文件。
ansible web -m copy -a "src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes force=yes" # file 模块允许更改文件的用户及权限
ansible web -m file -a "dest=/tmp/a.txt mode=600 owner=user group=user" # 使用file 模块创建目录,类似mkdir -p
ansible web -m file -a "dest=/tmp/test mode=755 owner=user group=user state=directory" # 使用file 模块删除文件或者目录
ansible web -m file -a "dest=/tmp/test state=absent" # 创建远程主机软连接,并设置所属用户和用户组,这里的src表示远端主机的文件
ansible web -m file -a "src=/root/1.txt dest=/tmp/1.txt.link owner=user group=user state=link" # touch 一个文件并添加用户读写权限,用户组去除写执行权限,其他组减去读写执行权限
ansible web -m file -a "path=/etc/foo.conf state=touch mode='u+rw,g-wx,o-rwx'"
管理软件包
# 注释:Ansible 支持很多操作系统的软件包管理,使用时-m 指定相应的软件包管理工具模块,如果没有这样的模块,可以自己定义类似的模块或者使用command 模块来安装软件包 # 安装 最新的 Apache
ansible web -m yum -a "name=httpd state=latest" # 删除apache
ansible web -m yum -a "name=httpd state=absent" # 从testing 仓库中安装最后一个版本得apache
ansible web -m yum -a "name=httpd enablerepo=testing state=present" # 更新所有的包
ansible web -m yum -a "name=* state=latest" # 安装远程的rpm包
ansible web -m yum -a "name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present" # 安装 'Development tools' 包组
ansible web -m yum -a "name='@Development tools' state=present"
用户和用户组
# 添加用户 'user'并设置其 uid 和主要的组'admin'
ansible web -m user -a "name=user comment='I am user ' uid=1040 group=admin" # 添加用户 'user'并设置其登陆shell,并将其假如admins和developers组
ansible web -m user -a "name=user shell=/bin/bash groups=admins,developers append=yes" # 删除用户 'user '
ansible web -m user -a "name=user state=absent remove=yes" # 创建 user用户得 2048-bit SSH key,并存放在 ~user/.ssh/id_rsa
ansible web -m user -a "name=user generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa" # 设置用户过期日期
ansible web -m user -a "name=user shell=/bin/zsh groups=nobdy expires=1422403387" # 创建test组,并设置git为1000
ansible web -m group -a "name=test gid=1000 state=present" # 删除test组
ansible web -m group -a "name=test state=absent"
源码部署
# Ansible 模块能够通知变更,当代码更新时,可以告诉Ansible 做一些特定的任务,比如从git 部署代码然后重启apache 服务等
ansible web-m git -a "repo=https://github.com/Icinga/icinga2.git dest=/tmp/myapp version=HEAD"
服务管理
# 确保web组所有主机的httpd 是启动的
ansible web-m service -a "name=httpd state=started" # 重启web组所有主机的httpd 服务
ansible web-m service -a "name=httpd state=restarted" # 确保web组所有主机的httpd 是关闭的
ansible web-m service -a "name=httpd state=stopped"
后台运行
这个后台运行测试不成功。具体原因未知,尼玛,后面测试playbook时继续看看
# 长时间运行的操作可以放到后台执行,ansible 会检查任务的状态;在主机上执行的同一个任
务会分配同一个job ID
后台执行命令3600s,-B 表示后台执行的时间
ansible all -B 3600 -a "/usr/bin/long_running_operation --do-stuff"
检查任务的状态
这个也测试未成功,尼玛的,后面测试playbook时继续看看
ansible all -m async_status -a "jid=123456789"
# 后台执行命令最大时间是1800s 即30 分钟,-P 每60s 检查下状态默认15s
ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff"
定时任务
# 每天5点,2点得时候执行 ls -alh > /dev/null
ansible test -m cron -a "name='check dirs' minute='0' hour='5,2' job='ls -alh > /dev/null'"
搜集系统信息
# 搜集主机的所有系统信息
ansible all -m setup # 搜集系统信息并以主机名为文件名分别保存在/tmp/facts 目录
ansible all -m setup --tree /tmp/facts # 搜集和内存相关的信息
ansible all -m setup -a 'filter=ansible_*_mb' # 搜集网卡信息
ansible all -m setup -a 'filter=ansible_eth[0-2]'
Ansible 小手册系列 七(Ad-hoc)的更多相关文章
- Ansible 小手册系列 十四(条件判断和循环)
条件判断 When 语句 在when 后面使用Jinja2 表达式,结果为True则执行任务. tasks: - name: "shut down Debian flavored syste ...
- Ansible 小手册系列 二十(经常遇到的问题)
(1). 怎么为任务设置环境变量? - name: set environment shell: echo $PATH $SOME >> /tmp/a.txt environment: P ...
- Ansible 小手册系列 十九(常见指令表)
Play 指令 说明 accelerate 开启加速模式 accelerate_ipv6 是否开启ipv6 accelerate_port 加速模式的端口 always_run any_error ...
- Ansible 小手册系列 十七(特性模块)
异步操作和轮询 --- # Requires ansible 1.8+ - name: 'YUM - fire and forget task' yum: name=docker-io state=i ...
- Ansible 小手册系列 十八(Lookup 插件)
file:获取文件内容 --- - hosts: all vars: contents: "{{ lookup('file', '/etc/foo.txt') }}" tasks: ...
- Ansible 小手册系列 十三(Jinja2)
用于playbook中的jinja 2过滤器 更改数据格式,其结果是字符串 {{ some_variable | to_json }} {{ some_variable | to_yaml }} 对于 ...
- Ansible 小手册系列 十二(Facts)
Facts 是用来采集目标系统信息的,具体是用setup模块来采集得. 使用setup模块来获取目标系统信息 ansible hostname -m setup 仅显示与ansible相关的内存信息 ...
- Ansible 小手册系列 十一(变量)
变量名约束 变量名称应为字母,数字和下划线. 变量应始终以字母开头. 变量名不应与python属性和方法名冲突. 变量使用 通过命令行传递变量(extra vars) ansible-playbook ...
- Ansible 小手册系列 十(包含和角色)
一.包含 (include) 使用include模块来包含foo文件 tasks: - include: foo.yml --- foo.yml - name: test foo command: e ...
随机推荐
- XE6移动开发环境搭建之IOS篇(7):在Mac OSX 10.8中安装XE6的PAServer(有图有真相)
XE6移动开发环境搭建之IOS篇(7):在Mac OSX 10.8中安装XE6的PAServer(有图有真相) 2014-08-22 21:06 网上能找到的关于Delphi XE系列的移动开发环境的 ...
- yield的表达式形式的应用(待补充)
1.yield的表达式形式应用的定义: 在一个生成器函数内,将yield赋值给一个变量,这就是yield的表达式形式.也叫生成器的表达式形式 2.send方法的定义: (1)定义: yield的表达式 ...
- Web 框架 Flask
Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求进行预处理,然后 ...
- iClap:产品经理再忙也要看《琅琊榜》
最先知道<琅琊榜>,是半年前偶然看了整整21分钟的<琅琊榜>片花,对麒麟才子梅长苏这一角色甚是期待,开播后每集必看,重复看,此剧果真不负众望,口碑爆棚,收视爆红,确是一部久违的 ...
- org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.my.service.ProductService] for bean with name 'productService' defi报错解决方法
原 javaweb项目报错org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [XXX] ...
- JMeter的安装和目录解析
Ubuntu系统中jmeter的安装和目录解析 作为一个Linux新手,在使用jdk时,或许会安装配置多次仍然导致无法使用情况(如无法登录系统等),请按如下步骤一步一步安装并配置 相关软件下载地址 J ...
- java反射 - getXXX 与 getDeclaredXXX
1.getXXX 和 getDeclaredXXX java 里 Class<?> 有下面这些方法: 类似的方法有: 2.getMethod(s) 和 getDeclaredMethod( ...
- Centos下Nginx配置WEB访问日志并结合shell脚本定时切割
在一个成熟的WEB系统里,没有日志管理是不可以的,有了日志,可以帮助你得到用户地域来源.跳转来源.使用终端.某个URL访问量等相关信息:通过错误日志,你可以得到系统某个服务或server的性能瓶颈等. ...
- Python3.x:报错POST data should be bytes, an iterable of bytes
Python3.x:报错POST data should be bytes, an iterable of bytes 问题: python3.x:报错 POST data should be byt ...
- 20145307第9周JAVA学习报告
20145307陈俊达 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC(Java DataBase Connectivity)即java数据库连接,是一种用于执行SQL语句 ...