ansible.md
ansible
测试环境配置
注意:192.168.100.201这台机器是主控机,剩下的192.168.100.202、192.168.100.203、192.168.100.210均为测试主机。
# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
82:68:12:6c:a7:62:24:15:7c:e4:6f:92:42:3a:64:66 root@node1
The key's randomart image is:
+--[ RSA 2048]----+
| .oo. |
|..... |
|oE.o. |
|O+o. + |
|=o+ + + S |
|o+ . o . |
| |
| |
| |
+-----------------+
# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.100.202
The authenticity of host '192.168.100.202 (192.168.100.202)' can't be established.
RSA key fingerprint is c4:4c:b0:22:d2:20:46:98:43:8c:19:fc:98:88:eb:9b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.100.202' (RSA) to the list of known hosts.
root@192.168.100.202's password:
Now try logging into the machine, with "ssh 'root@192.168.100.202'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.100.203
# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.100.210
主控机配置
# tail -5 /etc/ansible/hosts
[web]
192.168.100.202
192.168.100.203
[db]
192.168.100.210
测试
# ansible all -m ping
192.168.100.202 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.100.203 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.100.210 | SUCCESS => {
"changed": false,
"ping": "pong"
}
命令参数
- -a MODULE_ARGS, --args=MODULE_ARGS:模块参数
- --ask-vault-pass:加密playbook文件时提示输入密码
- -B SECONDS, --background=SECONDS:后台执行命令,超过SECONDS秒后终止正在执行的命令
- -D, --diff:当更新的文件数及内容较少时,该选项可显示这些文件不同的地方
- -e EXTRA_VARS, --extra-vars=EXTRA_VARS:在playbook中引入外部变量
- -f FORKS, --forks=FORKS:并发线程数,默认是5个
- -i INVENTORY, --inventory-file=INVENTORY:指定要读取的inventory文件
- -l SUBSET, --limit=SUBSET:指定运行的主机(正则)
- --list-hosts:列出符合条件的主机列表,不执行任何命令
- -m MODULE_NAME, --module-name=MODULE_NAME:指定执行使用的模块
- -M MODULE_PATH, --module-path=MODULE_PATH:指定模块存放路径,默认/usr/share/ansible,也可以通过ANSIBLE_LIBRARY设定默认路径
- -P POLL_INTERVAL, --poll=POLL_INTERVAL:定期返回后台认任务进度
- --syntax-check:检测playbook中的语法书写
- -t TREE, --tree=TREE:输出信息至TREE目录中,结果文件以远程主机名命名
- -v, --verbose:输出更详细的执行过程信息,-vvv可得到执行过程所有信息
- -k, --ask-pass:认证密码
- --private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE:指定密钥文件
- -u REMOTE_USER, --user=REMOTE_USER:指定远程主机以REMOTE_USER运行命令
- -c CONNECTION, --connection=CONNECTION:指定连接方式
- -T TIMEOUT, --timeout=TIMEOUT:指定连接远程主机的最大超时,单位是秒
- -s, --sudo:相当于Linux下的sudo命令
- -U SUDO_USER, --sudo-user=SUDO_USER:使用sudo相当于Linux下的sudo命令
常用模块
shell
默认情况下,ansible使用的module 是 command,这个模块并不支持 shell 变量和管道等,若想使用shell 来执行模块,请使用-m 参数指定 shell 模块,但是值得注意的是普通的命令执行模块是通过python的ssh执行。
举例
# ansible all -m shell -a 'ps aux |grep nginx'
192.168.100.202 | SUCCESS | rc=0 >>
root 1896 0.0 0.1 44728 1096 ? Ss 12:06 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
nginx 1899 0.0 0.1 45172 1672 ? S 12:06 0:00 nginx: worker process
root 3311 0.0 0.1 106092 1120 pts/1 S+ 16:56 0:00 /bin/sh -c ps aux |grep nginx
root 3313 0.0 0.0 103324 864 pts/1 S+ 16:56 0:00 grep nginx
192.168.100.203 | SUCCESS | rc=0 >>
root 3585 0.0 0.1 106092 1120 pts/1 S+ 20:24 0:00 /bin/sh -c ps aux |grep nginx
root 3587 0.0 0.0 103324 860 pts/1 S+ 20:24 0:00 grep nginx
192.168.100.210 | SUCCESS | rc=0 >>
root 7344 0.0 0.1 106092 1128 pts/1 S+ 20:24 0:00 /bin/sh -c ps aux |grep nginx
root 7346 0.0 0.0 103320 856 pts/1 S+ 20:24 0:00 grep nginx
copy
实现主控端向目标主机拷贝文件,类似于scp的功能。
举例
# ansible web -m copy -a "src=/etc/fstab dest=/tmp mode=0600"
# ansible web -m command -a 'ls -l /tmp/fstab'
192.168.100.203 | SUCCESS | rc=0 >>
-rw------- 1 root root 871 3月 12 20:31 /tmp/fstab
192.168.100.202 | SUCCESS | rc=0 >>
-rw------- 1 root root 871 3月 12 17:03 /tmp/fstab
file
file模块称之为文件属性模块,可以做的操作如下:
使用 file 模块创建目录:
# ansible db -m file -a "dest=/tmp/study mode=700 owner=root group=ftp state=directory"
192.168.100.210 | SUCCESS => {
"changed": true,
"gid": 50,
"group": "ftp",
"mode": "0700",
"owner": "root",
"path": "/tmp/study",
"size": 4096,
"state": "directory",
"uid": 0
}
# ansible db -m command -a 'ls -dl /tmp/study'
192.168.100.210 | SUCCESS | rc=0 >>
drwx------ 2 root ftp 4096 3月 12 20:44 /tmp/study
创建文件:
# ansible db -m file -a 'dest=/tmp/study/1.txt state=touch mode=600'
192.168.100.210 | SUCCESS => {
"changed": true,
"dest": "/tmp/study/1.txt",
"gid": 0,
"group": "root",
"mode": "0600",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
# ansible db -m command -a 'ls -l /tmp/study/1.txt'
192.168.100.210 | SUCCESS | rc=0 >>
-rw------- 1 root root 0 3月 12 21:00 /tmp/study/1.txt
删除文件
# ansible db -m file -a 'dest=/tmp/study/1.txt state=absent'
192.168.100.210 | SUCCESS => {
"changed": true,
"path": "/tmp/study/1.txt",
"state": "absent"
}
stat
获取远程文件状态信息,包含atime、ctime、mtime、md5、uid、gid等:
# ansible db -m stat -a 'path=/tmp/study'
yum
- name: install the latest version of Apache
yum: name=httpd state=latest
- name: remove the Apache package
yum: name=httpd state=absent
- name: install the latest version of Apache from the testing repo
yum: name=httpd enablerepo=testing state=present
- name: install one specific version of Apache
yum: name=httpd-2.2.29-1.4.amzn1 state=present
- name: upgrade all packages
yum: name=* state=latest
- name: install the nginx rpm from a remote repo
yum: name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present
- name: install nginx rpm from a local file
yum: name=/usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present
- name: install the 'Development tools' package group
yum: name="@Development tools" state=present
- name: install the 'Gnome desktop' environment group
yum: name="@^gnome-desktop-environment" state=present
cron
在指定节点上定义一个计划任务,每隔3分钟到主控端更新一次时间:
ansible all -m cron -a 'name="ntp date" minute=*/5 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 1.asia.pool.ntp.org"'
192.168.100.210 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"ntp date"
]
}
192.168.100.203 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"ntp date"
]
}
192.168.100.202 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"ntp date"
]
}
# ansible all -m command -a 'crontab -l'
192.168.100.203 | SUCCESS | rc=0 >>
#Ansible: ntp date
*/5 * * * * /usr/sbin/ntpdate 1.asia.pool.ntp.org
192.168.100.202 | SUCCESS | rc=0 >>
#Ansible: ntp date
*/5 * * * * /usr/sbin/ntpdate 1.asia.pool.ntp.org
192.168.100.210 | SUCCESS | rc=0 >>
#Ansible: ntp date
*/5 * * * * /usr/sbin/ntpdate 1.asia.pool.ntp.org
service
启动指定节点上的 httpd 服务,并让其开机自启动:
# ansible web -a 'rpm -qa httpd'
192.168.100.203 | SUCCESS | rc=0 >>
httpd-2.2.15-55.el6.centos.2.x86_64
192.168.100.202 | SUCCESS | rc=0 >>
httpd-2.2.15-56.el6.centos.3.x86_64
# ansible web -a 'chkconfig --list httpd'
192.168.100.202 | SUCCESS | rc=0 >>
httpd 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
192.168.100.203 | SUCCESS | rc=0 >>
httpd 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
# ansible web -a '/etc/init.d/httpd status'
192.168.100.202 | FAILED | rc=3 >>
httpd 已停
192.168.100.203 | FAILED | rc=3 >>
httpd 已停
# ansible web -m service -a 'name=httpd state=started enabled=yes'
192.168.100.202 | SUCCESS => {
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started"
}
192.168.100.203 | SUCCESS => {
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started"
}
# ansible web -a '/etc/init.d/httpd status'
192.168.100.203 | SUCCESS | rc=0 >>
httpd (pid 4901) 正在运行...
192.168.100.202 | SUCCESS | rc=0 >>
httpd (pid 4688) 正在运行...
# ansible web -a 'chkconfig --list httpd'
192.168.100.202 | SUCCESS | rc=0 >>
httpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
192.168.100.203 | SUCCESS | rc=0 >>
httpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
script
在指定节点上执行/root/test.sh脚本(该脚本是在ansible控制节点上的):
# cat test.sh
#!/bin/bash
uptime
echo "Hello world!"
# ansible db -m script -a '/root/test.sh'
192.168.100.210 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.100.210 closed.\r\n",
"stdout": " 21:43:11 up 4:35, 2 users, load average: 0.16, 0.03, 0.01\r\nHello world!\r\n",
"stdout_lines": [
" 21:43:11 up 4:35, 2 users, load average: 0.16, 0.03, 0.01",
"Hello world!"
]
}
get_url
下载lrzsz到web组机器的/tmp目录中:
# ansible web -m get_url -a 'url=https://mirrors.aliyun.com/centos/6.8/os/x86_64/Packages/lrzsz-0.12.20-27.1.el6.x86_64.rpm dest=/tmp/'
192.168.100.203 | SUCCESS => {
"changed": false,
"checksum_dest": "5fa0cc444e4474cab0198af83e405224b6130c7b",
"checksum_src": "5fa0cc444e4474cab0198af83e405224b6130c7b",
"dest": "/tmp/lrzsz-0.12.20-27.1.el6.x86_64.rpm",
"gid": 0,
"group": "root",
"md5sum": "2cc2edecc0e4f553a4ec0e5db49c1ec6",
"mode": "0644",
"msg": "OK (72436 bytes)",
"owner": "root",
"size": 72436,
"src": "/tmp/tmp1WXVKL",
"state": "file",
"uid": 0,
"url": "https://mirrors.aliyun.com/centos/6.8/os/x86_64/Packages/lrzsz-0.12.20-27.1.el6.x86_64.rpm"
}
192.168.100.202 | SUCCESS => {
"changed": false,
"checksum_dest": "5fa0cc444e4474cab0198af83e405224b6130c7b",
"checksum_src": "5fa0cc444e4474cab0198af83e405224b6130c7b",
"dest": "/tmp/lrzsz-0.12.20-27.1.el6.x86_64.rpm",
"gid": 0,
"group": "root",
"md5sum": "2cc2edecc0e4f553a4ec0e5db49c1ec6",
"mode": "0644",
"msg": "OK (72436 bytes)",
"owner": "root",
"size": 72436,
"src": "/tmp/tmpMxIP4A",
"state": "file",
"uid": 0,
"url": "https://mirrors.aliyun.com/centos/6.8/os/x86_64/Packages/lrzsz-0.12.20-27.1.el6.x86_64.rpm"
}
ansible.md的更多相关文章
- 初探ansible安装
一.ansible介绍常用的自动化运维工具 Puppet —基于 Ruby 开发,采用 C/S 架构,扩展性强,基于 SSL,远程命令执行相对较弱SaltStack —基于 Python 开发,采用 ...
- Ansible详解(一)
简介 Ansible是一个简单的自动化运维管理工具,基于Python语言实现,由Paramiko和PyYAML两个关键模块构建,可用于自动化部署应用.配置.编排task(持续交付.无宕机更新等).主版 ...
- Ansible 系列之 Ad-Hoc介绍及使用
Ad-Hoc 介绍 一.什么是ad-hoc 命令? ad-hoc 命令是一种可以快速输入的命令,而且不需要保存起来的命令.就相当于bash中的一句话shell.这也是一个好的地方,在学习ansible ...
- Ansible进阶--playbook的使用
一.什么是playbooksplaybooks是ansible的脚本.如同shell脚本一样,它是控制远程主机的一系列命令的集合,通过YAML语言编写.执行一些简单的任务,我们可以使用ad-hoc命令 ...
- Flask Ansible自动化平台搭建(持续更新)
一:简介 使用Ansible + Flask + Celery搭建web平台. 目录结构 . ├── ansible_api │ ├── ansible_playbook_inventory.py ...
- Jenkins + Ansible + Gitlab之gitlab篇
前言 持续交付 版本控制器:Gitlab.GitHub 持续集成工具:jenkins 部署工具:ansible 课程安排 Gitlab搭建与流程使用 Ansible环境配置与Playbook编写规范 ...
- [转帖]Ansible管理windows集群
Ansible管理windows集群 http://www.cnblogs.com/Dev0ps/p/10026908.html 写的挺好的 我关注点还是不够好呢 最近公司新项目需要安装400+win ...
- ansible配置文件详解
# ansible配置文件配置 配置项介绍 , 配置文件ansible.cfg, 运行playbook时,默认时在yaml文件所在路径寻找,然后再去/etc/ansible/下寻找 [defaults ...
- ansible基础-roles
一 简介 注:本文demo使用ansible2.7稳定版 在我看来,role是task文件.变量文件.handlers文件的集合体,这个集合体的显著特点是:可移植性和可重复执行性. 实践中,通常我们以 ...
随机推荐
- tap 和click 事件区别
clike事件和 Zepto.js 中tap的区别 首先介绍下Zepto: 最初是作为移动端开发的库,但是却可以作为JQuery轻量级的替代品,因为API和JQuery相似,而文件更小. 介绍下tap ...
- Service生命周期以及应用
Service概念及用途: Android中的服务,它与Activity不同,它是不能与用户交互的,不能自己启动的,运行在后台的程序,如果我们退出应用时,Service进程并没有结束,它仍然在后台运行 ...
- 二:java常用快捷键
ctrl+F6 切换编辑器 Ctrl+E 快速显示当前Editer的下拉列表 Ctrl+1 快速修复 Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Alt+Shif ...
- Netty面试
声明:此文章非本人所 原创,是别人分享所得,如有知道原作者是谁可以联系本人,如有转载请加上此段话 1.BIO.NIO 和 AIO 的区别? BIO:一个连接一个线程,客户端有连接请求时服务器端就需要 ...
- Spring 、SpringMVC 、Struts2之间的区别
一.Spring与SpringMVC的区别: spring是一个开源框架,是为了解决企业应用程序开发,功能如下: 功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能 范围:任何Ja ...
- CF696C PLEASE
矩阵快速幂+扩展欧拉定理 对于一个矩阵\(A\),我们有\(A^n \equiv A^{n\% \phi(m)+\phi(m)}(\%m)\) 经过简单的列举或推导可得 设目前进行了\(x\)轮,\( ...
- git使用总结(常用命令)
前言 写这篇文章的目的是让新手能够操作git管理自己的代码,可能你知道git的各种命令但是对其使用顺序并不熟,比如我.所以有必要整合一篇关于命令使用步骤的文章,图片用的人家的,也没询问让不让用,可能会 ...
- $.extend 的相关用法
1.1 $.extend(result,item1,item2…..) 将所有的参数项都合并result中,返回result,会破坏result的结构. 1.2 $.extend({},item1,i ...
- 网络I/O模型--06异步I/O
异步I/O (又称为 AIO )则是采用“订阅一通知”工作模式 : 即应用程序向操作系统注册I/O监听,然后继续做自己的事情.当操作系统发生I/O事件,并且准备好数据后 , 再主动通知应用程序,触发相 ...
- DNS隧道实战&&cobaltstrike利用dns隧道
前言 使用 dns 隧道进行 tcp 通信. 正文 首先配置域名 配置一个 A 记录指向我们的 vps, 然后配置几个 ns 记录,指向刚刚设置的 A 记录 然后在服务端安装 wget https:/ ...