我如何才能完成正确的剧本来获得我想要的信息

  • 本文关键字:本来 信息 我想要 ansible
  • 更新时间 :
  • 英文 :


我对ansible很陌生,所以我需要帮助来完成我的第一本ansible剧本我需要从日志目录下创建的文件中获取结果。我还需要输入容器名称,该名称被迁移以找到以容器命名的结果文件,并以1和3开头(需要2个文件的结果(找不到哪个部分出了问题。请帮帮我提前谢谢。

---
- name : find the results for migration ended
hosts: newmigservers
tasks:
- pause:
prompt: "what is the container name?"
echo: yes
register: result
- set_fact:
container: "{{ result.user_input }}"
- debug:
var: container
- name: find where the container is
shell: "grep -lr '{{ container }}'"
args:
chdir: "logs/"
register: grep_output
- name: cat 1,3 file for the results
command: cat {{ grep_output }}
register: results
PLAY [find the results for migration ended] *************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [1.1.1.1]
ok: [2.2.2.2]
ok: [3.3.3.3]

TASK [pause] ********************************************************************************************************************************************************************************************************************************
[pause]
what is the container name?:
ok: [1.1.1.1]

TASK [set_fact] *****************************************************************************************************************************************************************************************************************************
ok: [1.1.1.1]
fatal: [2.2.2.2]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'result' is undefinednnThe error appears to have been in '/home/amber/results.yml': line 9, column 7, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nn register: resultn - set_fact:n ^ heren"}
fatal: [3.3.3.3]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'result' is undefinednnThe error appears to have been in '/home/amber/results.yml': line 9, column 7, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nn register: resultn - set_fact:n ^ heren"}

TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [1.1.1.1] => {
"container": "amber_test_file_size.txt"
}

TASK [find where the container is] **********************************************************************************************************************************************************************************************************
skipping: [2.2.2.2]

TASK [cat 1,3 file for the results] *********************************************************************************************************************************************************************************************************
skipping: [3.3.3.3.]
to retry, use: --limit @/home/amber/results.retry

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
1.1.1.1 : ok=4 changed=0 unreachable=0 failed=0
2.2.2.2 : ok=1 changed=0 unreachable=0 failed=1
3.3.3.3 : ok=1 changed=0 unreachable=0 failed=1

Pause只为播放批次运行一次,导致只有批次中的第一个服务器(在我的情况下为1.1.1.1(选择变量。

你可以通过在你的剧本中添加序列来解决这个问题,如下所示:

---
- name : find the results for migration ended
hosts: newmigservers
serial: 1
tasks:
- pause:
prompt: "what is the container name?"
echo: yes
register: result
- set_fact:
container: "{{ result.user_input }}"
- debug:
var: container

最新更新