Ansible批量在远程主机执行命令

Ansible直接执行远程命令,不用ssh登录交互执行。shell

以下:网络

ansible all -i 192.168.199.180, -m shell -a "ifconfig" -u supermap

参数解释:ssh

  • -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=SuperMap
192.168.199.180 ansible_ssh_port=22 ansible_ssh_host=192.168.199.180 ansible_ssh_user=supermap ansible_ssh_pass=SuperMap
192.168.199.249 ansible_ssh_port=22 ansible_ssh_host=192.168.199.249 ansible_ssh_user=openswitch ansible_ssh_pass=SuperMap

使用主机文件,自动登陆后,批量执行命令:.net

ansible all -i host-list -m shell -a "ifconfig" -u my-username

这个仍是很方便的。code

参考:blog

相关文章
相关标签/搜索