Ansible:使用 stdout_lines 来自注册变量,它在 shell 模块中使用循环
Ansible: Use stdout_lines from registered variable which uses loop in shell module
想知道这里是否有人可以提供帮助...
我正在 运行ning Ansible 2.10.7 - 下面的 Ansible 任务负责检查以前的 Ansible 运行 是否存在任何 JBOSS 部署。
如果 check_deployments.stdout_lines (check_deployments.stdout_lines | length == 0) 没有返回任何内容,那么它应该部署循环中定义的所有部署。但是,如果返回 something (check_deployments.stdout_lines | length > 0) 那么它应该使用 check_deployments.stdout_lines内容(例如old_deployment1、old_deployment2、old_deployment3、old_deployment4)。
我目前 运行 将此 Ansible 与没有部署的新服务器结合使用,因此我希望由于 check_deployments.stdout_lines 为空而跳过取消部署部分。但是,它尝试 运行,但失败并出现以下错误:
TASK [app : Undeploy all existing deployments as the versions do not match deployments defined in group_vars] ************************************************************************
fatal: []: FAILED! => {"msg": "'dict object' has no attribute 'stdout_lines'"}
这是我调试的输出:
"check_deployments": {
"changed": true,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment1",
"delta": "0:00:02.081544",
"end": "2021-04-28 16:12:18.448965",
"failed": false,
"failed_when_result": false,
"invocation": {
"module_args": {
"_raw_params": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment1",
"_uses_shell": true,
"argv": null,
"chdir": "/opt/jboss/jboss-eap-7.1/bin",
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": "deployment1",
"msg": "non-zero return code",
"rc": 1,
"start": "2021-04-28 16:12:16.367421",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
},
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment2",
"delta": "0:00:02.185931",
"end": "2021-04-28 16:12:21.396740",
"failed": false,
"failed_when_result": false,
"invocation": {
"module_args": {
"_raw_params": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment2",
"_uses_shell": true,
"argv": null,
"chdir": "/opt/jboss/jboss-eap-7.1/bin",
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": "deployment2",
"msg": "non-zero return code",
"rc": 1,
"start": "2021-04-28 16:12:19.210809",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
},
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment3",
"delta": "0:00:01.938456",
"end": "2021-04-28 16:12:24.060703",
"failed": false,
"failed_when_result": false,
"invocation": {
"module_args": {
"_raw_params": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment3",
"_uses_shell": true,
"argv": null,
"chdir": "/opt/jboss/jboss-eap-7.1/bin",
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": "deployment3",
"msg": "non-zero return code",
"rc": 1,
"start": "2021-04-28 16:12:22.122247",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
},
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment4",
"delta": "0:00:01.934429",
"end": "2021-04-28 16:12:26.819414",
"failed": false,
"failed_when_result": false,
"invocation": {
"module_args": {
"_raw_params": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment4",
"_uses_shell": true,
"argv": null,
"chdir": "/opt/jboss/jboss-eap-7.1/bin",
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": "deployment4",
"msg": "non-zero return code",
"rc": 1,
"start": "2021-04-28 16:12:24.884985",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
]
}
}
这是我的 Ansible 任务:
- name: Check if the deployments defined in group_vars are already installed
ansible.builtin.shell: "./jboss-cli.sh --connect --commands='ls deployment' | grep -q {{ item }}"
args:
chdir: "{{ jboss_home_dir }}/bin"
register: check_deployments
failed_when: check_deployments.rc == 2
tags: # We always need to check this as the output determines whether or not we need to undeploy an existing deployment.
- skip_ansible_lint
loop:
- "{{ new_deployment1 }}"
- "{{ new_deployment2 }}"
- "{{ new_deployment3 }}"
- "{{ new_deployment4 }}"
- name: Show list of deployments
ansible.builtin.debug:
var: check_deployments.stdout_lines
- name: Undeploy all existing deployments as the versions do not match deployments defined in group_vars
ansible.builtin.command: "./jboss-cli.sh --connect 'undeploy {{ item }} --all-relevant-server-groups'"
args:
chdir: "{{ jboss_home_dir }}/bin"
when: check_deployments.stdout_lines | length > 0
register: check_undeployment_status
loop:
- "{{ check_deployments.stdout_lines }}"
- name: Deploy all deployments defined in group_vars
ansible.builtin.command: "./jboss-cli.sh --connect 'deploy {{ jboss_deployment_dir }}/{{ item }} --server-groups=dm-server-group'"
args:
chdir: "{{ jboss_home_dir }}/bin"
when: check_deployments.stdout_lines | length == 0 or check_undeployment_status.rc | default('') == 0
loop:
- "{{ new_deployment1 }}"
- "{{ new_deployment2 }}"
- "{{ new_deployment3 }}"
- "{{ new_deployment4 }}"
您的剧本和给定的输出有问题。当我构建一个没有所有依赖项的类似剧本时:
- name: "Check if the deployments defined in group_vars are already installed"
shell: "{{ item }}"
register: check_deployments
loop:
- "pwd"
- "ls -al"
- "pwd"
- name: "Show list of deployments"
debug:
var: check_deployments.stdout_lines
我正在获取信息
TASK [Show list of deployments] *************************************************
ok: [localhost] =>
check_deployments.stdout_lines: VARIABLE IS NOT DEFINED!
这是真的,因为您遍历了一个 Ansible 模块并将所有输出注册到一个变量中。在这种情况下,该变量包含一个结果列表。所以 - 你的 check_deployments.stdout_lines
循环是错误的。您需要遍历所有 check_deployments.results
,其中每个模块 运行 包含正确的“已注册”变量,您要检查
- name: "Undeploy..."
debug:
msg: "stdout: {{ item.cmd }}"
when: "item.stdout_lines | length > 1"
loop: "{{ check_deployments.results }}"
这只会输出一个条目(ls -al)。所有其他将被跳过。
想知道这里是否有人可以提供帮助...
我正在 运行ning Ansible 2.10.7 - 下面的 Ansible 任务负责检查以前的 Ansible 运行 是否存在任何 JBOSS 部署。
如果 check_deployments.stdout_lines (check_deployments.stdout_lines | length == 0) 没有返回任何内容,那么它应该部署循环中定义的所有部署。但是,如果返回 something (check_deployments.stdout_lines | length > 0) 那么它应该使用 check_deployments.stdout_lines内容(例如old_deployment1、old_deployment2、old_deployment3、old_deployment4)。
我目前 运行 将此 Ansible 与没有部署的新服务器结合使用,因此我希望由于 check_deployments.stdout_lines 为空而跳过取消部署部分。但是,它尝试 运行,但失败并出现以下错误:
TASK [app : Undeploy all existing deployments as the versions do not match deployments defined in group_vars] ************************************************************************
fatal: []: FAILED! => {"msg": "'dict object' has no attribute 'stdout_lines'"}
这是我调试的输出:
"check_deployments": {
"changed": true,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment1",
"delta": "0:00:02.081544",
"end": "2021-04-28 16:12:18.448965",
"failed": false,
"failed_when_result": false,
"invocation": {
"module_args": {
"_raw_params": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment1",
"_uses_shell": true,
"argv": null,
"chdir": "/opt/jboss/jboss-eap-7.1/bin",
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": "deployment1",
"msg": "non-zero return code",
"rc": 1,
"start": "2021-04-28 16:12:16.367421",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
},
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment2",
"delta": "0:00:02.185931",
"end": "2021-04-28 16:12:21.396740",
"failed": false,
"failed_when_result": false,
"invocation": {
"module_args": {
"_raw_params": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment2",
"_uses_shell": true,
"argv": null,
"chdir": "/opt/jboss/jboss-eap-7.1/bin",
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": "deployment2",
"msg": "non-zero return code",
"rc": 1,
"start": "2021-04-28 16:12:19.210809",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
},
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment3",
"delta": "0:00:01.938456",
"end": "2021-04-28 16:12:24.060703",
"failed": false,
"failed_when_result": false,
"invocation": {
"module_args": {
"_raw_params": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment3",
"_uses_shell": true,
"argv": null,
"chdir": "/opt/jboss/jboss-eap-7.1/bin",
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": "deployment3",
"msg": "non-zero return code",
"rc": 1,
"start": "2021-04-28 16:12:22.122247",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
},
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment4",
"delta": "0:00:01.934429",
"end": "2021-04-28 16:12:26.819414",
"failed": false,
"failed_when_result": false,
"invocation": {
"module_args": {
"_raw_params": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment4",
"_uses_shell": true,
"argv": null,
"chdir": "/opt/jboss/jboss-eap-7.1/bin",
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": "deployment4",
"msg": "non-zero return code",
"rc": 1,
"start": "2021-04-28 16:12:24.884985",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
]
}
}
这是我的 Ansible 任务:
- name: Check if the deployments defined in group_vars are already installed
ansible.builtin.shell: "./jboss-cli.sh --connect --commands='ls deployment' | grep -q {{ item }}"
args:
chdir: "{{ jboss_home_dir }}/bin"
register: check_deployments
failed_when: check_deployments.rc == 2
tags: # We always need to check this as the output determines whether or not we need to undeploy an existing deployment.
- skip_ansible_lint
loop:
- "{{ new_deployment1 }}"
- "{{ new_deployment2 }}"
- "{{ new_deployment3 }}"
- "{{ new_deployment4 }}"
- name: Show list of deployments
ansible.builtin.debug:
var: check_deployments.stdout_lines
- name: Undeploy all existing deployments as the versions do not match deployments defined in group_vars
ansible.builtin.command: "./jboss-cli.sh --connect 'undeploy {{ item }} --all-relevant-server-groups'"
args:
chdir: "{{ jboss_home_dir }}/bin"
when: check_deployments.stdout_lines | length > 0
register: check_undeployment_status
loop:
- "{{ check_deployments.stdout_lines }}"
- name: Deploy all deployments defined in group_vars
ansible.builtin.command: "./jboss-cli.sh --connect 'deploy {{ jboss_deployment_dir }}/{{ item }} --server-groups=dm-server-group'"
args:
chdir: "{{ jboss_home_dir }}/bin"
when: check_deployments.stdout_lines | length == 0 or check_undeployment_status.rc | default('') == 0
loop:
- "{{ new_deployment1 }}"
- "{{ new_deployment2 }}"
- "{{ new_deployment3 }}"
- "{{ new_deployment4 }}"
您的剧本和给定的输出有问题。当我构建一个没有所有依赖项的类似剧本时:
- name: "Check if the deployments defined in group_vars are already installed"
shell: "{{ item }}"
register: check_deployments
loop:
- "pwd"
- "ls -al"
- "pwd"
- name: "Show list of deployments"
debug:
var: check_deployments.stdout_lines
我正在获取信息
TASK [Show list of deployments] *************************************************
ok: [localhost] =>
check_deployments.stdout_lines: VARIABLE IS NOT DEFINED!
这是真的,因为您遍历了一个 Ansible 模块并将所有输出注册到一个变量中。在这种情况下,该变量包含一个结果列表。所以 - 你的 check_deployments.stdout_lines
循环是错误的。您需要遍历所有 check_deployments.results
,其中每个模块 运行 包含正确的“已注册”变量,您要检查
- name: "Undeploy..."
debug:
msg: "stdout: {{ item.cmd }}"
when: "item.stdout_lines | length > 1"
loop: "{{ check_deployments.results }}"
这只会输出一个条目(ls -al)。所有其他将被跳过。