Ansible模块如何返回事实

时间:2016-11-24 17:14:13

标签: ansible ansible-playbook ansible-facts

我写了Ansible模块my_module,需要设置一些事实。

我在模块中定义了以下代码

with Ada.Streams;
with Ada.Text_IO;

with GNAT.Sockets;

procedure UDP_Server is
   use GNAT.Sockets;
   Server : Socket_Type;
   Address, From : Sock_Addr_Type;
   Data : Ada.Streams.Stream_Element_Array (1 .. 512);
   Last : Ada.Streams.Stream_Element_Offset;
   Watchdog : Natural := 0;
begin
   Create_Socket (Server, Family_Inet, Socket_Datagram);
   Set_Socket_Option
     (Server,
      Socket_Level,
      (Reuse_Address, True));
   Set_Socket_Option
     (Server,
      Socket_Level,
      (Receive_Timeout,
       Timeout => 1.0));
   Address.Addr := Any_Inet_Addr;
   Address.Port := 50001;
   Bind_Socket (Server, Address);
   loop
      begin
         GNAT.Sockets.Receive_Socket (Server, Data, Last, From);
         Ada.Text_IO.Put_Line ("last : " & Last'Img);
         Ada.Text_IO.Put_Line ("from : " & Image (From.Addr));
      exception
         when Socket_Error =>
            Watchdog := Watchdog + 1;
            exit when Watchdog = 10;
      end;
   end loop;
end UDP_Server;

现在执行后的playbook my_module我想要访问新的事实,但它没有定义

.... 
response = {
    "hello": "world",
    "ansible_facts" : {
        "my_data": "xjfdks"
    }
}
module.exit_json(changed=False, meta=response)

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

您应该直接在模块的输出中设置ansible_facts,而不是在meta内。

要从示例中返回所有response个键:

module.exit_json(changed=False, **response)

或仅适用于ansible_facts:

module.exit_json(changed=False, ansible_facts=response['ansible_facts'])