英文:
Unable to select a particular host from inventory file in ansible
问题
我通过一个教程学习了如何使用synchronize命令在两台服务器之间直接传输文件,我正在尝试将文件从源服务器传输到目标服务器,其中源服务器只有一台,而目标服务器在清单文件中有多台,如下所示:
[source]
server_1
[destination]
server_2
server_3
server_4
根据特定条件,我将选择要使用"synchronize push"传输文件的服务器。但是我尝试了许多方法来提取服务器,但甚至无法选择一个主机:
- hosts: "{{ groups['destination'][0] }}"
- hosts: "{{destination.0}}"
- hosts: "{{destination[0]}}"
所有这些方法都给我抛出了以下错误:
引发错误的行似乎是:
- hosts: "{{ destination }}"
^ 这里我们可能错了,但这个问题看起来可能是引号缺失的问题。始终在模板表达式括号开始值时加上引号。例如:
with_items: - {{ foo }}
应该写成:
with_items: - "{{ foo }}"
在Ansible playbook中如何选择清单文件中的目标服务器?
英文:
I went through a tutorial to transfer files between two servers directly using synchronize command and I am trying to transfer a file from source to destination where source will have 1 server and destination will have multiple servers in the inventory file as seen below:
[source]
server_1
[destination]
server_2
server_3
server_4
and based on a specific condition, I will select which server I will "synchronize push" to transfer the file. But I tried many methods to extract the server but I am unable to even pick a host:
-hosts: "{{ groups['destination'][0] }}"
-hosts: "{{destination.0}}"
-hosts: "{{destination[0]}}"
Those all throw me the below error:
> The offending line appears to be:
>
>
> - hosts: "{{ destination }}"
> ^ here
>
> We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote
> template expression brackets when they start a value. For instance:
>
> with_items:
> - {{ foo }}
>
> Should be written as:
>
> with_items:
> - "{{ foo }}"
How do I select the destination server from inventory file in ansible playbook?
答案1
得分: 6
我认为您正在尝试在playbook中使用库存组[destination]。
hosts: "{{ destination }}"
^ 这里
这应该没有括号,而不是。
hosts: destination
如果您想要在特定匹配的主机上运行,那么
hosts: destination[2:3]
如果您想要在特定主机上运行任务,还可以使用delegate。
delegate_to: {{ hostname }}
英文:
I think you are trying to use Inventory group [destination] in playbook.
> hosts: "{{ destination }}"
^ here
This should be without bracket instead of.
> hosts: destination
And if you want to run on specific match host then
> hosts: destination[2:3]
Also you can use delegate if you want to run task to specific host.
> delegate_to: {{ hostname }}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论