记录一下ansible常用命令,以实战为主,下面以ntp服务为例,在ubuntu服务器上安装ntp服务,上传ntp配置文件,重启服务。
ansible-doc -l可以列出所有模块,ansible-doc <模块名>,或者 ansible <模块名> --help, 可以列出模块帮助文件。
本次测试用到文件拷贝,安装服务等。
需要sudo权限,在hosts文件里先写好,要测试的主机在axtestubuntu。
[axtestubuntu]
xx.102.170.xx ansible_ssh_user=axing ansible_become_user=root ansible_become=true ansible_become_pass='password'
1、用ping模块检查主机联通性,确定ssh配置是否ok
axing@ax:~/ansible$ ansible axtestubuntu -m ping
xx.102.170.xx | SUCCESS => {
"changed": false,
"ping": "pong"
}
2、安装及查看ntp服务,更新软件包
axing@ax:~/ansible$ ansible axtestubuntu -m apt -a 'name=ntp update_cache=yes'
xx.102.170.xx | CHANGED => {
"cache_update_time": 1554458584,
"cache_updated": true,
"changed": true,
"stderr": "",
"stderr_lines": [],
#略过 ...
"Setting up ntp (1:4.2.8p4+dfsg-3ubuntu5.9) ..."
]
}
从下面输出,可以看到ntp服务已经运行了
axing@ax:~/ansible$ ansible axtestubuntu -m command -a 'systemctl status ntp'
xx.102.170.xx | CHANGED | rc=0 >>
● ntp.service - LSB: Start NTP daemon
Loaded: loaded (/etc/init.d/ntp; bad; vendor preset: enabled)
Active: active (running) since Fri 2019-04-05 11:03:06 BST; 1min 24s ago
Docs: man:systemd-sysv-generator(8)
Process: 2297 ExecStart=/etc/init.d/ntp start (code=exited, status=0/SUCCESS)
Tasks: 2
Memory: 664.0K
CPU: 39ms
CGroup: /system.slice/ntp.service
└─2309 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 111:117
Apr 05 11:03:06 axtest69 ntpd[2309]: Listen normally on 2 lo 127.0.0.1:123
Apr 05 11:03:06 axtest69 ntpd[2309]: Listen normally on 3 ens3 46.102.170.69:123
Apr 05 11:03:06 axtest69 ntpd[2309]: Listen normally on 4 lo [::1]:123
Apr 05 11:03:06 axtest69 ntpd[2309]: Listen normally on 5 ens3 [fe80::5054:ff:fedd:3f75%2]:123
Apr 05 11:03:06 axtest69 ntpd[2309]: Listening on routing socket on fd #22 for interface updates
Apr 05 11:03:07 axtest69 ntpd[2309]: Soliciting pool server 91.189.89.198
Apr 05 11:03:08 axtest69 ntpd[2309]: Soliciting pool server 91.189.94.4
Apr 05 11:03:09 axtest69 ntpd[2309]: Soliciting pool server 91.189.91.157
Apr 05 11:03:10 axtest69 ntpd[2309]: Soliciting pool server 91.189.89.199
Apr 05 11:03:11 axtest69 ntpd[2309]: Soliciting pool server 2001:67c:1560:8003::c8
查看软件包,都是最新的了
axing@ax:~/ansible$ ansible axtestubuntu -m apt -a 'update_cache=yes upgrade=dist'
xx.102.170.xx | SUCCESS => {
"changed": false,
"msg": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nCalculating upgrade...\n0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.\n",
"stderr": "",
"stderr_lines": [],
"stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nCalculating upgrade...\n0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.\n",
"stdout_lines": [
"Reading package lists...",
"Building dependency tree...",
"Reading state information...",
"Calculating upgrade...",
"0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded."
]
}
3、ntp的配置文件是通用的,先从另一台主机上下载配置文件
axing@ax:~/ansible$ ansible xxx.co.uk -m fetch -a 'src=/etc/ntp.conf dest=ntp.conf backup=yes'
xxx.co.uk | CHANGED => {
"changed": true,
"checksum": "2b58d741a39ac5110e2b0236c64512b645dc7521",
"dest": "/home/axing/ansible/ntp.conf/bbsx.co.uk/etc/ntp.conf",
"md5sum": "6c48b2a45573d4734b3d0f0b2c211cf2",
"remote_checksum": "2b58d741a39ac5110e2b0236c64512b645dc7521",
"remote_md5sum": null
}
fetch模块会创建一个目录树,看起来是这样子的
4、上传文件,因为我已经上传过一次,所以提示SUCCESS
axing@ax:~/ansible$ ansible axtestubuntu -m copy -a 'src=/home/axing/ansible/files/ntp/ntp.conf dest=/etc/ntp.conf backup=yes'
xx.102.170.xx | SUCCESS => {
"changed": false,
"checksum": "2b58d741a39ac5110e2b0236c64512b645dc7521",
"dest": "/etc/ntp.conf",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"path": "/etc/ntp.conf",
"size": 2683,
"state": "file",
"uid": 0
}
5、重启ntp服务
axing@ax:~/ansible$ ansible axtestubuntu -m service -a 'name=ntp state=restarted'
xx.102.170.xx | CHANGED => {
"changed": true,
"name": "ntp",
"state": "started",
"status": {
"ActiveEnterTimestamp": "Fri 2019-04-05 11:03:06 BST",
"ActiveEnterTimestampMonotonic": "554217468892",
"ActiveExitTimestamp": "Fri 2019-04-05 11:02:55 BST",
"ActiveExitTimestampMonotonic": "554205580677",
"ActiveState": "active",
"After": "systemd-journald-dev-log.socket network-online.target system.slice systemd-journald.socket remote-fs.target sysinit.target basic.target",
"AllowIsolate": "no",
"AmbientCapabilities": "0",
"AssertResult": "yes",
"AssertTimestamp": "Fri 2019-04-05 11:03:06 BST",
#略过
"WatchdogTimestampMonotonic": "554217468874",
"WatchdogUSec": "0"
}
}
6、安装ntpdate软件包,测试ntp同步状态
axing@ax:~/ansible$ ansible axtestubuntu -m apt -a 'name=ntpdate update_cache=yes'
xx.102.170.xx | CHANGED => {
"cache_update_time": 1554459193,
"cache_updated": true,
"changed": true,
"stderr": "",
"stderr_lines": [],
#略过
"Processing triggers for man-db (2.7.5-1) ...",
"Setting up ntpdate (1:4.2.8p4+dfsg-3ubuntu5.9) ..."
]
}
测试NTP状态
axing@ax:~/ansible$ ansible axtestubuntu -m command -a 'ntpdate -u xx.102.170.xx'
xx.102.170.xx | CHANGED | rc=0 >>
5 Apr 11:14:18 ntpdate[3878]: adjust time server xx.102.170.xx offset -0.001195 sec
axing@ax:~/ansible$ ansible axtestubuntu -m command -a 'ntpq -p'
xx.102.170.xx | CHANGED | rc=0 >>
remote refid st t when poll reach delay offset jitter
==============================================================================
ntp.ubuntu.com .POOL. 16 p - 64 0 0.000 0.000 0.000
*xx.102.170.xx 91.189.94.4 3 u 33 64 7 0.667 -1.099 1.002
+ntp1.wirehive.n 195.66.241.10 2 u 34 64 7 2.481 -1.317 0.942
+139.59.199.215 160.177.117.215 3 u 34 64 7 2.657 0.184 0.788
+loncl2.askitsdo 85.199.214.98 2 u 38 64 3 1.498 -0.472 14.675
-time.shf.uk.as4 192.146.137.13 3 u 103 64 6 4.158 -1.397 1.084
chilipepper.can 193.79.237.14 2 u 34 64 7 1.097 -0.882 0.991
alphyn.canonica 132.246.11.231 2 u 34 64 7 69.899 -7.089 0.940
golem.canonical 145.238.203.14 2 u 37 64 7 1.094 -0.873 1.000
pugot.canonical 193.79.237.14 2 u 37 64 7 1.697 -0.635 0.980
一切正常,收工。
7、命令参数参考
ansible默认后面需要跟主机和选项部分,默认不指定模块时,使用的是command模块:
-a MODULE_ARGS, --args=MODULE_ARGS
模块的参数。
--ask-vault-pass
vault 密码。
-B SECONDS, --background=SECONDS
异步运行时,多长时间超时。
-C, --check
仅测试会改变什么内容,不真正执行;
-D, --diff
当更改文件和模板时,显示这些文件的差异。
-e EXTRA_VARS, --extra-vars=EXTRA_VARS
添加附加变量,比如key=value,yaml,json格式。
-f FORKS, --forks=FORKS
指定定要使用的并行进程数,默认为5个。
-h, --help
显示此帮助信息。
-i INVENTORY, --inventory-file=INVENTORY
指定主机清单文件或逗号分隔的主机,默认为/etc/ansible/hosts。
-l SUBSET, --limit=SUBSET
限制所选主机/组模式,只执行-l 后的主机和组。
--list-hosts
主机列表。
-m MODULE_NAME, --module-name=MODULE_NAME
要执行的模块,默认为command。
-M MODULE_PATH, --module-path=MODULE_PATH
要执行的模块的路径。
--new-vault-password-file=NEW_VAULT_PASSWORD_FILE
新vault密钥文件。
-o, --one-line
压缩输出,摘要输出
--output=OUTPUT_FILE
加密或解密输出文件名 用于标准输出。
-P POLL_INTERVAL, --poll=POLL_INTERVAL
如果使用-B,则设置轮询间隔。
--syntax-check
对playbook进行语法检查,且不执行playbook。
-t TREE, --tree=TREE
将日志内容保存在该目录中,文件名以执行主机名命名。
--vault-password-file=VAULT_PASSWORD_FILE
vault密码文件
-v, --verbose
输出执行的详细信息,使用-vvv获得更多,-vvvv 启用连接调试
--version
显示程序版本号
#特权参数
-s, --sudo
使用sudo (nopasswd)运行操作 , 不推荐使用, 提示使用"become"
-U SUDO_USER, --sudo-user=SUDO_USER
sudo 用户,默认为root, 不推荐使用
-S, --su
使用su运行操作, 不推荐使用
-R SU_USER, --su-user=SU_USER
su 用户,默认为root,不推荐使用
-b, --become
运行操作
--become-method=BECOME_METHOD
权限升级方法使用 ,默认为sudo,有效选择:sudo,su,pbrun,pfexec,runas,doas,dzdo
--become-user=BECOME_USER
使用哪个用户运行,默认为root
--ask-sudo-pass
sudo密码,不推荐使用
--ask-su-pass
su密码,不推荐使用
-K, --ask-become-pass
权限提升密码
#连接参数
-k, --ask-pass
要求用户输入请求连接密码
--private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE
私钥路径,使用这个文件来验证连接
-u REMOTE_USER, --user=REMOTE_USER
连接用户
-c CONNECTION, --connection=CONNECTION
连接类型,默认smart
-T TIMEOUT, --timeout=TIMEOUT
指定默认超时时间,默认是10S
--ssh-common-args=SSH_COMMON_ARGS
指定要传递给sftp / scp / ssh的常见参数 (例如 ProxyCommand)
--sftp-extra-args=SFTP_EXTRA_ARGS
指定要传递给sftp,例如-f -l
--scp-extra-args=SCP_EXTRA_ARGS
指定要传递给scp,例如 -l
--ssh-extra-args=SSH_EXTRA_ARGS
指定要传递给ssh,例如 -R
#示例
ansible all -m ping
ansible xx.xxx.xx.* -m ping
ansible all -m command -a ifconfig
ansible all -m shell -a "ifconfig | grep 'inet addr' "
...
加几条命令行更新所有服务器的命令:
# Upgrade all the Ubuntu servers.
ansible ubuntu -m apt -a "upgrade=yes update_cache=yes" -b
# Upgrade all the Ubuntu servers.
ansible debian -m apt -a "upgrade=yes update_cache=yes" -b
# Upgrade all the CentOS servers.
ansible centos -m yum -a "name=* state=latest" -b
# Upgrade all the Fedora servers.
ansible fedora -m dnf -a "name=* state=latest" -b