
この質問のタイトルに苦労していますので、より意味のあるものになるように自由に編集してください。
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
が、プレイブックを 2 つ持つことは望ましくありません。) したがって、次のように更新します。
- 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
プレイブックの実行中、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.組み込みファイル命令の代わりに
最後のタスクとして一時ファイルを削除します。
- 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 }}"