Problem with Ansible user and home directory on remote computers, pm2 process manager

Hello,
I am an Ansible beginner. I am trying to start Python script in pm2 process manager on my computers.
I think I have narrowed the problem to Ansible not knowing proper home directory.
Become, become_user, changing environment path in config or playbook does not solve the problem.

whoami returns my remote user, with become it returns root, while “echo $HOME” gives me /home/my_pc_user instead /home/remote_user

Here is my playbook:

Hello,
I am an Ansible beginner. I am trying to start Python script in pm2 process manager on my computers.
I think I have narrowed the problem to Ansible not knowing proper home directory.
Become, become_user, changing environment path in config or playbook does not solve the problem.

whoami returns my remote user, with become it returns root, while "echo $HOME" gives me /home/my_pc_user instead
/home/remote_user

1. Lookups always take place on the Ansible controller
2. Use command module instead of shell unless you have a very good reason to not use command
3. FileNotFoundError: [Errno 2] No such file or directory: 'iwconfig': 'iwconfig'
   iwconfig is in /usr/sbin, so that is probably missing in the $PATH Ansible uses (different from a regular ssh shell)

Regards
         Racke

Hello,
Thank your for your reply.
I have looked at my problem again and found out I was using " instead while having a command with variable. Now the problem is clear.

data@pc:~/tester$ ansible tv -a ‘echo $HOME’
10.10.20.254 | CHANGED | rc=0 >>
/home/tester
data@pc:~/tester$ ansible tv -a ‘echo $PATH’
10.10.20.254 | CHANGED | rc=0 >>
/usr/local/bin:/usr/bin:/bin:/usr/games

Ad 1.&3. The path to home was correct all the time, but my command while searching for solution was not. iwconfig is located inside /sbin/ which is not in known paths.
How I can permanently add a path to /sbin/ or other directories unavailable to Ansible?

Ad 2. I was using a “command” at the beginning but switched to a “shell” thinking it was a part of my problem.

Best regards,
Greg

Hello,
Thank your for your reply.
I have looked at my problem again and found out I was using *"* instead *'* while having a command with variable. Now
the problem is clear.

data@pc:~/tester$ ansible tv -a 'echo $HOME'
10.10.20.254 | CHANGED | rc=0 >>
/home/tester
data@pc:~/tester$ ansible tv -a 'echo $PATH'
10.10.20.254 | CHANGED | rc=0 >>
/usr/local/bin:/usr/bin:/bin:/usr/games

Ad 1.&3. The path to home was correct all the time, but my command while searching for solution was not. iwconfig is
located inside /sbin/ which is not in known paths.
How I can permanently add a path to /sbin/ or other directories unavailable to Ansible?

Ad 2. I was using a "command" at the beginning but switched to a "shell" thinking it was a part of my problem.

You can set the PATH variable for the whole playbook or just for one task with:

    environment:
       PATH: /usr/local/bin:/usr/bin:/bin:/usr/games:/sbin

Or alternatively add a symlink to iwconfig in /usr/bin or another directory in $PATH.

Regards
         Racke