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

111 views
Skip to first unread message

magnifito

unread,
Dec 1, 2020, 4:54:30 AM12/1/20
to Ansible Project
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:
---
- hosts: tv
  become_user: tester
  become: yes
  environment: 
    HOME: /home/tester  
  tasks:
  - name: Home debug
    debug:
      msg: "'{{ lookup('env', 'HOME') }}' is the HOME enviroment variable."
  - name: User debug
    debug:
      msg: "'{{ lookup('env', 'USR') | default('nobody', True) }}' is the USER."
  - name: Start wlan monitor in pm2
    shell: /home/tester/.npm-global/bin/pm2 start /home/tester/ops-tests/bin/wireless_monitor.py --name wireless_monitor --interpreter=python3 -- -i 3 -p /home/tester/wireless.log

Which returns:
data@pc:~/tester$ ansible-playbook pm2_wlan_monitor_start.yml -K
BECOME password: 

PLAY [tv] **********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [10.10.20.254]

TASK [Home debug] **************************************************************
ok: [10.10.20.254] => {
    "msg": "'/home/data' is the HOME enviroment variable."
}

TASK [User debug] **************************************************************
ok: [10.10.20.254] => {
    "msg": "'nobody' is the USER."
}

TASK [Start wlan monitor in pm2] ***********************************************
changed: [10.10.20.254]

PLAY RECAP *********************************************************************
10.10.20.254               : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Pm2 monitor will start process only to stop it with errors. Here is the Pm2 log:
M2        | 2020-11-30T12:21:52: PM2 log: App [wireless_monitor:0] starting in -fork mode-
PM2        | 2020-11-30T12:21:52: PM2 log: App [wireless_monitor:0] online
PM2        | 2020-11-30T12:21:52: PM2 log: App [wireless_monitor:0] exited with code [1] via signal [SIGINT]
PM2        | 2020-11-30T12:21:52: PM2 log: Script /home/tester/ops-tests/bin/wireless_monitor.py had too many unstable restarts (16). Stopped. "errored"

/home/tester/.pm2/logs/wireless-monitor-out.log last 15 lines:
/home/tester/.pm2/logs/wireless-monitor-error.log last 15 lines:
0|wireless |   File "/home/tester/ops-tests/bin/common.py", line 83, in check
0|wireless |     return self.make_result(self.get_value())
0|wireless |   File "/home/tester/ops-tests/bin/wireless_monitor.py", line 47, in get_signal_level
0|wireless |     w_info = get_wireles_info(_customer="get_signal_level")
0|wireless |   File "/home/tester/ops-tests/bin/common.py", line 61, in wrapper
0|wireless |     return_value = func(*args, **kwargs)
0|wireless |   File "/home/tester/ops-tests/bin/wireless_monitor.py", line 21, in get_wireles_info
0|wireless |     output = sp.run(["iwconfig"], stdout=sp.PIPE, stderr=sp.PIPE, universal_newlines=True)
0|wireless |   File "/usr/lib/python3.7/subprocess.py", line 472, in run
0|wireless |     with Popen(*popenargs, **kwargs) as process:
0|wireless |   File "/usr/lib/python3.7/subprocess.py", line 775, in __init__
0|wireless |     restore_signals, start_new_session)
0|wireless |   File "/usr/lib/python3.7/subprocess.py", line 1522, in _execute_child
0|wireless |     raise child_exception_type(errno_num, err_msg, err_filename)
0|wireless | FileNotFoundError: [Errno 2] No such file or directory: 'iwconfig': 'iwconfig'

When I SSH into my remote PC and use the same shell command the script starts in pm2 with no problems. I do not have to use full path to start pm2 which turned out is required to start it via Ansible playbook or ad-hoc command. Currently the process exits without making "wireless.log" file.

Any help or information would be appreciated as I could not find the solution.

Best regards,
Greg

Stefan Hornburg (Racke)

unread,
Dec 1, 2020, 5:22:15 AM12/1/20
to ansible...@googlegroups.com
On 12/1/20 10:54 AM, magnifito wrote:
> 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

> *Here is my playbook:*
> ---
> - hosts: tv
>   become_user: tester
>   become: yes
>   environment: 
>     HOME: /home/tester  
>   tasks:
>   - name: Home debug
>     debug:
>       msg: "'{{ lookup('env', 'HOME') }}' is the HOME enviroment variable."
>   - name: User debug
>     debug:
>       msg: "'{{ lookup('env', 'USR') | default('nobody', True) }}' is the USER."
>   - name: Start wlan monitor in pm2
>     shell: /home/tester/.npm-global/bin/pm2 start /home/tester/ops-tests/bin/wireless_monitor.py --name wireless_monitor
> --interpreter=python3 -- -i 3 -p /home/tester/wireless.log
>
> *Which returns:*
> data@pc:~/tester$ ansible-playbook pm2_wlan_monitor_start.yml -K
> BECOME password: 
>
> PLAY [tv] **********************************************************************
>
> TASK [Gathering Facts] *********************************************************
> ok: [10.10.20.254]
>
> TASK [Home debug] **************************************************************
> ok: [10.10.20.254] => {
>     "msg": "'/home/data' is the HOME enviroment variable."
> }
>
> TASK [User debug] **************************************************************
> ok: [10.10.20.254] => {
>     "msg": "'nobody' is the USER."
> }
>
> TASK [Start wlan monitor in pm2] ***********************************************
> changed: [10.10.20.254]
>
> PLAY RECAP *********************************************************************
> 10.10.20.254               : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
>
> *Pm2 monitor will start process only to stop it with errors. Here is the Pm2 log:*
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/3195fe76-8b36-4fcd-882a-7e9f84a65f8bn%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/3195fe76-8b36-4fcd-882a-7e9f84a65f8bn%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

OpenPGP_signature

magnifito

unread,
Dec 2, 2020, 6:55:18 AM12/2/20
to Ansible Project
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

Stefan Hornburg (Racke)

unread,
Dec 2, 2020, 7:04:55 AM12/2/20
to ansible...@googlegroups.com
On 12/2/20 12:55 PM, magnifito wrote:
> 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
> <https://groups.google.com/d/msgid/ansible-project/3195fe76-8b36-4fcd-882a-7e9f84a65f8bn%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/3195fe76-8b36-4fcd-882a-7e9f84a65f8bn%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/c043eb55-1855-42c9-b9fb-b991c0d48d02n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/c043eb55-1855-42c9-b9fb-b991c0d48d02n%40googlegroups.com?utm_medium=email&utm_source=footer>.
OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages