我怎样才能正确完成ansible playbook来获取我想要的信息

how can I complete correctly ansible playbook to get info what i want

我是 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 undefined\n\nThe error appears to have been in '/home/amber/results.yml': line 9, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n register: result\n - set_fact:\n ^ here\n"}

fatal: [3.3.3.3]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'result' is undefined\n\nThe error appears to have been in '/home/amber/results.yml': line 9, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n register: result\n - set_fact:\n ^ here\n"}

 

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

暂停只为播放批次运行一次,导致批次中只有第一个服务器(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