如何从kubernetes集群的ansible(register stdout)初始化中获取最后两行 [英] How to grab last two lines from ansible (register stdout) initialization of kubernetes cluster

查看:99
本文介绍了如何从kubernetes集群的ansible(register stdout)初始化中获取最后两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的剧本文件中的一个问题:

This is the piece of my playbook file for the question:

  - name: Initialize the Kubernetes cluster using kubeadm
    command: kubeadm init --config /etc/kubernetes/kubeadminit.yaml
    register: init_output

  - name: Copy join command to local file
    local_action: copy content={{ init_output.stdout }} dest="./join-command"

当前,join命令包含整个标准输出(30多个文本行).我要抓住的只是init_output.stdout的最后两行,而不是整个输出.我已经研究过使用索引引用(即init_output.stdout [#]),但是我不知道输出将始终是相同的长度,并且我不知道如何使用索引来抓取多行,但我可以肯定的是,最后两行将始终是join命令.有什么建议吗?

Currently join-command contains the entire stdout (30+ lines of text) for content. What I want to grab is just the last two lines of init_output.stdout instead of the entire output. I've looked into using index reference (ie. init_output.stdout[#]) but I don't know that the output will always be the same length and I don't know how to use indexes to grab more than one line, but i'm fairly certain that the last two lines will always be the join command. Any suggestions?

推荐答案

从列表 stdout_lines

- local_action: copy content={{ init_output.stdout_lines[-2:] }} dest="./join-command"


可以格式化块中的行.例如


It's possible to format the lines in a block. For example

    - local_action:
        module: copy
        content: |
          {{ init_output.stdout_lines[-2] }}
          {{ init_output.stdout_lines[-1] }}
        dest: "./join-command"

要在循环中附加行,请尝试

To append the lines in a loop try

    - local_action:
        module: lineinfile
        path: "./join-command"
        line: "{{ item }}"
        insertafter: EOF
        create: true
      loop: "{{ init_output.stdout_lines[-2:] }}"

这篇关于如何从kubernetes集群的ansible(register stdout)初始化中获取最后两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