gpt4 book ai didi

ansible - 将剧本信息保存到主机文件

转载 作者:行者123 更新时间:2023-12-03 18:31:07 24 4
gpt4 key购买 nike

我有一本使用 Ansible 内置的核心模块在 DigitalOcean 上启动新液滴的剧本:

- name: Provision droplet on DigitalOcean
local_action: digital_ocean
state=present
ssh_key_ids=1234
name=mydroplet
client_id=ABC
api_key=ABC
size_id=1
region_id=2
image_id=3
wait_timeout=500
register: my_droplet
- debug: msg="ID is {{ my_droplet.droplet.id }}"
- debug: msg="Your droplet has the IP address of {{ my_droplet.droplet.ip_address }}"

我使用(注意本地参数)运行它:
ansible-playbook playbooks/create_droplet.yml -c local -i playbooks/hosts

我的主机文件最初如下所示:
[production]
TBA

[localhost]
localhost

当上面的剧本完成后,我可以在 STDOUT 中看到调试信息:
ok: [localhost] => {
"msg": "Your droplet has the IP address of 255.255.255.255"
}

这个剧本有什么办法可以保留 my_droplet.ip_address变量并将 TBA 保存在 hosts 文件中,而不必在那里手动复制粘贴它?我问是因为我想将此配置手册添加到 ruby​​ 脚本中,该脚本随后使用另一个手册引导 VPS。

最佳答案

我也在做同样的事情,写了一个从 dict 创建服务器的剧本(一次大约 53 个服务器,动态创建一个完整的测试环境)。要使用静态主机文件,我添加以下内容:

- name: Create in-memory inventory
add_host:
name: "{{ item.value.ServerName }}"
groups: "{{ item.value.RoleTag }},tag_Environment_{{ env_name }}"
when: item.value.template is defined
with_dict: server_details

- name: create file
become: yes
template:
src: ansible-hosts.j2
dest: "{{ wm_inventory_file }}"

ansible-hosts.j2 模板很简单:
{% for item in groups %}

[{{item}}]
{% for entry in groups[item] %}
{{ entry }}
{% endfor %}
{% endfor %}

关于ansible - 将剧本信息保存到主机文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26943772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com