Ansible直接执行远程命令,不用ssh登陆交互执行。
如下:
ansible all -i 192.168.199.180, -m shell -a "ifconfig" -u supermap
参数解释:
- -i 连接到远程主机“192.168.199.180,”,注意这里的“,”直接指定主机名列表,没有“,”该参数则被解释为主机名的列表文件。
- -m 为ansible模块名称,执行shell命令。
- -a 远程shell命令,为ifconfig(获取网络配置参数)。
- -u 指定执行的远程主机用户名。
- -k 可以指定是否交互输入参数,如果没有,则需要在本机有预存的访问密钥。
- 可以先使用ssh登录,选择保存,即可自动保存下来。
主机文件格式,样例参考( host-list):
[local]192.168.199.173 ansible_ssh_port=22 ansible_ssh_host=192.168.199.173 ansible_ssh_user=supermap ansible_ssh_pass=SuperMap192.168.199.180 ansible_ssh_port=22 ansible_ssh_host=192.168.199.180 ansible_ssh_user=supermap ansible_ssh_pass=SuperMap192.168.199.249 ansible_ssh_port=22 ansible_ssh_host=192.168.199.249 ansible_ssh_user=openswitch ansible_ssh_pass=SuperMap
使用主机文件,自动登录后,批量执行命令:
ansible all -i host-list -m shell -a "ifconfig" -u my-username
这个还是很方便的。
参考: