Ansible 提取查找结果
我试图找出如何在Ansible任务中提取nslookup命令结果的“地址”值。我将始终在结果中返回1个ip地址:Ansible 提取查找结果,ansible,Ansible,我试图找出如何在Ansible任务中提取nslookup命令结果的“地址”值。我将始终在结果中返回1个ip地址: nslookup fs-d12345.efs.ap-southeast-2.amazonaws.com 10.75.0.2 Server: 10.75.0.2 Address: 10.75.0.2#53 Non-authoritative answer: Name: fs-d12345.efs.ap-southeast-2.amazonaws.com Addres
nslookup fs-d12345.efs.ap-southeast-2.amazonaws.com 10.75.0.2
Server: 10.75.0.2
Address: 10.75.0.2#53
Non-authoritative answer:
Name: fs-d12345.efs.ap-southeast-2.amazonaws.com
Address: 10.75.21.67
我需要将值10.75.21.67存储为var,以便稍后在剧本中使用
我的任务类似于:
- shell: "nslookup fs-d12345.efs.ap-southeast-2.amazonaws.com 10.75.0.2"
register: results
如何提取地址的值
谢谢 在使用
命令
或shell
模块之前,请务必仔细查看常见任务的替代方案,Ansible社区通常会为您完成繁重的工作
如果您可以安装一个额外的Python包来支持,那么Ansible中已经内置了一个nslookup(well dig)实用程序:
- set_fact:
target_ip: "{{ lookup('dig', 'fs-d12345.efs.ap-southeast-2.amazonaws.com', '@10.75.0.2') }}"
要使其工作,先决条件是您需要在运行此任务的机器上安装dnspython库,例如
apt-get install python-dnspython
或
如果您只想在控制计算机上执行此操作,但希望能够访问远程计算机上的查找数据,则可以执行以下操作:
- hosts: localhost
connection: local
tasks:
- set_fact:
target_ip: "{{ lookup('dig', 'fs-d12345.efs.ap-southeast-2.amazonaws.com', '@10.75.0.2') }}"
- hosts: remote_machine
tasks:
- debug:
var: hostvars['localhost'].target_ip
谢谢,这个有用。是的,它是python dns。我担心我必须想出一些正则表达式模式过滤器来提取地址。
- hosts: localhost
connection: local
tasks:
- set_fact:
target_ip: "{{ lookup('dig', 'fs-d12345.efs.ap-southeast-2.amazonaws.com', '@10.75.0.2') }}"
- hosts: remote_machine
tasks:
- debug:
var: hostvars['localhost'].target_ip