ansible - 失败! => {"msg": "'failed' 不是调试中的有效选项"}
ansible - FAILED! => {"msg": "'failed' is not a valid option in debug"}
在下面的代码中:
---
- name: set task definition facts
set_fact:
todobackend_task_def_arn:
"{{ cf_stack.stack_outputs.TodobackendTaskDefinition | regex_replace('^(.*):[\d]*$', '\1') }}"
todobackend_adhoc_task_def_arn:
"{{ cf_stack.stack_outputs.TodobackendAdhocTaskDefinition | regex_replace('^(.*):[\d]*$', '\1') }}"
- name: update task definition
aws_ecs_taskdefinition:
state: update
arn: "{{ item }}"
containers:
- name: todobackend
image: "{{ image_tag }}"
with_items:
- "{{ todobackend_task_def_arn }}"
- "{{ todobackend_adhoc_task_def_arn }}"
when: image_tag is defined
- name: run migrations
aws_ecs_task:
operation: run
cluster: "{{ cf_stack.stack_outputs.EcsCluster }}"
task_definition: "{{ todobackend_adhoc_task_def_arn }}"
count: 1
overrides:
containerOverrides:
- name: todobackend
command:
- "manage.py"
- "migrate"
- "--noinput"
register: migration_task
when: ('migrate' in ecs_tasks | default([]))
- block:
- debug: msg= {{ migration_task }}
when: debug is defined
- name: "fail if migration task failed"
fail: msg="One or more migration tasks exited with non-zero exit code"
with_items: "{{ migration_task.task | default([])}}"
when: item.containers[0].exitCode != 0
when: migration_task is defined
- name: run collectstatic
aws_ecs_task:
operation: run
cluster: "{{ cf_stack.stack_outputs.EcsCluster }}"
task_definition: "{{ todobackend_adhoc_task_def_arn }}"
count: "{{ instance_count | default(1) }}"
overrides:
containerOverrides:
- name: todobackend
command:
- "manage.py"
- "migrate"
- "--noinput"
register: collectstatic_task
when: ('collectstatic' in ecs_tasks | default([]))
- block:
- debug: msg= {{ collectstatic_task }}
when: debug is defined
- name: "fail if collectstatic task failed"
fail: msg="One or more collectstatic tasks exited with non-zero exit code"
with_items: "{{ collectstatic_task.task | default([])}}"
when: item.containers[0].exitCode != 0
when: collectstatic_task is defined
- name: configure ecs service
aws_ecs_service:
state: update
name: "{{ cf_stack.stack_outputs.TodobackendService }}"
cluster: "{{ cf_stack.stack_outputs.EcsCluster }}"
task_definition: "{{ todobackend_task_def_arn }}"
desired_count: "{{ instance_count | default(1) }}"
deployment_config:
minimumHealthyPercent: 50
maximumPercent: 200
register: configure_ecs_service
when: stack_config is defined
- debug: msg={{ configure_ecs_service }}
when: configure_ecs_service is defined and debug is defined
- name: deploy service update
aws_ecs_service:
state: update
name: "{{ cf_stack.stack_outputs.TodobackendService }}"
cluster: "{{ cf_stack.stack_outputs.EcsCluster }}"
task_definition: "{{ todobackend_task_def_arn }}"
register: update_ecs_service
when: image_tag is defined
- debug: msg={{ update_ecs_service }}
when: update_ecs_service is defined and debug is defined
错误如下:
TASK [set task definition facts] **************************************************************************************************************
ok: [localhost]
TASK [update task definition] *****************************************************************************************************************
skipping: [localhost] => (item=arn:aws:ecs:us-west-2:738712609768:task-definition/todobackend-TodobackendTaskDefinition-1Q6N008BB5S3E)
skipping: [localhost] => (item=arn:aws:ecs:us-west-2:738712609768:task-definition/todobackend-TodobackendAdhocTaskDefinition-XRWUXMCWSD4A)
TASK [run migrations] *************************************************************************************************************************
changed: [localhost]
TASK [debug] **********************************************************************************************************************************
[DEPRECATION WARNING]: Using variables for task params is unsafe, especially if the variables come from an external source like facts. This
feature will be removed in version 2.6. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
fatal: [localhost]: FAILED! => {"msg": "'failed' is not a valid option in debug"}
to retry, use: --limit @/home/user/git/../todobackend-deploy/site.retry
PLAY RECAP ************************************************************************************************************************************
localhost : ok=4 changed=2 unreachable=0 failed=1
$
命令:
$ ansible-playbook site.yml --ask-vault-pass -e '{"ecs_tasks":["migrate","collectstatic"],"stack_config":"true","debug":"true"}'
$ ansible --version
ansible 2.5.1
编辑:
回答后修改如下:
tasks/create_stack.yaml
---
- name: task to create/update stack
cloudformation:
stack_name: todobackend
state: present
template: templates/stack.yml
template_format: yaml
template_parameters:
VpcId: "{{ vpc_id }}"
SubnetId: "{{ subnet_id }}"
KeyPair: "{{ ec2_keypair }}"
InstanceCount: "{{ instance_count | default(1) }}"
DbSubnets: "{{ db_subnets | join(',') }}"
DbAvailabilityZone: "{{ db_availability_zone }}"
DbUsername: "{{ db_username }}"
DbPassword: "{{ db_password }}"
tags:
Environment: test
register: cf_stack
- name: Debug output
debug:
msg: "{{ cf_stack }}"
when: debug is defined
tasks/deploy_app.yml
---
- name: set task definition facts
set_fact:
todobackend_task_def_arn:
"{{ cf_stack.stack_outputs.TodobackendTaskDefinition | regex_replace('^(.*):[\d]*$', '\1') }}"
todobackend_adhoc_task_def_arn:
"{{ cf_stack.stack_outputs.TodobackendAdhocTaskDefinition | regex_replace('^(.*):[\d]*$', '\1') }}"
- name: update task definition
aws_ecs_taskdefinition:
state: update
arn: "{{ item }}"
containers:
- name: todobackend
image: "{{ image_tag }}"
with_items:
- "{{ todobackend_task_def_arn }}"
- "{{ todobackend_adhoc_task_def_arn }}"
when: image_tag is defined
- name: run migrations
aws_ecs_task:
operation: run
cluster: "{{ cf_stack.stack_outputs.EcsCluster }}"
task_definition: "{{ todobackend_adhoc_task_def_arn }}"
count: 1
overrides:
containerOverrides:
- name: todobackend
command:
- "manage.py"
- "migrate"
- "--noinput"
register: migration_task
when: ('migrate' in ecs_tasks | default([]))
- block:
- name: Debug migration task
debug:
msg: "{{ migration_task }}"
when: debug is defined
- name: "fail if migration task failed"
fail: msg="One or more migration tasks exited with non-zero exit code"
with_items: "{{ migration_task.task | default([])}}"
when: item.containers[0].exitCode != 0
when: migration_task is defined
- name: run collectstatic
aws_ecs_task:
operation: run
cluster: "{{ cf_stack.stack_outputs.EcsCluster }}"
task_definition: "{{ todobackend_adhoc_task_def_arn }}"
count: "{{ instance_count | default(1) }}"
overrides:
containerOverrides:
- name: todobackend
command:
- "manage.py"
- "migrate"
- "--noinput"
register: collectstatic_task
when: ('collectstatic' in ecs_tasks | default([]))
- block:
- name: Debug collect static task
debug:
msg: "{{ collectstatic_task }}"
when: debug is defined
- name: "fail if collectstatic task failed"
fail: msg="One or more collectstatic tasks exited with non-zero exit code"
with_items: "{{ collectstatic_task.task | default([])}}"
when: item.containers[0].exitCode != 0
when: collectstatic_task is defined
- name: configure ecs service
aws_ecs_service:
state: update
name: "{{ cf_stack.stack_outputs.TodobackendService }}"
cluster: "{{ cf_stack.stack_outputs.EcsCluster }}"
task_definition: "{{ todobackend_task_def_arn }}"
desired_count: "{{ instance_count | default(1) }}"
deployment_config:
minimumHealthyPercent: 50
maximumPercent: 200
register: configure_ecs_service
when: stack_config is defined
- debug:
msg: "{{ configure_ecs_service }}"
when: configure_ecs_service is defined and debug is defined
- name: deploy service update
aws_ecs_service:
state: update
name: "{{ cf_stack.stack_outputs.TodobackendService }}"
cluster: "{{ cf_stack.stack_outputs.EcsCluster }}"
task_definition: "{{ todobackend_task_def_arn }}"
register: update_ecs_service
when: image_tag is defined
- debug:
msg: "{{ update_ecs_service }}"
when: update_ecs_service is defined and debug is defined
如何解决错误:“'failed' 不是调试中的有效选项”?
这可能是您的(非常!!!)旧的 ansible 版本与旧的 k=v
+ 较新的 yaml 语法在您的任务中的混合。我无法在 ansible 2.9.7 上重现这个。
以下语法应该可以解决您的问题:
- debug:
msg: "{{ update_ecs_service }}"
when: update_ecs_service is defined and debug is defined
请注意,由于您是直接调试 var 值,因此您还可以使用:
- debug:
var: update_ecs_service
when: update_ecs_service is defined and debug is defined
虽然以上是一个很好的做法,但我仍然强烈建议您将 ansible 升级到更新的版本。
旁注:debug is defined
通常可以替换为 verbosity
parameter 至 debug
,因此您可以使用 -v
至 [=17] 选项控制输出或不输出=].
在下面的代码中:
---
- name: set task definition facts
set_fact:
todobackend_task_def_arn:
"{{ cf_stack.stack_outputs.TodobackendTaskDefinition | regex_replace('^(.*):[\d]*$', '\1') }}"
todobackend_adhoc_task_def_arn:
"{{ cf_stack.stack_outputs.TodobackendAdhocTaskDefinition | regex_replace('^(.*):[\d]*$', '\1') }}"
- name: update task definition
aws_ecs_taskdefinition:
state: update
arn: "{{ item }}"
containers:
- name: todobackend
image: "{{ image_tag }}"
with_items:
- "{{ todobackend_task_def_arn }}"
- "{{ todobackend_adhoc_task_def_arn }}"
when: image_tag is defined
- name: run migrations
aws_ecs_task:
operation: run
cluster: "{{ cf_stack.stack_outputs.EcsCluster }}"
task_definition: "{{ todobackend_adhoc_task_def_arn }}"
count: 1
overrides:
containerOverrides:
- name: todobackend
command:
- "manage.py"
- "migrate"
- "--noinput"
register: migration_task
when: ('migrate' in ecs_tasks | default([]))
- block:
- debug: msg= {{ migration_task }}
when: debug is defined
- name: "fail if migration task failed"
fail: msg="One or more migration tasks exited with non-zero exit code"
with_items: "{{ migration_task.task | default([])}}"
when: item.containers[0].exitCode != 0
when: migration_task is defined
- name: run collectstatic
aws_ecs_task:
operation: run
cluster: "{{ cf_stack.stack_outputs.EcsCluster }}"
task_definition: "{{ todobackend_adhoc_task_def_arn }}"
count: "{{ instance_count | default(1) }}"
overrides:
containerOverrides:
- name: todobackend
command:
- "manage.py"
- "migrate"
- "--noinput"
register: collectstatic_task
when: ('collectstatic' in ecs_tasks | default([]))
- block:
- debug: msg= {{ collectstatic_task }}
when: debug is defined
- name: "fail if collectstatic task failed"
fail: msg="One or more collectstatic tasks exited with non-zero exit code"
with_items: "{{ collectstatic_task.task | default([])}}"
when: item.containers[0].exitCode != 0
when: collectstatic_task is defined
- name: configure ecs service
aws_ecs_service:
state: update
name: "{{ cf_stack.stack_outputs.TodobackendService }}"
cluster: "{{ cf_stack.stack_outputs.EcsCluster }}"
task_definition: "{{ todobackend_task_def_arn }}"
desired_count: "{{ instance_count | default(1) }}"
deployment_config:
minimumHealthyPercent: 50
maximumPercent: 200
register: configure_ecs_service
when: stack_config is defined
- debug: msg={{ configure_ecs_service }}
when: configure_ecs_service is defined and debug is defined
- name: deploy service update
aws_ecs_service:
state: update
name: "{{ cf_stack.stack_outputs.TodobackendService }}"
cluster: "{{ cf_stack.stack_outputs.EcsCluster }}"
task_definition: "{{ todobackend_task_def_arn }}"
register: update_ecs_service
when: image_tag is defined
- debug: msg={{ update_ecs_service }}
when: update_ecs_service is defined and debug is defined
错误如下:
TASK [set task definition facts] **************************************************************************************************************
ok: [localhost]
TASK [update task definition] *****************************************************************************************************************
skipping: [localhost] => (item=arn:aws:ecs:us-west-2:738712609768:task-definition/todobackend-TodobackendTaskDefinition-1Q6N008BB5S3E)
skipping: [localhost] => (item=arn:aws:ecs:us-west-2:738712609768:task-definition/todobackend-TodobackendAdhocTaskDefinition-XRWUXMCWSD4A)
TASK [run migrations] *************************************************************************************************************************
changed: [localhost]
TASK [debug] **********************************************************************************************************************************
[DEPRECATION WARNING]: Using variables for task params is unsafe, especially if the variables come from an external source like facts. This
feature will be removed in version 2.6. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
fatal: [localhost]: FAILED! => {"msg": "'failed' is not a valid option in debug"}
to retry, use: --limit @/home/user/git/../todobackend-deploy/site.retry
PLAY RECAP ************************************************************************************************************************************
localhost : ok=4 changed=2 unreachable=0 failed=1
$
命令:
$ ansible-playbook site.yml --ask-vault-pass -e '{"ecs_tasks":["migrate","collectstatic"],"stack_config":"true","debug":"true"}'
$ ansible --version
ansible 2.5.1
编辑:
回答后修改如下:
tasks/create_stack.yaml
---
- name: task to create/update stack
cloudformation:
stack_name: todobackend
state: present
template: templates/stack.yml
template_format: yaml
template_parameters:
VpcId: "{{ vpc_id }}"
SubnetId: "{{ subnet_id }}"
KeyPair: "{{ ec2_keypair }}"
InstanceCount: "{{ instance_count | default(1) }}"
DbSubnets: "{{ db_subnets | join(',') }}"
DbAvailabilityZone: "{{ db_availability_zone }}"
DbUsername: "{{ db_username }}"
DbPassword: "{{ db_password }}"
tags:
Environment: test
register: cf_stack
- name: Debug output
debug:
msg: "{{ cf_stack }}"
when: debug is defined
tasks/deploy_app.yml
---
- name: set task definition facts
set_fact:
todobackend_task_def_arn:
"{{ cf_stack.stack_outputs.TodobackendTaskDefinition | regex_replace('^(.*):[\d]*$', '\1') }}"
todobackend_adhoc_task_def_arn:
"{{ cf_stack.stack_outputs.TodobackendAdhocTaskDefinition | regex_replace('^(.*):[\d]*$', '\1') }}"
- name: update task definition
aws_ecs_taskdefinition:
state: update
arn: "{{ item }}"
containers:
- name: todobackend
image: "{{ image_tag }}"
with_items:
- "{{ todobackend_task_def_arn }}"
- "{{ todobackend_adhoc_task_def_arn }}"
when: image_tag is defined
- name: run migrations
aws_ecs_task:
operation: run
cluster: "{{ cf_stack.stack_outputs.EcsCluster }}"
task_definition: "{{ todobackend_adhoc_task_def_arn }}"
count: 1
overrides:
containerOverrides:
- name: todobackend
command:
- "manage.py"
- "migrate"
- "--noinput"
register: migration_task
when: ('migrate' in ecs_tasks | default([]))
- block:
- name: Debug migration task
debug:
msg: "{{ migration_task }}"
when: debug is defined
- name: "fail if migration task failed"
fail: msg="One or more migration tasks exited with non-zero exit code"
with_items: "{{ migration_task.task | default([])}}"
when: item.containers[0].exitCode != 0
when: migration_task is defined
- name: run collectstatic
aws_ecs_task:
operation: run
cluster: "{{ cf_stack.stack_outputs.EcsCluster }}"
task_definition: "{{ todobackend_adhoc_task_def_arn }}"
count: "{{ instance_count | default(1) }}"
overrides:
containerOverrides:
- name: todobackend
command:
- "manage.py"
- "migrate"
- "--noinput"
register: collectstatic_task
when: ('collectstatic' in ecs_tasks | default([]))
- block:
- name: Debug collect static task
debug:
msg: "{{ collectstatic_task }}"
when: debug is defined
- name: "fail if collectstatic task failed"
fail: msg="One or more collectstatic tasks exited with non-zero exit code"
with_items: "{{ collectstatic_task.task | default([])}}"
when: item.containers[0].exitCode != 0
when: collectstatic_task is defined
- name: configure ecs service
aws_ecs_service:
state: update
name: "{{ cf_stack.stack_outputs.TodobackendService }}"
cluster: "{{ cf_stack.stack_outputs.EcsCluster }}"
task_definition: "{{ todobackend_task_def_arn }}"
desired_count: "{{ instance_count | default(1) }}"
deployment_config:
minimumHealthyPercent: 50
maximumPercent: 200
register: configure_ecs_service
when: stack_config is defined
- debug:
msg: "{{ configure_ecs_service }}"
when: configure_ecs_service is defined and debug is defined
- name: deploy service update
aws_ecs_service:
state: update
name: "{{ cf_stack.stack_outputs.TodobackendService }}"
cluster: "{{ cf_stack.stack_outputs.EcsCluster }}"
task_definition: "{{ todobackend_task_def_arn }}"
register: update_ecs_service
when: image_tag is defined
- debug:
msg: "{{ update_ecs_service }}"
when: update_ecs_service is defined and debug is defined
如何解决错误:“'failed' 不是调试中的有效选项”?
这可能是您的(非常!!!)旧的 ansible 版本与旧的 k=v
+ 较新的 yaml 语法在您的任务中的混合。我无法在 ansible 2.9.7 上重现这个。
以下语法应该可以解决您的问题:
- debug:
msg: "{{ update_ecs_service }}"
when: update_ecs_service is defined and debug is defined
请注意,由于您是直接调试 var 值,因此您还可以使用:
- debug:
var: update_ecs_service
when: update_ecs_service is defined and debug is defined
虽然以上是一个很好的做法,但我仍然强烈建议您将 ansible 升级到更新的版本。
旁注:debug is defined
通常可以替换为 verbosity
parameter 至 debug
,因此您可以使用 -v
至 [=17] 选项控制输出或不输出=].