我想通过在命令行中提供实例 ID 来在特定的 ec2 实例上运行 playbook。比如:
ansible-playbook playbook.yml -e "instance-id=i-123456"
.
我尝试使用
amazon.aws.aws_ec2
插件库存,但过滤器不支持 Jinja2,因此它无法识别从命令行提供的参数。
这是我使用的库存:
plugin: amazon.aws.aws_ec2
regions:
- us-east-2
filters:
"tag:MyTag": "*"
hostnames:
- instance-id
我想一次仅在一个实例上运行该剧本。如果我将提供包含所有实例的清单,但如果我错误地忘记添加
标志,则剧本将在带有 ... 标签的所有实例上运行。--limit
问:“如何让 playbook 仅在特定条件下运行?”
pre_tasks
,并检查某些条件。
一个最小的示例手册
---
- hosts: test
become: false
gather_facts: false
pre_tasks:
- name: Check amount of play_hosts
delegate_to: localhost
fail:
msg:
- This playbook is allowed to run on one host at a time only!
- Provided amount was {{ ansible_play_hosts | length }}
when: ansible_play_hosts | length | int > 1
run_once: true
tasks:
- debug:
var: ansible_play_hosts
将产生
的输出TASK [Check amount of play_hosts] ******************************
fatal: [test.example.com -> localhost]: FAILED! => changed=false
msg:
- This playbook is allowed to run on one host at a time only!
- Provided amount was 2
或
TASK [debug] ********************
ok: [test.example.com] =>
ansible_play_hosts:
- test.example.com
PLAY RECAP **********************
test.example.com : ok=1
类似问答