如何正确构建 Ansible 库存

发布于 2025-01-12 03:59:13 字数 901 浏览 0 评论 0原文

我想构建一个 Ansible 库存,但又不想让它变得像意大利面条一样,而且我喜欢保持 DRY。 我查看了 Ansible 文档,但我不能查找以下信息:

我目前正在使用此格式来构建库存。

[dev-postgres]
a1-dev-01 ansible_host=vm-1.my.internal.domain.com 
a1-dev-02 ansible_host=vm-2.my.internal.domain.com 

[dev-ldap]
a1-dev-02 ansible_host=vm-2.my.internal.domain.com 
a1-dev-03 ansible_host=vm-3.my.internal.domain.com 

[dev:children]
dev-ldap
dev-postgres

[dev:vars]
env=dev

[jumphost]
a2-jump-01 ansible_host=vm-0.my.internal.domain.com 

关于此设置,我有三个问题:

  • 是否可以为每个 ansible_host 定义“vm-x”,并配置“.my.internal.domain.com”一次,并且每个主机都会将该值附加到其 ansible_host 值中?
  • 是否可以为每个主机定义一次名称(例如a6-dev-01)+ ansible_host。然后可以在多个组下设置名称(例如a6-dev-01),而无需定义ansible_host?
  • 组的名称可以有多个值吗?例如[dev-postgres, another_value]

I want to build an Ansible inventory without making it spaghetti and I like to be DRY.
I took a look at the Ansible Docs, but I can't find the information for the following:

I am currently using this format to build an inventory.

[dev-postgres]
a1-dev-01 ansible_host=vm-1.my.internal.domain.com 
a1-dev-02 ansible_host=vm-2.my.internal.domain.com 

[dev-ldap]
a1-dev-02 ansible_host=vm-2.my.internal.domain.com 
a1-dev-03 ansible_host=vm-3.my.internal.domain.com 

[dev:children]
dev-ldap
dev-postgres

[dev:vars]
env=dev

[jumphost]
a2-jump-01 ansible_host=vm-0.my.internal.domain.com 

I have three questions regarding this setup:

  • Is it possible, for each ansible_host, define 'vm-x', and configure the '.my.internal.domain.com' once, and each host will have that value appended to their ansible_host value?
  • Is it possible to define for each host the name(e.g. a6-dev-01) + ansible_host once. And then the name(e.g. a6-dev-01) could be set under multiple groups, without defining ansible_host?
  • Is it possible for the name of the group to have multiple values? E.g. [dev-postgres, another_value]?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极致的悲 2025-01-19 03:59:13

使用构建库存插件。请参见

shell> ansible-doc -t inventory constructed

示例

shell> cat hosts-627/1-hosts
a1_dev_01 vm_x=vm-1
a1_dev_02 vm_x=vm-2
a1_dev_03 vm_x=vm-3
a2_jump_01 vm_x=vm-0

[dev_postgres]
a1_dev_01
a1_dev_02

[dev_ldap]
a1_dev_02
a1_dev_03

[dev:children]
dev_postgres
dev_ldap

[dev:vars]
env=dev

[jumphost]
a2_jump_01

[all:vars]
vm_domain=my.internal.domain.com
shell> cat hosts-627/2-constructed.yml 
plugin: constructed
strict: true
compose:
  ansible_host: vm_x ~ '.' ~ vm_domain

给出

shell> ansible-inventory -i hosts-627 --list --yaml
all:
  children:
    dev:
      children:
        dev_ldap:
          hosts:
            a1_dev_02:
              ansible_host: vm-2.my.internal.domain.com
              env: dev
              vm_domain: my.internal.domain.com
              vm_x: vm-2
            a1_dev_03:
              ansible_host: vm-3.my.internal.domain.com
              env: dev
              vm_domain: my.internal.domain.com
              vm_x: vm-3
        dev_postgres:
          hosts:
            a1_dev_01:
              ansible_host: vm-1.my.internal.domain.com
              env: dev
              vm_domain: my.internal.domain.com
              vm_x: vm-1
            a1_dev_02: {}
    jumphost:
      hosts:
        a2_jump_01:
          ansible_host: vm-0.my.internal.domain.com
          vm_domain: my.internal.domain.com
          vm_x: vm-0
    ungrouped: {}

下一个选项是将变量放入 group_vars 中。例如

shell> cat hosts-628/hosts
a1_dev_01 vm_x=vm-1
a1_dev_02 vm_x=vm-2
a1_dev_03 vm_x=vm-3
a2_jump_01 vm_x=vm-0

[dev_postgres]
a1_dev_01
a1_dev_02

[dev_ldap]
a1_dev_02
a1_dev_03

[dev:children]
dev_postgres
dev_ldap

[jumphost]
a2_jump_01
shell> cat group_vars/all.yml 
vm_domain: my.internal.domain.com
ansible_host: "{{ vm_x }}.{{ vm_domain }}"
shell> cat group_vars/dev.yml 
env: dev

给出

