以下Ansible ad-hoc命令在主机上运行ifconfig
:
ansible all -i "192.168.3.5," -m shell -a "ifconfig" -u root
(注意以下输出中的MAC 00:aa:bb:cc:dd:11
为小写)
192.168.3.5 | CHANGED | rc=0 >>
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.3.5 netmask 255.255.255.0 broadcast 0.0.0.0
inet6 fe80::2ec:acff:fecd:e248 prefixlen 64 scopeid 0x20<link>
ether 00:aa:bb:cc:dd:11 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 16 memory 0xd0900000-d0920000
[当我通过SSH或本地在主机上运行ifconfig
时,输出的格式不同(请注意,MAC 00:AA:BB:CC:DD:11
均为大写)
eth0 Link encap:Ethernet HWaddr 00:AA:BB:CC:DD:11
inet addr:192.168.3.5 Bcast:0.0.0.0 Mask:255.255.255.0
inet6 addr: fe80::2ec:acff:fecd:e248/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1250659 errors:0 dropped:0 overruns:0 frame:0
TX packets:572911 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1050302730 (1001.6 MiB) TX bytes:88810961 (84.6 MiB)
Interrupt:16 Memory:d0900000-d0920000
根本原因是什么? Ansible有没有办法以SSH /本地格式显示输出?
ansible 2.9.7
配置文件=无
python版本= 3.7.4
目标主机192.168.3.5:Alpine Linux 3.10
问题运行which ifconfig
确定每个命令的路径后,我发现Ansible使用/bin/ifconfig
,而SSH使用/sbin/ifconfig
解决方案使用. /etc/profile
为每个Ansible命令获取配置文件。例如:
ansible all -i "192.168.3.5," -m shell -a ". /etc/profile && ifconfig" -u root
请参阅此相关答案以获取更多解决方案:link
感谢Zeitounator的提示