如何使ansible assemble仅在我的本地计算机上输出已组装的文件?

huangapple go评论69阅读模式
英文:

How do I make ansible assemble output the assembled file only on my local machine?

问题

  1. 在/tmp目录下创建一个以当前主机名命名的文件。

  2. 从远程主机获取之前创建的所有文件,并将它们放在主控机上的一个目录中。

  3. 这是我遇到问题的地方。我想在控制器上将所有这些文件合并成一个文件。我尝试使用assemble命令,它起作用了... 但它将组装后的文件放在了所有远程主机上,而不是主控机上。我如何做到这一点,但只在指定在主控机上的目的地合并文件,排除远程主机?

英文:

My current playbook does the following.

  1. Creates a file in /tmp named after the current host.

  2. Fetches all of the previously created files from the remote hosts and puts them in a directory on the master.

  3. This is where I'm running into trouble. I'd like to combine all of these files into one only on the controller. I tried to do this using assemble and it worked.... But it placed the assembled file on all of the remote hosts, not the master. How do I do this but only combine the files at the destination specified on the master, excluding remote hosts?

---
- block: 
    
    - shell: |
        cd /tmp
        echo {{ inventory_hostname }} > {{ inventory_hostname }}-Info.txt

    - ansible.builtin.fetch:
        src: /tmp/{{ inventory_hostname }}-Info.txt
        dest: /tmp/gatheredFiles/
        flat: yes 

# Creating the file on all remote hosts, not on the master machine.
    - ansible.builtin.assemble:
        src: /tmp/gatheredFiles
        dest: /tmp/combinedFile.txt
        delimiter: '######'
        remote_src: false 

      

  # when: "'condition' in ansible_hostname"

答案1

得分: 1

你至少有两个选项可以选择。

## 使用单独的任务

也许最简单的解决方案是将 `assemble` 任务移到一个只针对 `localhost` 的单独任务中:

```yaml
- hosts: all
  tasks:
    - shell: |
        cd /tmp
        echo {{ inventory_hostname }} > {{ inventory_hostname }}-Info.txt

    - ansible.builtin.fetch:
        src: /tmp/{{ inventory_hostname }}-Info.txt
        dest: /tmp/gatheredFiles/
        flat: yes 

- hosts: localhost
  tasks:
    # 在所有远程主机上创建文件,而不是在主机上创建。
    - ansible.builtin.assemble:
        src: /tmp/gatheredFiles
        dest: /tmp/combinedFile.txt
        delimiter: ''######''
        remote_src: false 

使用委派

或者,你可能可以结合使用 delegate_torun_once 来实现相同的效果:

- hosts: all
  tasks:
    - shell: |
        cd /tmp
        echo {{ inventory_hostname }} > {{ inventory_hostname }}-Info.txt        

    - ansible.builtin.fetch:
        src: /tmp/{{ inventory_hostname }}-Info.txt
        dest: /tmp/gatheredFiles/
        flat: yes 

    # 在所有远程主机上创建文件,而不是在主机上创建。
    - delegate_to: localhost
      run_once: true
      ansible.builtin.assemble:
        src: /tmp/gatheredFiles
        dest: /tmp/combinedFile.txt
        delimiter: ''######''
        remote_src: false 

<details>
<summary>英文:</summary>

You have at least two options here.

## Use a separate play

Perhaps the simplest solution is to move the `assemble` task to a separate play that only targets `localhost`:

  • hosts: all
    tasks:

    • shell: |
      cd /tmp
      echo {{ inventory_hostname }} > {{ inventory_hostname }}-Info.txt

    • ansible.builtin.fetch:
      src: /tmp/{{ inventory_hostname }}-Info.txt
      dest: /tmp/gatheredFiles/
      flat: yes

  • hosts: localhost
    tasks:

    Creating the file on all remote hosts, not on the master machine.

    • ansible.builtin.assemble:
      src: /tmp/gatheredFiles
      dest: /tmp/combinedFile.txt
      delimiter: '######'
      remote_src: false

## Use delegation

Alternatively, you can probably combine `delegate_to` and `run_once` to achieve the same thing:

  • hosts: all
    tasks:

    • shell: |
      cd /tmp
      echo {{ inventory_hostname }} > {{ inventory_hostname }}-Info.txt

    • ansible.builtin.fetch:
      src: /tmp/{{ inventory_hostname }}-Info.txt
      dest: /tmp/gatheredFiles/
      flat: yes

    Creating the file on all remote hosts, not on the master machine.

    • delegate_to: localhost
      run_once: true
      ansible.builtin.assemble:
      src: /tmp/gatheredFiles
      dest: /tmp/combinedFile.txt
      delimiter: '######'
      remote_src: false

</details>



# 答案2
**得分**: 0

以下是您提供的内容的翻译:

给定库存

```ini
shell&gt; cat hosts
test_11
test_13

