我写了一本非常有趣的剧本,包括输出到文件中。我有几个要从我的EC2环境中收集的字段(简单标记:名称;公共IP,私有IP)。与输出有关,我遇到了两个似乎无法解决的小问题。
为什么我的调试消息列表会收集服务器名称,而不是所有公共IP和私有IP?
如何获得每个名称及其各自的公用IP和专用IP的行?] >>
我发现了周围的各种代码片段和示例,并将它们组合在一起,以创建一个非常基础的入门手册,稍后我将对其进行扩展。
这是我的剧本:
--- - name: Test ec2 info hosts: local connection: local tasks: - name: Get EC2 Info ec2_instance_info: filters: "tag:Name": "SERVER0*" register: ec2_name - name: Get instance IP addresses. debug: msg: "{{ item.0 }} | {{ item.1 }} | {{ item.2 }}" with_together: - "{{ ec2_name.instances | map(attribute='tags.Name') | list }}" - "{{ ec2_name.instances[0].public_ip_address }}" - "{{ ec2_name.instances[0].private_ip_address }}" - name: Gather and Save Instance Info set_fact: Tag_Name: "{{ ec2_name.instances | map(attribute='tags.Name') | list }}" Pub_IP: "{{ ec2_name.instances[0].public_ip_address }}" Pvt_IP: "{{ ec2_name.instances[0].private_ip_address }}" - local_action: copy content="{{ Tag_Name }} | {{ Pub_IP }} | {{ Pvt_IP }}" dest=ec2iptest.txt
这是调试输出到屏幕:
TASK [Get only running instance IP addresses.] ********************************* ok: [localhost] => (item=['SERVER01', 'xx.xx.xx.xx', '10.1.0.10']) => { "msg": "SERVER01 | xx.xx.xx.xx | 10.1.0.10" } ok: [localhost] => (item=['SERVER02', None, None]) => { "msg": "SERVER02 | | " } ok: [localhost] => (item=['SERVER03', None, None]) => { "msg": "SERVER03 | | " } ok: [localhost] => (item=['SERVER04', None, None]) => { "msg": "SERVER04 | | " } TASK [Gather and Save Instance Info] ******************************************* ok: [localhost] TASK [copy] ******************************************************************** changed: [localhost -> localhost] PLAY RECAP ********************************************************************* localhost : ok=5 changed=1
这是文本文件的输出:
['SERVER01', 'SERVER02', 'SERVER03', 'SERVER04'] | xx.xx.xx.xx | 10.1.0.10
将所有服务器标签名称放在括号中的一行上,并且仅提供一个公共IP和一个私有IP。
我希望输出到文本文件(带有管道定界符)是:
SERVER_NAME01 | PublicIP | PrivateIP SERVER_NAME02 | PublicIP | PrivateIP SERVER_NAME03 | PublicIP | PrivateIP ...
任何有关正确收集和换行的帮助都将非常有用。
谢谢。
我写了一本非常有趣的剧本,包括输出到文件中。我有几个要从我的EC2环境中收集的字段(简单标记:名称;公共IP,私有IP)。我正在...
我认为您需要的是:
Pub_IP:“ {{ec2_name.instances [0] .network_interfaces [0] .public_ip_address}}”]