我对 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 ?
据我了解,hosts: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