8

My playbook is as below:

- hosts : mygroup
  user : user
  sudo : yes
  tasks :
  - name : Copy script
    copy : 'src=/home/user/Scripts/logchecker.py dest=/opt/root2/logchecker.py owner=root group=root mode=755'
  - name : Execute script
    command : '/usr/bin/python /opt/root2/logchecker.py'

The file upload is working, But execution is failing. Even though I am able to execute the script without any issues directly on the server. Am I doing anything wrong?

030
  • 5,901
  • 13
  • 68
  • 110
nitins
  • 2,579
  • 15
  • 44
  • 68
  • Can you edit your question to include the full command run and its output? – EEAA Sep 06 '13 at 20:50
  • you can make a custom module fairly easily. – Skylar Saveland Oct 28 '13 at 21:03
  • I suspect that the `mode=755` was the problem here, as the value is interpreted as decimal not octal. Using `mode="0755"` would probably work. The [file module documentation](http://docs.ansible.com/ansible/latest/file_module.html) covers this but should really have a warning with highlighted text. – RichVel Oct 21 '17 at 06:13

2 Answers2

6

I have used a similar playbook which works as expected:

# playbook.yml
---
- hosts: ${target}
  sudo: yes

  tasks:
  - name: Copy file
    copy: src=../files/test.py dest=/opt/test.py owner=howardsandford group=admin mode=755

  - name: Execute script
    command: /opt/test.py

And test.py:

#!/usr/bin/python

# write to a file
f = open('/tmp/test_from_python','w')
f.write('hi there\n')

Running the playboook:

ansible-playbook playbook.yml --extra-vars "target=the_host_to_run_script_on"

Shows:

PLAY [the_host_to_run_script_on] ***************************************************************

GATHERING FACTS ***************************************************************
ok: [the_host_to_run_script_on]

TASK: [Copy file] *************************************************************
changed: [the_host_to_run_script_on]

TASK: [Execute script] ********************************************************
changed: [the_host_to_run_script_on]

PLAY RECAP ********************************************************************
the_host_to_run_script_on  : ok=3    changed=2    unreachable=0    failed=0

And on the remote host:

$ cat /tmp/test_from_python
hi there

Several differences between our setup:

  • I don't have single quotes around the copy and command parameters
  • The shebang sets the python interpreter rather than specifying /usr/bin/python from the command line
  • I set the owner of the script to my own username and primary group that is in sudoers, rather than root

Hopefully this can point you in the right direction of where the differences might be.

  • In ansible 1.9.4 - hosts: ${target} doesn't work in playbook, i have to change it with - hosts: '{{target}}'. its mentioned in documentation. http://docs.ansible.com/ansible/playbooks_variables.html – Adeel Ahmad Nov 30 '15 at 13:07
2

You need only Below plugin script to use

---
- hosts: ${target}
  become: true
  tasks:
  - name: Copy and Execute the script
    script: /opt/test.py
mudrii
  • 121
  • 1