Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19k points)

I want to check on all my hosts and this command is perfect for it

ansible -m shell -a "ps -eo pcpu,user,args | sort -r -k1 | head -n5"

But the problem is I have to execute it in multiple places and that gets annoying. Can I use this shell command in Ansible playbook? 

1 Answer

0 votes
by (33.1k points)

The local_action runs the command on the local server, not on the servers you specify in the host parameter.

You can change your "Execute the script" task to

- name: Execute the script

  command: sh /home/test_user/

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

remote_user: test_user

So, the playbook will become:


- name: Transfer and execute a script.

  hosts: server

  remote_user: test_user

  sudo: yes


     - name: Transfer the script

       copy: dest=/home/test_user mode=0777

     - name: Execute the script

       command: sh /home/test_user/

Hope this answer helps you!

Browse Categories