How to run a localhost python script in a play defined for a different host group?

62 views
Skip to first unread message

Ankit Vashistha

unread,
Dec 15, 2018, 7:18:49 AM12/15/18
to Ansible Project
I have got the following playbook:

---
- hosts: Windows

  tasks
:
   
- name: Ensure Service status is running and startup mode set to Automatic
      win_service
:
        name
: MSSQLSERVER
        state
: started
        start_mode
: auto
...

I want to run a Python script on the localhost i.e., the machine which runs Ansible which sends Notification alert to MS Team chat window. How can i modify the above playbook in such a way that once the task is completed, the python script present in /home/myscript/msnotification.py in localhost is executed.

Regards,
Ankit

Kai Stian Olstad

unread,
Dec 15, 2018, 7:23:46 AM12/15/18
to ansible...@googlegroups.com
On Saturday, 15 December 2018 13:18:49 CET Ankit Vashistha wrote:
> I have got the following playbook:
>
> ---
> - hosts: Windows
>
> tasks:
> - name: Ensure Service status is running and startup mode set to
> Automatic
> win_service:
> name: MSSQLSERVER
> state: started
> start_mode: auto
> ...
>
> I want to run a Python script on the *localhost* i.e., the machine which
> runs Ansible which sends Notification alert to MS Team chat window. How can
> i modify the above playbook in such a way that once the task is completed,
> the python script present in */home/myscript/msnotification.py *in
> *localhost* is executed.

Add delegate_to: localhost on the task(s) you need to run on localhost and not the remote host.

--
Kai Stian Olstad


Ankit Vashistha

unread,
Dec 15, 2018, 7:34:38 AM12/15/18
to ansible...@googlegroups.com
Thanks a lot Kai for the quick help. I will try this.

Thanks and Regards,
Ankit


--
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.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2744585.4kni9uFpsz%40x1.
For more options, visit https://groups.google.com/d/optout.

Ankit Vashistha

unread,
Dec 15, 2018, 7:37:40 AM12/15/18
to ansible...@googlegroups.com
I have one more query though. What is the exact different between local_action and delegate_to ?
Thanks and Regards,

Ankit Kumar Sharma
Mobile +91-9717144596


On Sat, Dec 15, 2018 at 5:53 PM Kai Stian Olstad <ansible-pr...@olstad.com> wrote:

Kai Stian Olstad

unread,
Dec 15, 2018, 8:19:13 AM12/15/18
to ansible...@googlegroups.com
On Saturday, 15 December 2018 13:37:17 CET Ankit Vashistha wrote:
> I have one more query though. What is the exact different between
> local_action* and delegate_to ?*

Nothing.

I prefer using delegate_to since it's then easy to add or remove the delegation.
If using local_action more change need to be done to add or remove it.



--
Kai Stian Olstad


Ankit Vashistha

unread,
Dec 15, 2018, 9:34:18 AM12/15/18
to ansible...@googlegroups.com
I tried the following modified playbook:

- hosts: Windows
  gather_facts: false
  tasks:
    - name: Ensure Service status is running and startup mode set to Automatic
      win_service:
        name: MSSQLSERVER
        state: started
        start_mode: auto
   
    - name: Send Job completion notification to MS Teams
      script: /tmp/ansible_scripts/MSTeamsNotifications.pyc
      args:
        executable: /usr/bin/python
      delegate_to: 127.0.0.1
...

