Ubuntu 下 Ansible 使用记录
环境:
- Ubuntu: 18.04 LTS
- Ansible: 2.5.2
- Python: 2.7.15rc1
安装:
apt-get install ansible 或 pip install ansible
配置:
vi /etc/ansible/hosts
修改一下远程配置,把需要远程的服务器地址填入(可以自己配置一个组)
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
[localserver]
192.168.137.139
192.168.137.140
# Ex 1: Ungrouped hosts, specify before any group headers.
## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10
这里我添加了一个localserver组,加入了本地测试用安装的虚拟机地址(蓝色部分)
保存并退出
实际服务器测试当中,还会出现以下错误
FAILED! => {"failed": true, "msg": "ERROR! Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."}
现在服务器都会开启密钥检查(因为测试环境仅为账号密码登陆所以不考虑公钥验证,后期遇到会添加)
如出现上述错误,则还需修改配置文件
vi /etc/ansible/ansible.cfg
....
# option lets you increase or decrease that
# timeout to something more suitable for the
# environment.
# gather_timeout = 10
# additional paths to search for roles in, colon separated
#roles_path = /etc/ansible/roles
# uncomment this to disable SSH key host checking
#host_key_checking = False
# change the default callback, you can only have one 'stdout' type enabled at a time.
#stdout_callback = skippy
.....
去掉 host_key_checking = False 前面的注释即可(红色部分)
测试:
ansible localserver -m ping -u root -k
SSH password:
192.168.137.140 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.137.139 | SUCCESS => {
"changed": false,
"ping": "pong"
}
localserver 就是刚才配置的组,也可以指定一个服务器,直接键入IP就行
ansible 192.168.137.139 -m ping -u root -k
SSH password:
192.168.137.139 | SUCCESS => {
"changed": false,
"ping": "pong"
}
参数说明:-m 后接命令 -u 用户名 -k 连接前询问密码(因为本地测试没使用公钥认证,进使用账号密码登陆)
基础命令
远程查询以下剩余内存(执行命令)
root@server:~# ansible 192.168.137.139 -m command -a "free -m" -k SSH password: 192.168.137.139 | SUCCESS | rc=0 >> total used free shared buff/cache available Mem: 961 231 210 12 519 569 Swap: 2043 0 2043
复制文件
root@server:~# touch test_file
root@server:~# ansible 192.168.137.139 -m copy -a "src=/root/test_file dest=/root" -k
SSH password:
192.168.137.139 | SUCCESS => {
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/root/test_file",
"gid": 0,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "0644",
"owner": "root",
"size": 0,
"src": "/root/.ansible/tmp/ansible-tmp-1525760764.39-252880811110737/source",
"state": "file",
"uid": 0
}
查询文件信息(状态)
root@server:~# ansible 192.168.137.139 -m stat -a "path=/root/test_file" -k
SSH password:
192.168.137.139 | SUCCESS => {
"changed": false,
"stat": {
"atime": 1525760766.1826777,
"attr_flags": "e",
"attributes": [
"extents"
],
"block_size": 4096,
"blocks": 0,
"charset": "binary",
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"ctime": 1525760766.1826777,
"dev": 64768,
"device_type": 0,
"executable": false,
"exists": true,
"gid": 0,
"gr_name": "root",
"inode": 274007,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mimetype": "inode/x-empty",
"mode": "0644",
"mtime": 1525760765.906087,
"nlink": 1,
"path": "/root/test_file",
"pw_name": "root",
"readable": true,
"rgrp": true,
"roth": true,
"rusr": true,
"size": 0,
"uid": 0,
"version": "3915031093",
"wgrp": false,
"woth": false,
"writeable": true,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
}
远程下载
ansible 192.168.137.139 -m get_url -a "url=https://x-item.com dest=/root/virace.html" -k
远程包管理
ansible 192.168.137.139 -m yum -a "name=python3" -k
未完待续
本作品采用 知识共享署名-非商业性使用 4.0 国际许可协议 进行许可
文章评论
评论测试