首页 > 解决方案 > 在组变量中设置 ansible_host

问题描述

这可能是一个愚蠢的想法,但我的 ansible 脚本有两个相同的游乐场。

我想创建一个包含两个组的清单并通过组从主机中选择 ip 这可能吗?我需要它是因为前端和后端在两个组中都需要相同的 host_vars,但它们是不同的服务器。

主机变量:

frontend.yml(在 host_vars 中):

frontent_property=look good

后端 .yml(在 host_vars 中):

backend_property=handle requests 

组变量:

integration.yml(在 group_vars 中):

integration_property=have fun

production.yml(在 group_vars 中):

production_property=do business 

存货:

frontend
backend 

[integration]
frontend  ansible_host=10.0.0.5
backend    ansible_host=10.0.0.6

[production]
frontend  ansible_host=10.1.0.5
backend   ansible_host=10.1.0.6

剧本:

- hosts: integration
  gather_facts: false
  tasks: 
    - debug:
        msg: "{{ ansible_host }}"
        
- hosts: production
  gather_facts: false
  tasks: 
    - debug:
        msg: "{{ ansible_host }}"

这不起作用(两者都针对集成 ips 运行)-是否有另一种解决方案来解决这个问题?

我找到了解决方案,但现在我失去了 host_vars :

group_vars:frontend.yml(在 group_vars 中):

frontent_property=look good

backend.yml(在 group_vars 中):

backend_property=handle requests 

integration.yml(在 group_vars 中):

some_property=have fun

production.yml(在 group_vars 中):

some_property=do business 

存货:

10.0.0.5
10.0.0.6
10.1.0.5
10.1.0.6

[frontend]
10.0.0.5
10.1.0.5

[backend]
10.0.0.6
10.1.0.6

[integration]
10.0.0.6
10.0.0.5  

[production]
10.1.0.5 
10.1.0.6

但更可能是我犯了一个错误,因为我没有使用 :children 说明符(Group in Group)所以我不认为 [backend] 中的组值可以从范围 [integration]/production 或我错了吗?

标签: ansible

解决方案


这是正确的库存

10.0.0.5
10.0.0.6
10.1.0.5
10.1.0.6

[integration]
10.0.0.5 my_hostname=frontend
10.0.0.6 my_hostname=backend

[production]
10.1.0.5 my_hostname=frontend
10.1.0.6 my_hostname=backend

推荐阅读