我需要将所有可见任务的输出存储到Ansible控制节点上的某个文件(在下面的示例中为output.log)中。。我试着用这种方式实现它。
- name: Playbook to store the output of two tasks to the same file
hosts: test
tasks:
- name: task_1
shell: command_1
register: output_1
- name: save output_1 using copy module
copy:
content: {{ output_1.stdout_lines }}
dest: ./output.log
delegate_to: localhost
- name: task_2
shell: command_2
register: output_2
- name: save output_2 using copy module
copy:
content: {{ output_2.stdout_lines }}
dest: ./output.log
delegate_to: localhost
问题是,当剧本完成执行时,output.log文件Has只有最后一个任务的输出。我知道每次我使用复制模块时,它都在替换旧的内容。
有没有办法做到这一点?
声明注册变量的模式
out_pattern: '^out_d+$'
执行任务并注册输出
- command: echo command_1
register: out_1
- command: echo command_2
register: out_2
获取所有注册变量的值并映射属性stdout_lines
- set_fact:
log: "{{ lookup('vars', *q('varnames', out_pattern))|
map(attribute='stdout_lines')|list }}"
为
log:
- [command_1]
- [command_2]
创建所有主机和变量的字典log
log_all: "{{ dict(ansible_play_hosts|
zip(ansible_play_hosts|
map('extract', hostvars, 'log'))) }}"
并将其写入控制器
上的文件中。- copy:
dest: /tmp/out.log
content: "{{ log_all|to_yaml }}"
run_once: true
delegate_to: localhost
完整剧本示例
shell> cat pb.yml
- hosts: test
vars:
out_pattern: '^out_d+$'
log_all: "{{ dict(ansible_play_hosts|
zip(ansible_play_hosts|
map('extract', hostvars, 'log'))) }}"
tasks:
- command: echo command_1
register: out_1
- command: echo command_2
register: out_2
- set_fact:
log: "{{ lookup('vars', *q('varnames', out_pattern))|
map(attribute='stdout_lines')|list }}"
- debug:
var: log|to_yaml
- block:
- debug:
var: log_all|to_yaml
- copy:
dest: /tmp/out.log
content: "{{ log_all|to_yaml }}"
run_once: true
delegate_to: localhost
为
shell> ansible-playbook pb.yml -l test_11,test_13
PLAY [test] ***********************************************************************************
TASK [command] ********************************************************************************
changed: [test_13]
changed: [test_11]
TASK [command] ********************************************************************************
changed: [test_11]
changed: [test_13]
TASK [set_fact] *******************************************************************************
ok: [test_11]
ok: [test_13]
TASK [debug] **********************************************************************************
ok: [test_13] =>
log|to_yaml: |-
- [command_1]
- [command_2]
ok: [test_11] =>
log|to_yaml: |-
- [command_1]
- [command_2]
TASK [debug] **********************************************************************************
ok: [test_11 -> localhost] =>
log_all|to_yaml: |-
test_11:
- [command_1]
- [command_2]
test_13:
- [command_1]
- [command_2]
TASK [copy] ***********************************************************************************
ok: [test_11 -> localhost]
PLAY RECAP ************************************************************************************
test_11: ok=6 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test_13: ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
日志文件
shell> cat /tmp/out.log
test_11:
- [command_1]
- [command_2]
test_13:
- [command_1]
- [command_2]