ansible using method
ansible using method
Ansible模块介绍
1、安装Ansible
# yum -y install epel-release
# yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto
# yum -y install ansible
# ansible --version
ansible 2.1.0.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
SSH密钥
### 2、设置SSH密钥
在Ansible服务端生成密钥,并且复制公钥到节点中。
# ssh-keygen -t rsa
使用ssh-copy-id命令来复制Ansible公钥到节点中。
# ssh-copy-id -i root@10.81.235.190
hosts
3、为Ansible定义节点清单
编辑文件/etc/ansible/hosts 维护着Ansible要管理的服务器清单
# vim /etc/ansible/hosts
[test]
10.81.235.190
10.81.235.195
10.81.235.200
==== ping ====
4、尝试在Ansible服务端运行命令
使用ping检查‘docker-deploy’或者ansible节点的连通性
# ansible -m ping 'test'
10.81.235.190 | SUCCESS => {
"changed": false,
"ping": "pong"
}
10.81.235.195 | SUCCESS => {
"changed": false,
"ping": "pong"
}
10.81.235.200 | SUCCESS => {
"changed": false,
"ping": "pong"
}
执行shell命令
例子1:检查Ansible节点的运行时间(uptime) ,由于这些模块均是默认的,因此可以不加-m 模块名称。
# ansible -m command -a "uptime" 'test'
10.81.235.200 | SUCCESS | rc=0 >>
07:29:36 up 8 days, 26 min, 3 users, load average: 0.20, 0.19, 0.18
10.81.235.190 | SUCCESS | rc=0 >>
07:29:37 up 8 days, 4:32, 3 users, load average: 0.28, 0.23, 0.19
10.81.235.195 | SUCCESS | rc=0 >>
07:29:38 up 8 days, 4:32, 3 users, load average: 0.16, 0.17, 0.17
例子2:重定向输出到文件中
# ansible all -m command -a "df -Th" > ~/52opn.txt -k
# cat ~/52opn.txt
10.81.235.190 | SUCCESS | rc=0 >>
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
ext3 8.2G 3.1G 4.7G 41% /
/dev/sda1 ext3 99M 13M 82M 13% /boot
tmpfs tmpfs 252M 16K 252M 1% /dev/shm
10.81.235.195 | SUCCESS | rc=0 >>
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
ext3 8.2G 3.1G 4.7G 40% /
/dev/sda1 ext3 99M 13M 82M 13% /boot
tmpfs tmpfs 252M 16K 252M 1% /dev/shm
10.81.235.200 | SUCCESS | rc=0 >>
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
ext3 6.7G 3.0G 3.4G 47% /
/dev/sda1 ext3 99M 13M 82M 13% /boot
tmpfs tmpfs 252M 16K 252M 1% /dev/shm
==== ansible常用模块 ====
5、ansible常用模块
command
1、command:命令模块,默认模块,用于在远程执行命令,不支持变量。
# ansible 10.81.235.190 -a 'date' -k
10.81.235.190 | SUCCESS | rc=0 >>
Mon Aug 15 08:02:45 CST 2016
===== cron =====
2、cron:计划任务模块:
# ansible test -m cron -a 'minute="*/10" job="/bin/echo "hello"" name="52opn web"' -k
#Ansible: 52opn web
*/10 * * * * /bin/echo "hello"
user
3、user:用户管理模块:
ansible webserver -m user -a 'name=“node1” ’
===== copy =====
4、copy:远程复制模块
a、复制文件至远程主机:
# ansible all -m copy -a 'src=/etc/vsftpd/vsftpd.conf dest=/etc/vsftpd owner=root mode=640' -k
10.81.235.195 | SUCCESS => {
"changed": false,
"checksum": "2fb3723667a5ee43d828c0a580530ea2c2fd007a",
"dest": "/etc/vsftpd/vsftpd.conf",
"gid": 0,
"group": "root",
"mode": "0640",
"owner": "root",
"path": "/etc/vsftpd/vsftpd.conf",
"size": 4629,
"state": "file",
"uid": 0
}
content
b、往远程主机文件写入内容:使用content代替src。
# ansible all -m copy -a 'content="ansible test host" dest=/etc/my.cnf' -k
ansible test host
===== 文件属性 =====
5、file:设置文件属性模块
a、设置指定文件属性:
# ansible all -m file -a 'owner=tester group=tester mode=775 path=/usr/local/src/name.txt' -k
ll -rw-r--r-- 1 root root 14 Aug 15 06:37 name.txt
ll -rwxrwxr-x 1 tester tester 14 Aug 15 06:37 name.txt
===== 文件属性 link =====
b、创建文件符号链接:
# ansible all -m file -a 'path=/usr/local/bin/ziplink src=/usr/bin/zip state=link' -k
total 0
lrwxrwxrwx 1 root root 12 Aug 15 08:52 ziplink -> /usr/bin/zip
6、ping:测试指定主机网络是否通讯:
# ansible test -m ping
SSH password:
10.81.235.200 | SUCCESS => {
"changed": false,
"ping": "pong"
}
service
7、service:指定服务状态模块管理(必须保证远程主机事先安装好指定服务)
-
enabled:是否开机自动启动 true/false
-
name:服务名称
-
start:状态,取值为startd,stopd,restartd
# ansible all -m service -a 'enabled=true name=httpd state=started' -k
SSH password:
10.81.235.195 | SUCCESS => {
"changed": false,
"enabled": true,
"name": "httpd",
"state": "started"
}
shell模块
8、shell模块: 在远程主机上运行命令,支持管道、变量等在使用复制命令时使用。
# ansible all -m shell -a '/sbin/ifconfig |grep "HWaddr" | awk "{print $5}"' -k
10.81.235.200 | SUCCESS | rc=0 >>
eth0 Link encap:Ethernet HWaddr 08:00:27:1F:F7:67
yum
9、yum:程序安装
name:指定要安装的程序包,可以带上版本号 state:preset,laster表示安装最新,absent卸载
# ansible all -m yum -a 'name=wget state=present' -k
10.81.235.200 | SUCCESS => {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"wget-1.11.4-2.el5_4.1.i386 providing wget is already installed"
]
}
setup
10、setup:收集远程主机的facts
# ansible test -m setup
每个被管理节点在接受并运行管理命令之前,会将自己主机相关信息,操作系统版本、ip地址等会报告给ansible主机
用于将状态报告给ansible主机直接调用其变量。
==== doc ====
11、查看模块帮助命令:
# ansible-doc -l ansible-doc -s Modules 查看支持模块
- 点赞
- 收藏
- 关注作者
评论(0)