Naggappan Ramukannan
Naggappan Ramukannan

Reputation: 2822

ansible only first shell command is executed

Following is my yamal file,

---

     - hosts: qa-workstations

       tasks:

           - name: update java version
             shell: echo "asdfasdf" > /tmp/abc
             shell: echo "asdf" >> /tmp/abc

If I execute ansible using below command, ansible-playbook test.yml -k

It executes only 1st shell. How to solve this issue?

Upvotes: 0

Views: 380

Answers (2)

Bruce P
Bruce P

Reputation: 20769

You've actually only defined a single task here. The second shell line simply overrides the first. The proper way to write this is:

---

 - hosts: qa-workstations

   tasks:

       - name: create /tmp/abc
         shell: echo "asdfasdf" > /tmp/abc

       - name: Append to /tmp/abc
         shell: echo "asdf" >> /tmp/abc

Upvotes: 3

nghnam
nghnam

Reputation: 671

If you want a task execute many commands, you can use with_items loop:

Example:

tasks:
  - name: test
    shell: "{{ item }}"
    with_items:
      - echo Ansible
      - df -h

But if you have many commands, you should use script module. script module copies your shell script to remote machine and executes it.

Upvotes: 3

Related Questions