Reputation: 193
I want to run shell content when action variable is "start".
My playbook is:
vars:
action: "{{ action }}"
tasks:
- set_fact:
env_param: "{{ deployment_environment }}"
- name: Start Filebeat
shell: "sh {{ env_select[env_param].deployment_path }}/filebeat/filebeat-{{ filebeat_version }}-linux-x86_64/run.sh"
when: action == "start"
- name: Stop Filebeat
shell: "ps aux | grep -i filebeat | awk '{print $2}' | xargs kill -9"
when: action == "stop"
My above code is running successfully when running the "start" action, without any errors, but the shell didn't execute. My script executes manually and it has content using the following command:
./filebeat -e >> filebeat-out.log 2>&1 &
What am I missing?
Upvotes: 0
Views: 423
Reputation: 5740
It depends on how you execute. If you provide extra vars with:
ansible-playbook playbook.yml -e action=stop
Then Ansible should execute the task. You should see the task as "Changed" with color orange on the Ansible console.
If filebeat didn't close, see on the system what the output is of:
ps aux | grep -i filebeat | awk '{print $2}'
It could be that the returned value is incorrect.
Also, when following the Ansible mindset, you should write the task as so:
- name: stop filebeat when action is stop
service:
name: filebeat
state: stopped
when: "'stop' in action"
Please don't start/stop services using shell, but rather use the Ansible built-in modules.
Upvotes: 2
Reputation: 1
Your code it should be just fine. try to execute a more simple shell command for troubleshooting porposes. just to validate your entire anisble code. Maybe you can remove also the "sh" before your command as shell ansible module is already using shell.
Upvotes: 0