我的ansible yml文件中有3个任务,如下所示。
---
- name: Instance provisioning
local_action:
module: ec2
region: "{{ vpc_region }}"
key_name: "{{ ec2_keypair }}"
instance_type: "{{ instance_type }}"
image: "{{ ec2_image}}"
zone: "{{ public_az }}"
volumes:
- device_name: "{{ device }}"
volume_type: "{{ instance_volumetype }}"
volume_size: "{{ volume }}"
delete_on_termination: "{{ state }}"
instance_tags:
Name: "{{ instance_name }}_{{ release_name }}_APACHE"
environment: "{{ env_type }}"
vpc_subnet_id: "{{ public_id }}"
assign_public_ip: "{{ public_ip_assign }}"
group_id: "{{ sg_apache }},{{ sg_internal }}"
wait: "{{ wait_type }}"
register: ec2
- name: adding group to inventory file
lineinfile:
dest: "/etc/ansible/hosts"
regexp: "^\\[{{ release_name }}\\]"
line: "[{{ release_name }}]"
state: present
- name: adding apache ip to hosts
lineinfile:
dest: "/etc/ansible/hosts"
line: "{{ item.private_ip }} name=apache dns={{ item.public_dns_name }}
with_items: ec2.instances
现在我想检查每个任务的退出状态,无论是成功还是失败。
如果任务中的任何一个失败,我的其他任务就不应该执行。
请建议如何写一本ansible剧本
答案 0 :(得分:1)
在每个任务中注册一个变量,然后在下一个任务中进行检查。见http://docs.ansible.com/ansible/playbooks_tests.html#task-results
答案 1 :(得分:0)
也许playbook blocks并且错误处理是为了帮助你?
答案 2 :(得分:0)
这已经是Ansible中的默认行为。如果任务失败,则Playbook将中止并报告失败。您不需要围绕此构建任何额外的功能。
答案 3 :(得分:0)
库马尔
如果您想检查每个任务输出,如果成功或失败,请执行此操作,
---
- name: Instance provisioning
local_action:
module: ec2
region: "{{ vpc_region }}"
key_name: "{{ ec2_keypair }}"
instance_type: "{{ instance_type }}"
image: "{{ ec2_image}}"
zone: "{{ public_az }}"
volumes:
- device_name: "{{ device }}"
volume_type: "{{ instance_volumetype }}"
volume_size: "{{ volume }}"
delete_on_termination: "{{ state }}"
instance_tags:
Name: "{{ instance_name }}_{{ release_name }}_APACHE"
environment: "{{ env_type }}"
vpc_subnet_id: "{{ public_id }}"
assign_public_ip: "{{ public_ip_assign }}"
group_id: "{{ sg_apache }},{{ sg_internal }}"
wait: "{{ wait_type }}"
register: ec2
- name: adding group to inventory file
lineinfile:
dest: "/etc/ansible/hosts"
regexp: "^\\[{{ release_name }}\\]"
line: "[{{ release_name }}]"
state: present
when: ec2 | changed
register: fileoutput
- name: adding apache ip to hosts
lineinfile:
dest: "/etc/ansible/hosts"
line: "{{ item.private_ip }} name=apache dns={{ item.public_dns_name }}
with_items: ec2.instances
when: fileoutput | changed
在代码中,如果任务已更改为True,则在每个任务中注册一个变量,后续任务将执行,否则将跳过该任务。
答案 4 :(得分:0)
在第一个任务中,您已将输出注册到ec2。 现在使用fail模块在任务失败时停止播放。
实施例
register: ec2
fail:
when: "ec2.rc == 1"
这里rc是命令的返回码。我们假设1表示失败,0表示成功。
在每项任务后使用失败模块。
让我知道它是否适合你......