如何获取ansible中每个任务的退出状态
我的ansible yml文件中有3个任务,如下所示如何获取ansible中每个任务的退出状态,ansible,Ansible,我的ansible yml文件中有3个任务,如下所示 --- - name: Instance provisioning local_action: module: ec2 region: "{{ vpc_region }}" key_name: "{{ ec2_keypair }}" instance_type: "{{ instance_type }}" image: "{{ ec2_image}}" zo
---
- 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 playbook,它的错误处理是为了帮助您?可能,它的错误处理是为了帮助您?在每个任务中注册一个变量,然后在下一个任务中检查它。请参见在每个任务中注册一个变量,然后在下一个任务中检查它。请参见这已经是Ansible中的默认行为。如果任务失败,Playbook将中止并报告失败。您不需要为此内置任何额外的功能。这已经是Ansible中的默认行为。如果任务失败,Playbook将中止并报告失败。您不需要为此内置任何额外的功能。Kumar 如果要检查每个任务输出是否成功,请执行此操作
---
- 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,则后续任务将执行,否则将跳过该任务。Kumar
如果要检查每个任务输出是否成功,请执行此操作
---
- 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,则后续任务将执行,否则将跳过该任务。在第一个任务中,您已将输出注册到ec2。 现在,如果任务失败,请使用失败模块停止播放 前 这里rc是命令的返回码。。我们假设1表示失败,0表示成功 在每个任务后使用失败模块
如果对你有效,请告诉我 在第一个任务中,您已经将输出注册到ec2。 现在,如果任务失败,请使用失败模块停止播放 前 这里rc是命令的返回码。。我们假设1表示失败,0表示成功 在每个任务后使用失败模块
如果对你有效,请告诉我 大家好,因为我是ansible的初学者,当我浏览链接时,真的没有快速的想法,因此请分享一个示例以便我更好地理解。大家好,因为我是ansible的初学者,当我浏览链接时,真的没有快速的想法,因此,请分享一个示例,以便我更好地理解。当任务失败后,游戏手册失败已经是默认行为时,为什么要这样做?虽然游戏手册中没有包含它,但他可能很容易希望在任务中包含一个“failed_when:False”,以便失败的任务继续。这将是一个原因。当任务失败时,您并不总是希望剧本失败。事实上,如果你把剧本设计得很好,你可能永远都不想让它发生。这与他所要求的完全相反,因为如果一项任务失败,他确实希望整个剧本都失败。这也是非常糟糕的做法。如果遇到故障,您几乎总是希望停止,因为这意味着不可能达到您声明的状态。如果失败后需要进行一些清理,则可以使用块中的错误处理来执行。我假设他们运行了playbook,并在任务失败时看到它失败。我从那里推断。你的“难以置信的坏习惯”评论毫无根据,特别是考虑到它包含在语言中。不是每个任务都声明一个状态…这不是SaltStack。如果您不在ansible 2.0上,则没有错误块。此外,还有许多实例需要忽略错误。如果您使用shell收集信息,这是一个完美的例子。任何非零退出状态都将为零,但这可能正是你在寻找(非零状态)来做出决定的原因。我并不是说使用它是非常糟糕的做法,尽管应该尽可能避免。我是说尽可能多地做这件事是不好的做法,这似乎是你说的“你可能永远都不希望[在发生错误时退出剧本]发生”所暗示的,此时,人们不太可能在2.0之前的版本上编写新的剧本。当任务失败后剧本失败已经是默认行为时,为什么要这样做?虽然剧本中没有包含它,但他可能很容易想在任务中包含一个“failed_when:False”,以便失败的任务继续。这将是一个原因。当任务失败时,您并不总是希望剧本失败。事实上,如果你把剧本设计得很好,你可能永远都不想让它发生。这与他所要求的完全相反,因为如果一项任务失败,他确实希望整个剧本都失败。这也是非常糟糕的做法。如果遇到故障,您几乎总是希望停止,因为这意味着不可能达到您声明的状态。如果失败后需要进行一些清理,则可以使用块中的错误处理来执行。我假设他们运行了playbook,并在任务失败时看到它失败。我从那里推断。你的“难以置信的坏习惯”评论毫无根据,特别是考虑到它包含在语言中。不是每个任务都声明一个状态…这不是SaltStack。如果您不在ansible 2.0上,则没有错误块。此外,还有许多实例需要忽略错误。如果您使用shell收集信息,这是一个完美的例子。任何非零退出状态都将为零,但这可能正是你在寻找(非零状态)来做出决定的原因。我并不是说使用它是非常糟糕的做法,尽管应该尽可能避免。我是说,尽可能多地这样做是一种不好的做法,这就是你说的“你可能永远不希望(发生错误时退出剧本)发生”所暗示的。此外,目前人们不太可能在2.0之前编写新剧本。