0

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?

Environment

ansible 2.9.7

config file = None

python version = 3.7.4

Target Host 192.168.3.5: Alpine Linux 3.10

  • 2
    Neither in FreeBSD nor in Ubuntu remote hosts I'm able to reproduce the problem. The MAC address looks suspicious. – Vladimir Botka May 12 '20 at 08:16
  • Check which alias/binary you are using in either case with `alias ifconfig` and `which ifconfig`, both in ad-hoc and direct ssh connection. There might be a difference which could explain what you experience. – Zeitounator May 12 '20 at 09:35
  • @VladimirBotka I edited the MAC which is why it looks suspicious. Thank you for trying to reproduce though. Zeitounator seems to have uncovered the underlying problem. – Hyper Light May 12 '20 at 16:28
  • @Zeitounator Thank you for the suggestion! That was the problem. Ansible is using `/bin/ifconfig` while SSH and local use `/sbin/ifconfig`. Is there anyway to have Ansible use `/sbin/ifconfig`? – Hyper Light May 12 '20 at 16:30
  • Set your path in ansible, or use the full path in your command. – Zeitounator May 12 '20 at 18:32

1 Answers1

0

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