
我正在努力解決這個問題的標題,所以請隨意編輯它以使其更有意義。
假設您在 Ansible 中有一個任務並且有register
輸出。例如:
- name: Set up working directory
shell: mktemp -d
register: workdir
我想使用註冊的輸出來完成另一項任務。例如:
- name: Create a file
with_items: "{{ workdir.stdout }}"
shell: touch {{ item }}/test-file
這一切都很好。現在,我想循環這兩個任務 N 次。 (我知道我可以將其提取到一個單獨的 yaml 檔案中,並include_tasks
與 結合使用loop
,但我不想有兩個劇本。)因此,我更新為:
- name: Set up working directory
shell: mktemp -d
register: workdir
loop:
- 1
- 2
由於循環的結果,workdir
變數採用了不同的結構,因此現在對其進行迭代略有不同。例如:
- name: Create a file
with_items: "{{ workdir.results }}"
shell: touch {{ item.stdout }}/test-file
當 playbook 運作時,Ansible 控制台會在每次迭代中顯示「item」的完整值。這使得輸出變得嘈雜並且人類難以視覺解析。例如:
TASK [Create a file] ***************************************************
changed: [localhost] => (item={'cmd': 'mktemp -d', 'stdout': '/tmp/tmp.a
ncF0iBqzP', 'stderr': '', 'rc': 0, 'start': '2023-05-26 15:22:46.458962'
, 'end': '2023-05-26 15:22:46.465557', 'delta': '0:00:00.006595', 'chang
ed': True, 'invocation': {'module_args': {'_raw_params': 'mktemp -d', '_
uses_shell': True, 'warn': True, 'stdin_add_newline': True, 'strip_empty
_ends': True, 'argv': None, 'chdir': None, 'executable': None, 'creates'
: None, 'removes': None, 'stdin': None}}, 'stdout_lines': ['/tmp/tmp.anc
F0iBqzP'], 'stderr_lines': [], 'failed': False, 'item': 1, 'ansible_loop
_var': 'item'})
changed: [localhost] => (item={'cmd': 'mktemp -d', 'stdout': '/tmp/tmp.v
Tpkvx6RK0', 'stderr': '', 'rc': 0, 'start': '2023-05-26 15:22:46.727879'
, 'end': '2023-05-26 15:22:46.734876', 'delta': '0:00:00.006997', 'chang
ed': True, 'invocation': {'module_args': {'_raw_params': 'mktemp -d', '_
uses_shell': True, 'warn': True, 'stdin_add_newline': True, 'strip_empty
_ends': True, 'argv': None, 'chdir': None, 'executable': None, 'creates'
: None, 'removes': None, 'stdin': None}}, 'stdout_lines': ['/tmp/tmp.vTp
kvx6RK0'], 'stderr_lines': [], 'failed': False, 'item': 2, 'ansible_loop
_var': 'item'})
這是我的問題:
有沒有辦法
- 縮寫 Ansible 控制台輸出中「item」的輸出?
- 更改循環,使其以不同的方式迭代,例如:
with_items: "{{ workdir.results.*.stdout }}"
shell: touch {{ item }}/test-file
- 一些可以修改項目的過濾器?
- 還有其他聰明的解決方案嗎?
答案1
還有更多選擇:
- 使用循環控制。看用標籤限制循環輸出。例如,
shell> cat pb.yml
- hosts: localhost
tasks:
- command: mktemp -d
register: workdir
with_sequence: end=2
- command: "touch {{ item.stdout }}/test-file"
loop: "{{ workdir.results }}"
loop_control:
label: "{{ item.stdout }}"
給出
shell> ansible_playbook pb.yml
PLAY [localhost] *****************************************************************************
TASK [command] *******************************************************************************
changed: [localhost] => (item=1)
changed: [localhost] => (item=2)
TASK [command] *******************************************************************************
changed: [localhost] => (item=/tmp/tmp.2pOVwfrYLI)
changed: [localhost] => (item=/tmp/tmp.FyF0JCghAo)
PLAY RECAP ***********************************************************************************
localhost: ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- (可選)先建立目錄列表
shell> cat pb.yml
- hosts: localhost
vars:
workdirs: "{{ workdir.results|map(attribute='stdout') }}"
tasks:
- command: mktemp -d
register: workdir
with_sequence: end=2
- command: "touch {{ item }}/test-file"
loop: "{{ workdirs }}"
給出相同的
shell> ansible-playbook pb.yml
PLAY [localhost] *****************************************************************************
TASK [command] *******************************************************************************
changed: [localhost] => (item=1)
changed: [localhost] => (item=2)
TASK [command] *******************************************************************************
changed: [localhost] => (item=/tmp/tmp.OOHLqAfFlX)
changed: [localhost] => (item=/tmp/tmp.T5lF2ZruwZ)
PLAY RECAP ***********************************************************************************
localhost: ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
筆記:
使用模組ansible.builtin.tempfile代替命令
使用模組ansible.builtin.file而不是命令
刪除臨時檔案作為最後一個任務。
- hosts: localhost
vars:
workdirs: "{{ workdir.results|map(attribute='path') }}"
tasks:
- tempfile:
state: directory
register: workdir
with_sequence: end=2
- file:
state: touch
path: "{{ item }}/test-file"
loop: "{{ workdirs }}"
- file:
state: absent
path: "{{ item }}"
loop: "{{ workdirs }}"