Ansible 打印创建的主机
Ansible print created hosts
我编写了一个创建多个 VM 的 Ansible Playbook。剧本分为两个文件。 Main.yaml 和 vars.yaml。它创建了 VM,并且看起来运行良好。我没有收到任何错误,所以我假设它已成功将创建的主机添加到清单中。我想检查创建的主机是否已添加到清单中。我如何 print/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']
您可以使用剧本打印清单文件的内容:
- debug: msg="the hosts are is {{lookup('file', '/etc/ansible/hosts') }}"
或者,您可以使用命令行列出主机:
ansible --list-hosts all
或使用剧本中的此命令:
tasks:
- name: list hosts
command: ansible --list-hosts all
register: hosts
- debug:
msg: "{{hosts.stdout_lines}}"
下面的示例说明了如何创建 in-memory 主机并列出它们。神奇的是add_hosts
需要单独播放
---
- name: adding host playbook
hosts: localhost
connection: local
tasks:
- name: add host to in-memory inventory
add_host:
name: awesome_host_name
groups: in_memory
- name: checking hosts
hosts: in_memory
connection: local
gather_facts: false
tasks:
- debug: var=group_names
- debug: msg="{{ inventory_hostname }}"
- debug: var=hostvars[inventory_hostname]
我编写了一个创建多个 VM 的 Ansible Playbook。剧本分为两个文件。 Main.yaml 和 vars.yaml。它创建了 VM,并且看起来运行良好。我没有收到任何错误,所以我假设它已成功将创建的主机添加到清单中。我想检查创建的主机是否已添加到清单中。我如何 print/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']
您可以使用剧本打印清单文件的内容:
- debug: msg="the hosts are is {{lookup('file', '/etc/ansible/hosts') }}"
或者,您可以使用命令行列出主机:
ansible --list-hosts all
或使用剧本中的此命令:
tasks:
- name: list hosts
command: ansible --list-hosts all
register: hosts
- debug:
msg: "{{hosts.stdout_lines}}"
下面的示例说明了如何创建 in-memory 主机并列出它们。神奇的是add_hosts
需要单独播放
---
- name: adding host playbook
hosts: localhost
connection: local
tasks:
- name: add host to in-memory inventory
add_host:
name: awesome_host_name
groups: in_memory
- name: checking hosts
hosts: in_memory
connection: local
gather_facts: false
tasks:
- debug: var=group_names
- debug: msg="{{ inventory_hostname }}"
- debug: var=hostvars[inventory_hostname]