我對 ansible 還很陌生,透過閱讀我正在做的事情應該可以工作。我有以下文件名為aws.yml
---
- hosts: localhost
connection: local
gather_facts: no
vars:
aws_region: us-east-1
tasks:
- name: Main VPC
ec2_vpc:
state: present
cidr_block: 10.5.0.0/16
resource_tags: {"environment": "prod"}
region: "{{ aws_region }}"
dns_hostnames: no
dns_support: yes
instance_tenancy: default
internet_gateway: yes
subnets:
- cidr: 10.5.151.96/27
az: "{{ aws_region }}a"
resource_tags: {"Name": "private"}
- cidr: 10.5.151.128/27
az: "{{ aws_region }}b"
resource_tags: {"Name": "private"}
- cidr: 10.5.151.160/27
az: "{{ aws_region }}c"
resource_tags: {"Name": "private"}
當我運行 ansible-playbook 時,我得到以下信息
$ ansible-playbook aws.yml
ERROR: Unable to find an inventory file, specify one with -i ?
根據我的理解,主機: localhost 應該取代使用 -i ,因為我試圖在這裡建立一個 VPC,所以我不想透過 ssh 進入機器來建構它。
謝謝
答案1
您需要指向一個清單檔案(甚至是一個空檔案),因為劇本只說明該戲劇適用於哪個主機,但沒有提供定義此類清單的方法,而清單則提供了定義此類清單的方法。幸運的是,localhost
預設情況下可用:
$ cat playbook.yml
---
- hosts: localhost
connection: local
gather_facts: no
tasks:
- shell: echo foo
$ ansible-playbook -i /dev/null playbook.yml
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [localhost] **************************************************************
TASK: [shell echo foo] ********************************************************
changed: [localhost]
PLAY RECAP ********************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0