but getting the following error:
TASK [Send Job completion notification to MS Teams] ****************************
19:57:29
9
fatal: [WinTest4143 -> 127.0.0.1]: FAILED! => {"changed": false, "msg": "Could not find or access '/tmp/ansible_scripts/MSTeamsNotifications.pyc' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}

this file exists on Ansible Server and works fine. The file has full permissions too but it doesn't work.

$ ls -lrt /tmp/ansible_scripts/MSTeamsNotifications.pyc
-rwxrwxrwx. 1 anks users 1043 Dec 15 07:21 /tmp/ansible_scripts/MSTeamsNotifications.pyc

$ /usr/bin/python /tmp/ansible_scripts/MSTeamsNotifications.pyc
MSTeamsNotifications.py - Send Notifications to MS Teams Team
1

Thanks and Regards,
Ankit

--
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.
To post to this group, send email to ansible...@googlegroups.com.

Kai Stian Olstad

unread,
Dec 15, 2018, 10:16:59 AM12/15/18
to ansible...@googlegroups.com
On Saturday, 15 December 2018 15:33:57 CET Ankit Vashistha wrote:
> I tried the following modified playbook:
>
> *- hosts: Windows*
> * gather_facts: false*
> * tasks:*
> * - name: Ensure Service status is running and startup mode set to
> Automatic*
> * win_service:*
> * name: MSSQLSERVER*
> * state: started*
> * start_mode: auto*
>
>
> * - name: Send Job completion notification to MS Teams*
> * script: /tmp/ansible_scripts/MSTeamsNotifications.pyc*
> * args:*
> * executable: /usr/bin/python*
> * delegate_to: 127.0.0.1*

Recommend using localhost and not 127.0.0.1, localhost is special in Ansible and does the right ting.

The script module copies the file to the remote host and execute the script, so in this case you should use the command module instead and just run the script/program since it's already on the Ansible controller.


--
Kai Stian Olstad


Ankit Vashistha

unread,
Dec 15, 2018, 12:19:20 PM12/15/18
to ansible...@googlegroups.com
Hello Kai,

Unfortunately, I am still getting the error. The current Playbook:

- hosts: Windows
  gather_facts: false
  tasks:
    - name: Ensure Service status is running and startup mode set to Automatic
      win_service:
        name: MSSQLSERVER
        state: started
        start_mode: auto

    - name: Send Job completion notification to MS Teams
      command: '/usr/bin/python /tmp/ansiblescripts/MSTeamsNotifications.pyc'
      delegate_to: localhost


Result:
TASK [Send Job completion notification to MS Teams] ****************************
task path: /var/lib/awx/projects/_6__innersource_cio_config_mgmt/playbooks/mssql/SQLE_Service_Status.yml:15
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: awx
<localhost> EXEC /bin/sh -c 'echo ~awx && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /var/lib/awx/.ansible/tmp/ansible-tmp-1544892619.83-135217386386436 `" && echo ansible-tmp-1544892619.83-135217386386436="` echo /var/lib/awx/.ansible/tmp/ansible-tmp-1544892619.83-135217386386436 `" ) && sleep 0'
Using module file /var/lib/awx/venv/custom-venv/lib/python2.7/site-packages/ansible/modules/commands/command.py
<localhost> PUT /var/lib/awx/.ansible/tmp/ansible-local-3LdqDbo/tmpLJ0CAV TO /var/lib/awx/.ansible/tmp/ansible-tmp-1544892619.83-135217386386436/AnsiballZ_command.py
<localhost> EXEC /bin/sh -c 'chmod u+x /var/lib/awx/.ansible/tmp/ansible-tmp-1544892619.83-135217386386436/ /var/lib/awx/.ansible/tmp/ansible-tmp-1544892619.83-135217386386436/AnsiballZ_command.py && sleep 0'
<localhost> EXEC /bin/sh -c '/var/lib/awx/venv/custom-venv/bin/python /var/lib/awx/.ansible/tmp/ansible-tmp-1544892619.83-135217386386436/AnsiballZ_command.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /var/lib/awx/.ansible/tmp/ansible-tmp-1544892619.83-135217386386436/ > /dev/null 2>&1 && sleep 0'
fatal: [WinTest4143-> localhost]: FAILED! => {
    "changed": true, 
    "cmd": [
        "/usr/bin/python", 
        "/tmp/ansiblescripts/MSTeamsNotifications.pyc"
    ], 
    "delta": "0:00:00.022198", 
    "end": "2018-12-15 16:50:20.371504", 
    "invocation": {
        "module_args": {
            "_raw_params": "/usr/bin/python /tmp/ansiblescripts/MSTeamsNotifications.pyc", 
            "_uses_shell": false, 
            "argv": null, 
            "chdir": null, 
            "creates": null, 
            "executable": null, 
            "removes": null, 
            "stdin": null, 
            "warn": true
        }
    }, 
    "msg": "non-zero return code", 
    "rc": 2, 
    "start": "2018-12-15 16:50:20.349306", 
    "stderr": "/usr/bin/python: can't open file '/tmp/ansiblescripts/MSTeamsNotifications.pyc': [Errno 2] No such file or directory", 
    "stderr_lines": [
        "/usr/bin/python: can't open file '/tmp/ansiblescripts/MSTeamsNotifications.pyc': [Errno 2] No such file or directory"
    ], 
    "stdout": "", 
    "stdout_lines": []
}

Another thing i noticed is, when i try to sudo to awx user, i get a expired password error. What is the default password for awx user. I don't remember setting it up anywhere during installation of Tower.

# sudo su - awx
sudo: Account or password is expired, reset your password and try again
Changing password for root.
(current) UNIX password:

Thanks and Regards,
Ankit

--
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.
To post to this group, send email to ansible...@googlegroups.com.

Jordan Borean

unread,
Dec 15, 2018, 3:00:47 PM12/15/18
to Ansible Project
If you are seeing this issue in AWX you are probably coming across bubble wrap https://docs.ansible.com/ansible-tower/3.1.4/html/administration/proot_func_variables.html. Bubblewrap limits what directories a playbook can run and IIRC it’s default is the project directory of the playbook itself. I recommend keeping that script as part of the playbook got repo in the files directory and referencing it by name.
Reply all
Reply to author
Forward
0 new messages