您的代码

shell&gt; cat pb.yml
- hosts: all
  tasks:
    - shell: |
        cd /tmp
        echo {{ inventory_hostname }} &gt; {{ inventory_hostname }}-Info.txt        
    - fetch:
        src: /tmp/{{ inventory_hostname }}-Info.txt
        dest: /tmp/gatheredFiles/
        flat: yes
    - assemble:
        src: /tmp/gatheredFiles
        dest: /tmp/combinedFile.txt
        remote_src: false

按预期工作

<sup>

shell&gt; ansible-playbook pb.yml 

PLAY [all] ************************************************************************************

TASK [shell] **********************************************************************************
changed: [test_13]
changed: [test_11]

TASK [fetch] **********************************************************************************
changed: [test_13]
changed: [test_11]

TASK [assemble] *******************************************************************************
changed: [test_11]
changed: [test_13]

PLAY RECAP ************************************************************************************
test_11: ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test_13: ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

</sup>

<hr>

远程主机上的文件已创建

shell&gt; ssh admin@test_11 cat /tmp/test_11-Info.txt
test_11
shell&gt; ssh admin@test_13 cat /tmp/test_13-Info.txt
test_13

,获取到控制器

shell&gt; tree /tmp/gatheredFiles/
/tmp/gatheredFiles/
├── test_11-Info.txt
└── test_13-Info.txt

,并且内容已组合

shell&gt; cat /tmp/combinedFile.txt 
test_11
test_13

唯一的改进是仅运行assemble任务一次

    - assemble:
        src: /tmp/gatheredFiles
        dest: /tmp/combinedFile.txt
        remote_src: false
      run_once: true
英文:

Given the inventory

shell&gt; cat hosts
test_11
test_13

You code

shell&gt; cat pb.yml
- hosts: all
  tasks:
    - shell: |
        cd /tmp
        echo {{ inventory_hostname }} &gt; {{ inventory_hostname }}-Info.txt        
    - fetch:
        src: /tmp/{{ inventory_hostname }}-Info.txt
        dest: /tmp/gatheredFiles/
        flat: yes
    - assemble:
        src: /tmp/gatheredFiles
        dest: /tmp/combinedFile.txt
        remote_src: false

works as expected

<sup>

shell&gt; ansible-playbook pb.yml 

PLAY [all] ************************************************************************************

TASK [shell] **********************************************************************************
changed: [test_13]
changed: [test_11]

TASK [fetch] **********************************************************************************
changed: [test_13]
changed: [test_11]

TASK [assemble] *******************************************************************************
changed: [test_11]
changed: [test_13]

PLAY RECAP ************************************************************************************
test_11: ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test_13: ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

</sup>

<hr>

The files at the remote hosts were created

shell&gt; ssh admin@test_11 cat /tmp/test_11-Info.txt
test_11
shell&gt; ssh admin@test_13 cat /tmp/test_13-Info.txt
test_13

, fetched to the controller

shell&gt; tree /tmp/gatheredFiles/
/tmp/gatheredFiles/
├── test_11-Info.txt
└── test_13-Info.txt

, and the contents were assembled

shell&gt; cat /tmp/combinedFile.txt 
test_11
test_13

The only improvement would be running the assemble task once

    - assemble:
        src: /tmp/gatheredFiles
        dest: /tmp/combinedFile.txt
        remote_src: false
      run_once: true

huangapple
  • 本文由 发表于 2023年5月11日 04:43:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定