可靠的剧本:运行时加载内部库存文件



我有一个INI格式的库存文件:

例如:

[db]
8.8.8.8 ansible_user=root ansible_ssh_private_key_file=/keys/root-id_rsa.pem
....

我正在寻找一种方法来在运行时自动加载我的库存文件,而不必在触发我的易操作手册命令时指定哪个库存文件路径

ansible-playbook playbook.yml --inventory-file=hosts (I'm trying to avoid from this) -vv

我熟悉"add_host"模块,但我仍然更喜欢创建INI格式的清单文件,并以某种方式让剧本自动加载它。这可能吗?

编辑:

感谢用户@techraf和@Jeff Hemmen,我添加了更多细节来质疑

此外,我不想使用ansible.cfg文件,因为我想在playbook.yml文件本身内部执行,而不是在外部执行

类似于:

- name: add_host {{environment_type}} db servers
hosts: localhost
vars_files:
- vars/main.yml
roles:
- { role: my_role}
inventory_file: (possible?)
- inventory/hosts.ini (possible?)

我想在playbook.yml文件本身内部执行此操作,而不是从外部执行

您不能在剧本中设置库存文件。Playbook是播放列表,必须具有hosts声明。在Ansible尝试(但未能)解释hosts之前,无法从剧本内部引用库存。

ansible.cfg文件中指定库存,该文件存储在与您的剧本相同的目录中:

[defaults]
inventory = ./hosts

在ansible.cfg的[defaults]部分中,有一个名为inventory的指令。将其设置为您的库存文件或目录。

我的读数:

...
[defaults]
# some basic default values...
inventory      = inventory/
...

这很容易,让剧本调用另一个带有shell的剧本:

外壳:易操作的剧本-i inventory.ini yourplaybook.yml

delegate_to:localhost

在nutshell中,您可以动态修改库存并为其运行做准备,然后生成一个shell以再次运行ansible。这样,ansible的第二代将获得修改后的库存并完成工作。甚至可以用一本剧本来做到这一点,其中创造性地使用了额外的变量,但只制作两本剧本更容易。。

有很多可能性。首先,将使用ansible插件进行静态和/或动态库存加载/配置。对于几个内置的示例,您可以参考下面列出的易解释文档。你可以在游戏中随时调用它们,就像大多数易解析插件一样:

  1. ansible.buildin.[plugin_name]:

    • ansible.builtin.advanced_host_list–分析具有范围的"主机列表">
    • ansible.builtin.constructed–使用Jinja2基于现有库存构建变量和组
    • ansible.builtin.generator–使用Jinja2根据模式构建主机和组
    • ansible.builtin.host_list–分析"主机列表"字符串
    • ansible.builtin.ini–使用Ansible INI文件作为库存源
    • ansible.builtin.script–执行返回JSON的清单脚本
    • ansible.builtin.toml–使用特定的TOML文件作为库存来源
    • ansible.builtin.yaml–使用特定的YAML文件作为库存来源

    或对于更复杂和完全动态的库存生成,请访问:

  2. amazon.aws.aws_ec2_inventure:

    要安装它,请使用:ansible galaxy collection install amazon.aws。您需要进一步的要求才能使用此库存插件,请参阅要求以了解详细信息。

    要在剧本中使用它,请指定:amazon.aws.aws_ec2。

    • amazon.aws.aws_ec2-从Amazon Web Services EC2获取库存主机。使用以aws_ec2结尾的YAML配置文件。{yml|YAML}
  3. ansible.buildin.add_host:

    • 使用变量在库存中创建新的主机和组,以便在同一剧本的后续播放中使用。

    • 采用变量,以便可以更全面地定义新主机。

    • Windows目标也支持此模块。

    使用此模块,您可以使用应转换为主机的字符串循环自定义列表/字典。完整的剧本示例如下:

    - name: 'Parse dictionary hosts'
    hosts: 'localhost'
    vars:
    my_parsed_hosts:
    controller-a:
    name: 'controller-node'
    ansible_user: 'ubuntu'
    ansible_host: '10.0.10.1'
    my_variable: false
    worker-b:
    name: 'worker-node'
    ansible_user: 'ubuntu'
    ansible_host: '127.0.0.1'
    my_variable: false
    aws-worker-custom-c:
    name: 'aws-worker-custom'
    ansible_user: 'ubuntu'
    ansible_host: '192.168.42.128'
    my_variable: true
    tasks:
    - name: 'Add new hosts to this playbook from dictionary'
    ansible.builtin.add_host:
    name: '{{ item.value.name }}'
    ansible_user: '{{ item.value.ansible_user }}'
    ansible_host: '{{ item.value.ansible_host }}'
    my_variable: '{{ item.value.my_variable }}'
    groups:
    - 'worker_nodes'
    - 'grafana'
    with_dict: '{{ my_parsed_hosts }}'
    when:
    - ('worker_nodes' not in groups)
    - ('worker' in item.value.name)
    - (item.value.name not in ansible_play_hosts_all)
    - name: 'Use parsed dictionary hosts'
    hosts: 'worker_nodes'
    tasks:
    - name: 'Print debug welcome message'
    ansible.builtin.debug:
    msg: >
    Node {{ ansible_host }} says welcome!
    

最新更新