ansible仅执行第一个shell命令

时间:2015-09-16 11:07:39

标签: ansible

以下是我的yamal文件,

---

     - hosts: qa-workstations

       tasks:

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

如果我使用下面的命令执行ansible, ansible-playbook test.yml -k

它只执行第一个shell。如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

如果您希望任务执行许多命令,您可以使用with_items循环:

示例:

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

但是如果你有很多命令,你应该使用script模块。 script模块将您的shell脚本复制到远程计算机并执行它。

答案 1 :(得分:3)

你实际上只在这里定义了一个任务。第二个shell行简单地覆盖了第一行。写这个的正确方法是:

---

 - hosts: qa-workstations

   tasks:

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

       - name: Append to /tmp/abc
         shell: echo "asdf" >> /tmp/abc
相关问题