Ansible返回值 - 需要IP地址

问题描述:

我们目前正在将Ansible与OpenStack结合使用。我已经编写了一本剧本(通过OpenStack部署新服务器),我使用的模块为os_server,我使用auto_ip: yes,新服务器将成为从OpenStack服务器分配的IP地址。Ansible返回值 - 需要IP地址

如果我使用-vvvv输出命令,我得到一个长输出其中在该输出的中间列出了一个IP地址。

因此,我是一个懒惰的人,我想把这个IP地址放在一个变量中,让我在额外的字段中显示这个IP地址。 它应该是这样的:

"........output stuf..... 
................................ 
............................. 
.............................. 
..............................." 

"The IP Adress of the New server is ....." 

有没有你知道把这些IP地址字段中的变量或以滤除输出到IP地址的任何可能性?

如果您需要屏幕截图来查看我的意思,请不要直接写下来,我会给您!

+0

请编辑您的问题的标题。 “地址”与两个d。在标题中引用Openstack和os_server。像这样,即使它是一个很好的问题,我也不会满足你的问题。 –

Ansible OpenStack模块使用shade python包创建服务器。

根据shade源代码,create_server方法返回一个代表创建服务器的字典。

尝试注册os_server的结果并对其进行调试。 IP地址应该在那里。

例子:

- name: launch a compute instance 
    hosts: localhost 
    tasks: 
    - name: launch an instance 
    os_server: 
     state: present 
     ... 
     auto_ip: yes 
    register: result 

- debug: var=result 

此外,您还可以看看这个sample playbook它正是这样做的。

下面是摘录:

- name: create cluster notebook VM 
    register: notebook_vm 
    os_server: 
    name: "{{ cluster_name }}-notebook" 
    flavor: "{{ notebook_flavor }}" 
    image: "CentOS-7.0" 
    key_name: "{{ ssh_key }}" 
    network: "{{ network_name }}" 
    security_groups: 
     - "{{ cluster_name }}-notebook" 
    auto_ip: yes 
    boot_from_volume: "{{ notebook_boot_from_volume }}" 
    terminate_volume: yes 
    volume_size: 25 

- name: add notebook to inventory 
    add_host: 
    name: "{{ cluster_name }}-notebook" 
    groups: notebooks 
    ansible_ssh_host: "{{ notebook_vm.openstack.private_v4 }}" 
    ansible_ssh_user: cloud-user 
    public_ip: "{{ notebook_vm.openstack.public_v4 }}" 
    public_name: "{{ lookup('dig', notebook_vm.openstack.public_v4 + '/PTR', wantlist=True)[0] }}" 
    tags: ['vm_creation'] 
+0

我认为值的确切名称是: “{{notebook_vm.openstack.accessIPv4}}” –