Ansible newby trying to do something very simple (unsuccessfully)

346 views
Skip to first unread message

Jane Mortimore

unread,
Jul 10, 2015, 9:33:55 AM7/10/15
to ansible...@googlegroups.com, jane.mo...@usfoods.com
I'm am trying to write an ansible script that will generate thread dumps on java processes.  Here is my playbook:

---

 - name: generate thread dumps
   hosts: localhost
   user: oracle
   serial: 100%
   vars:
     pid: 0
     serverNames:
         - AdminServer

   tasks:
     - command: "kill -3 `pgrep -f {item}`"
       with_items: serverNames



This is the ansible result:

$ ansible-playbook thread-dump.yml -i inventory -v

PLAY [generate thread dumps] **************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [command kill -3 `pgrep -f {item}`] *************************************
failed: [localhost] => (item=AdminServer) => {"changed": true, "cmd": ["kill", "-3", "`pgrep", "-f", "{item}`"], "delta": "0:00:00.005389", "end": "2015-07-09 17:11:38.275632", "item": "AdminServer", "rc": 3, "start": "2015-07-09 17:11:38.270243", "warnings": []}
stderr: kill: can't find process "`pgrep"
kill: can't find process "-f"
kill: can't find process "{item}`"

FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
           to retry, use: --limit @/home/oracle/thread-dump.retry

localhost                  : ok=1    changed=0    unreachable=0    failed=1



The command works fine when I run it at the command prompt.

I have also tried creating a script and executing it with the script command.  Here is the kill3.sh script:

kill -3 `pgrep -f $1`

Which also fails:

]$ ansible-playbook thread-dump.yml -i inventory -v

PLAY [generate thread dumps] **************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [script kill3.sh {item}] ************************************************
failed: [localhost] => (item=AdminServer) => {"changed": true, "item": "AdminServer", "rc": 1}
stderr: OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /home/oracle/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: auto-mux: Trying existing master
debug1: mux_client_request_session: master session id: 2
debug1: mux_client_request_session: master session id: 2
Shared connection to localhost closed.

stdout: Invalid preceding regular expressionkill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]


FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
           to retry, use: --limit @/home/oracle/thread-dump.retry

localhost                  : ok=1    changed=0    unreachable=0    failed=1


Note the script works fine when I run it from the command prompt.

What am I doing wrong?

Thanks in advance for any assistance.

Jane




Brian Coca

unread,
Jul 10, 2015, 8:36:19 PM7/10/15
to ansible...@googlegroups.com, jane.mo...@usfoods.com
- name: generate thread dumps
hosts: localhost
user: oracle
# serial: 100% <= this is not needed as that is the default
vars:
pid: 0
serverNames:
- AdminServer

tasks:
- command: "kill -3 `pgrep -f {{item}}`" # <= note that the
braces are double, not single for variable interpolation
with_items: serverNames
register: myoutput # <= using below to display results

- debug: var=myoutput


--
Brian Coca

Brian Coca

unread,
Jul 10, 2015, 9:51:10 PM7/10/15
to Mortimore, Jane E, ansible...@googlegroups.com
ah, missed the backtics, try using shell: instead of command:



--
Brian Coca
Reply all
Reply to author
Forward
0 new messages