Ansible返回值 - 需要IP地址

时间:2016-07-25 06:31:42

标签: automation ansible return-value

我们目前正在将Ansible与OpenStack结合使用。我写了一个剧本(通过OpenStack部署新服务器),我使用模块#include <stdio.h> main() { int i, n, count; int ara[]={2, 3, 5, 7}; for(i=0; i<4; i++) { count=0; for(n=10; n>0; n--) { while(n%ara[i]==0) { count++; n=n/ara[i]; } } printf("(%d^%d)" , ara[i], count); } return 0; } ,我使用os_server,新服务器将成为从OpenStack服务器分配的IP地址。

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

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

-vvvv

您是否知道将这些IP地址字段放在变量中或将该输出过滤到IP地址?

如果你需要一个截图来看我的意思,没问题,只需写下来,我就会给你!

1 个答案:

答案 0 :(得分:2)

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

根据shade源代码,create_server方法返回表示创建的服务器的dict。

尝试注册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']