特定任务应仅适用于单个服务器

时间:2019-01-15 00:08:50

标签: ansible ansible-2.x

几乎没有一项任性的任务只能运行一次,而不能多次运行

我仍在学习ansible,我写了一本ansible剧本,其中包含诸如echo username之类的任务到日志文件,但是当它在远程主机上运行时,它会回显所有输出。任何帮助表示赞赏。

- name: user
shell: echo "$LOGNAME user"  >> /tmp/audit_list
delegate_to: localhost

- name: remote hosts 
action: shell  echo "remote host is {{ansible_fqdn}}" >> /tmp/audit_list
delegate_to: localhost

/ tmp / audit_list如下所示:

 tommy user
 tommy user
remote host is apache1

由于我在3台服务器上运行上述剧本,因此它将打印用户名3次,但我只想打印一次,然后再打印执行剧本的所有远程主机。

以下是我想要的输出

tommy user
remote host is apache1 
remote host is apache2
remote hoost is apache3

1 个答案:

答案 0 :(得分:0)

您需要 run_once 。参见下面的示例

- hosts: all
  gather_facts: yes
  tasks:
    - name: user
      shell: echo "$LOGNAME user"  >> /tmp/audit_list
      delegate_to: localhost
      run_once: true
    - name: remote hosts 
      shell:  echo "remote host is {{ansible_fqdn}}" >> /tmp/audit_list
      delegate_to: localhost


> cat /tmp/audit_list 
root user
remote host is test_01
remote host is test_02
remote host is test_03