将脚本的输出重定向到localhost上/ tmp中的文件

时间:2019-07-12 16:55:26

标签: ansible

我需要修改脚本以将输出重定向到/ tmp目录本地的文件中。

Ansible的新手。从离开我工作场所的人那里捡起。尝试各种尝试后失败,请寻求帮助!在阅读了几次文档之后,我下面的代码是一片黑暗。我将不胜感激,谁能告诉我在这里最后几行之后需要什么才能将该脚本的输出保存到本地文件中。

  - name: copy_contents
      register: capture_output
      copy:
          content: "{{ capture_output.stdout }}"
          dest: "/tmp/scn.txt"
       delegate_to: localhost

我尝试将以上代码添加到validate_primary_host之后的以下脚本的末尾,但这不起作用。

---
- name:  check primary host
  tasks:
  hosts: all
  gather_facts: false

  vars:
   account: "{{lookup('cyberarkpassword', AppID='XXXXXX', Query='address=oracle.db;username=XXXXXX', Output='Password,PassProps.UserName')}}"
   ora_pass: "{{account.password}}"
   account2: "{{lookup('cyberarkpassword', AppID='XXXXXX', Query='address=oracle.db_cbuk;username=XXXXXX', Output='Password,PassProps.UserName')}}"
   ora_passcb: "{{account2.password}}"

  roles:
      - validate-primary-host

1 个答案:

答案 0 :(得分:1)

下面任务中的

register 将任务 copy 的结果存储在变量 capture_output 中。目前尚不清楚变量 capture_output 是否已经注册过, capture_output.stdout 中是否存储了任何内容。这可能是问题所在。

- name: copy_contents
  copy:
    content: "{{ capture_output.stdout }}"
    dest: "/tmp/scn.txt"
  register: capture_output
  delegate_to: localhost

测试以下任务。本地主机上的/tmp/scn.txt文件应包含正在运行播放的主机的主机名。

- name: Register hostname
  command: hostname
  register: capture_output

- name: copy_contents
  copy:
    content: "{{ capture_output.stdout }}"
    dest: "/tmp/scn.txt"
  delegate_to: localhost

如果以上任务在多个主机上运行,​​则文件/tmp/scn.txt将被顺序覆盖。为了避免这种情况,请使用主机专用名称

     dest: "/tmp/scn-{{ inventory_hostname }}.txt"

语法错误

问题代码中的缩进是错误的。

  - name: copy_contents
      register: capture_output
      copy:
          content: "{{ capture_output.stdout }}"
          dest: "/tmp/scn.txt"
       delegate_to: localhost

这可能是错误原因

 ERROR! 'register' is not a valid attribute

正确的语法如下

  - name: copy_contents
    register: capture_output
    copy:
      content: "{{ capture_output.stdout }}"
      dest: "/tmp/scn.txt"
    delegate_to: localhost