https://github.com/ansible/ansible/blob/stable-2.9/contrib/inventory/
https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html
https://docs.ansible.com/ansible/latest/user_guide/intro_dynamic_inventory.html#inventory-script-example-aws-ec2 (深坑,脚本404,找到了脚本,各种错误,请扔一边)
前言
通过 ansible 获取大区下 ec2 资源信息
授权
export AWS_ACCESS_KEY_ID='AK123'
export AWS_SECRET_ACCESS_KEY='abc123'
export EC2_INI_PATH=ec2.ini
库存(inventory)
[local]
localhost
Playbook
---
- name: test ec2
hosts: local
gather_facts: no # 我们要这信息干什么?我们是有目标的
connection: local # 木有定义资源
tasks:
- name: get ec2 info
ec2_instance_info:
region: cn-north-1
register: data_output
- name: show ec2 info
debug:
msg: "{{ data_output|json_query('instances[*].network_interfaces[*].private_ip_address') }}"
执行
ansible-playbook -i hosts ec2.yml
输出
TASK [show ec2 info] ******************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
[
"10.100.10.250"
],
[
"10.100.10.252"
],
[
"10.100.10.210"
],
[
"10.100.10.251"
]
]
}