Ansible 콘솔 출력에서 ​​'item' 값을 어떻게 단축할 수 있습니까?

Ansible 콘솔 출력에서 ​​'item' 값을 어떻게 단축할 수 있습니까?

이 질문의 제목을 정하는 데 어려움을 겪고 있으므로 더 의미있게 편집할 수 있습니다.

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_taskswith 와 결합하여 사용할 수 있다는 것을 알고 있지만 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

플레이북이 실행 중일 때 Ansible 콘솔은 각 반복에서 "항목"에 대한 전체 값을 표시합니다. 이는 출력을 시끄럽게 만들고 사람이 시각적으로 구문 분석하기 어렵게 만듭니다. 예를 들어:

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

노트:

- 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 }}"

관련 정보