shell> ansible-inventory -i hosts-628 --list --yaml
all:
  children:
    dev:
      children:
        dev_ldap:
          hosts:
            a1_dev_02:
              ansible_host: '{{ vm_x }}.{{ vm_domain }}'
              env: dev
              vm_domain: my.internal.domain.com
              vm_x: vm-2
            a1_dev_03:
              ansible_host: '{{ vm_x }}.{{ vm_domain }}'
              env: dev
              vm_domain: my.internal.domain.com
              vm_x: vm-3
        dev_postgres:
          hosts:
            a1_dev_01:
              ansible_host: '{{ vm_x }}.{{ vm_domain }}'
              env: dev
              vm_domain: my.internal.domain.com
              vm_x: vm-1
            a1_dev_02: {}
    jumphost:
      hosts:
        a2_jump_01:
          ansible_host: '{{ vm_x }}.{{ vm_domain }}'
          vm_domain: my.internal.domain.com
          vm_x: vm-0
    ungrouped: {}

唯一的区别是表达式将根据需要进行计算。这意味着变量vm_xvm_domain将成为优先级。例如,剧本

shell> cat test-628.yml
- hosts: all
  gather_facts: false
  tasks:
    - debug:
        var: ansible_host

给出(删节)

ok: [a2_jump_01] => 
  ansible_host: vm-0.my.internal.domain.com
ok: [a1_dev_01] => 
  ansible_host: vm-1.my.internal.domain.com
ok: [a1_dev_02] => 
  ansible_host: vm-2.my.internal.domain.com
ok: [a1_dev_03] => 
  ansible_host: vm-3.my.internal.domain.com

Use constructed inventory plugin. See

shell> ansible-doc -t inventory constructed

For example

shell> cat hosts-627/1-hosts
a1_dev_01 vm_x=vm-1
a1_dev_02 vm_x=vm-2
a1_dev_03 vm_x=vm-3
a2_jump_01 vm_x=vm-0

[dev_postgres]
a1_dev_01
a1_dev_02

[dev_ldap]
a1_dev_02
a1_dev_03

[dev:children]
dev_postgres
dev_ldap

[dev:vars]
env=dev

[jumphost]
a2_jump_01

[all:vars]
vm_domain=my.internal.domain.com
shell> cat hosts-627/2-constructed.yml 
plugin: constructed
strict: true
compose:
  ansible_host: vm_x ~ '.' ~ vm_domain

gives

shell> ansible-inventory -i hosts-627 --list --yaml
all:
  children:
    dev:
      children:
        dev_ldap:
          hosts:
            a1_dev_02:
              ansible_host: vm-2.my.internal.domain.com
              env: dev
              vm_domain: my.internal.domain.com
              vm_x: vm-2
            a1_dev_03:
              ansible_host: vm-3.my.internal.domain.com
              env: dev
              vm_domain: my.internal.domain.com
              vm_x: vm-3
        dev_postgres:
          hosts:
            a1_dev_01:
              ansible_host: vm-1.my.internal.domain.com
              env: dev
              vm_domain: my.internal.domain.com
              vm_x: vm-1
            a1_dev_02: {}
    jumphost:
      hosts:
        a2_jump_01:
          ansible_host: vm-0.my.internal.domain.com
          vm_domain: my.internal.domain.com
          vm_x: vm-0
    ungrouped: {}

The next option is putting the variables into the group_vars. For example

shell> cat hosts-628/hosts
a1_dev_01 vm_x=vm-1
a1_dev_02 vm_x=vm-2
a1_dev_03 vm_x=vm-3
a2_jump_01 vm_x=vm-0

[dev_postgres]
a1_dev_01
a1_dev_02

[dev_ldap]
a1_dev_02
a1_dev_03

[dev:children]
dev_postgres
dev_ldap

[jumphost]
a2_jump_01
shell> cat group_vars/all.yml 
vm_domain: my.internal.domain.com
ansible_host: "{{ vm_x }}.{{ vm_domain }}"
shell> cat group_vars/dev.yml 
env: dev

gives

shell> ansible-inventory -i hosts-628 --list --yaml
all:
  children:
    dev:
      children:
        dev_ldap:
          hosts:
            a1_dev_02:
              ansible_host: '{{ vm_x }}.{{ vm_domain }}'
              env: dev
              vm_domain: my.internal.domain.com
              vm_x: vm-2
            a1_dev_03:
              ansible_host: '{{ vm_x }}.{{ vm_domain }}'
              env: dev
              vm_domain: my.internal.domain.com
              vm_x: vm-3
        dev_postgres:
          hosts:
            a1_dev_01:
              ansible_host: '{{ vm_x }}.{{ vm_domain }}'
              env: dev
              vm_domain: my.internal.domain.com
              vm_x: vm-1
            a1_dev_02: {}
    jumphost:
      hosts:
        a2_jump_01:
          ansible_host: '{{ vm_x }}.{{ vm_domain }}'
          vm_domain: my.internal.domain.com
          vm_x: vm-0
    ungrouped: {}

The only difference is that the expressions will be evaluated on demand. This means that the variables vm_x and vm_domain will be subjects of the precedence. For example the playbook

shell> cat test-628.yml
- hosts: all
  gather_facts: false
  tasks:
    - debug:
        var: ansible_host

gives (abridged)

ok: [a2_jump_01] => 
  ansible_host: vm-0.my.internal.domain.com
ok: [a1_dev_01] => 
  ansible_host: vm-1.my.internal.domain.com
ok: [a1_dev_02] => 
  ansible_host: vm-2.my.internal.domain.com
ok: [a1_dev_03] => 
  ansible_host: vm-3.my.internal.domain.com
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文