The following Ansible ad-hoc command runs ifconfig
on the host machine:
ansible all -i "192.168.3.5," -m shell -a "ifconfig" -u root
(notice the MAC 00:aa:bb:cc:dd:11
in the following output is lowercase)
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
When I run ifconfig
on the host via SSH or locally, the output is formatted differently (notice the MAC 00:AA:BB:CC:DD:11
is all caps)
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
What's the root cause? Is there a way for Ansible to display the output in the SSH/local format?
ansible 2.9.7
config file = None
python version = 3.7.4
Target Host 192.168.3.5: Alpine Linux 3.10
Problem
After running which ifconfig
to determine the path of each command, I found that Ansible was using /bin/ifconfig
whereas SSH was using /sbin/ifconfig
Solution
Source the profile for each Ansible command with . /etc/profile
. For example:
ansible all -i "192.168.3.5," -m shell -a ". /etc/profile && ifconfig" -u root
See this related answer for more solutions: link
Thanks to Zeitounator for the tip