英文:
How do I make ansible assemble output the assembled file only on my local machine?
问题
-
在/tmp目录下创建一个以当前主机名命名的文件。
-
从远程主机获取之前创建的所有文件,并将它们放在主控机上的一个目录中。
-
这是我遇到问题的地方。我想在控制器上将所有这些文件合并成一个文件。我尝试使用assemble命令,它起作用了... 但它将组装后的文件放在了所有远程主机上,而不是主控机上。我如何做到这一点,但只在指定在主控机上的目的地合并文件,排除远程主机?
英文:
My current playbook does the following.
-
Creates a file in /tmp named after the current host.
-
Fetches all of the previously created files from the remote hosts and puts them in a directory on the master.
-
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_to
和 run_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
- ansible.builtin.assemble:
## 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> cat hosts
test_11
test_13
您的代码
shell> cat pb.yml
- hosts: all
tasks:
- shell: |
cd /tmp
echo {{ inventory_hostname }} > {{ 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> 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> ssh admin@test_11 cat /tmp/test_11-Info.txt
test_11
shell> ssh admin@test_13 cat /tmp/test_13-Info.txt
test_13
,获取到控制器
shell> tree /tmp/gatheredFiles/
/tmp/gatheredFiles/
├── test_11-Info.txt
└── test_13-Info.txt
,并且内容已组合
shell> 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> cat hosts
test_11
test_13
You code
shell> cat pb.yml
- hosts: all
tasks:
- shell: |
cd /tmp
echo {{ inventory_hostname }} > {{ 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> 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> ssh admin@test_11 cat /tmp/test_11-Info.txt
test_11
shell> ssh admin@test_13 cat /tmp/test_13-Info.txt
test_13
, fetched to the controller
shell> tree /tmp/gatheredFiles/
/tmp/gatheredFiles/
├── test_11-Info.txt
└── test_13-Info.txt
, and the contents were assembled
shell> 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论