gpt4 book ai didi

Ansible 打印创建的主机

转载 作者:行者123 更新时间:2023-12-03 09:04:14 25 4
gpt4 key购买 nike

我编写了一个可创建多个虚拟机的 Ansible Playbook。 Playbook 分为两个文件。 Main.yaml 和 vars.yaml。它创建了虚拟机,看起来运行良好。我没有收到任何错误,所以我假设它已成功将创建的主机添加到 list 中。我想检查创建的主机是否已添加到 list 中。我如何打印/列出库存的主机?我的目标是稍后在创建的虚拟机上运行脚本。谢谢。

**Main.yaml**
#########CREATING VM#########
---
- hosts: localhost
vars:
http_port: 80
max_clients: 200
vars_files:
- vars.yaml
tasks:
- name: create VM
os_server:
name: "{{ item.name }}"
state: present
image: "{{ item.image }}"
boot_from_volume: True
security_groups: ssh
flavor: "{{ item.flavor }}"
key_name: mykey
region_name: "{{ lookup('env', 'OS_REGION_NAME') }}"
nics:
- net-name: private
wait: yes
register: instances
with_items: "{{ instance_definitions }}"
############################################
- name: whait 15 seconds
pause: seconds=15
when: instances.changed
######DEBUG#################################
- name: display results
debug:
msg: "{{ item }}"
with_items: "{{ instances.results }}"
############################################
- name: Add new VM to ansible Inventory
add_host:
name: "{{ item.server.name}}"
ansible_host: "{{item.server.public_v4}}"
ansible_user: "{{ansible_user}}"
ansible_ssh_common_args: -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no
groups: just_created
with_items: "{{ instances.results }}"



**vars.yaml**
---
instance_definitions:
- { name: Debian Jessie, image: Debian Jessie 8, flavor: c1.small, loginame: debian }
- { name: Debian Stretch, image: Debian Stretch 9, flavor: c1.small, loginame: debian }

最佳答案

这就是 magic variables是为了。

您的所有主机都将出现在列表中:

groups['just_created']

关于Ansible 打印创建的主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48285944/

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