Question:
I am planning to execute a shell script on a remote server using Ansible playbook.
blank test.sh file:
1 2 |
touch test.sh |
Playbook:
1 2 3 4 5 6 7 8 9 10 11 12 |
--- - name: Transfer and execute a script. hosts: server user: test_user sudo: yes tasks: - name: Transfer the script copy: src=test.sh dest=/home/test_user mode=0777 - name: Execute the script local_action: command sudo sh /home/test_user/test.sh |
When I run the playbook, the transfer successfully occurs but the script is not executed.
Answer:
local_action
runs the command on the local server, not on the servers you specify in hosts
parameter.
Change your “Execute the script” task to
1 2 3 |
- name: Execute the script command: sh /home/test_user/test.sh |
and it should do it.
You don’t need to repeat sudo in the command line because you have defined it already in the playbook.
According to Ansible Intro to Playbooks user
parameter was renamed to remote_user
in Ansible 1.4 so you should change it, too
1 2 |
remote_user: test_user |
So, the playbook will become:
1 2 3 4 5 6 7 8 9 10 11 12 |
--- - name: Transfer and execute a script. hosts: server remote_user: test_user sudo: yes tasks: - name: Transfer the script copy: src=test.sh dest=/home/test_user mode=0777 - name: Execute the script command: sh /home/test_user/test.sh |