如何在ansible中保存stdout_lines的一部分/几行

时间:2018-06-26 09:37:13

标签: ansible ansible-2.x

我目前正在注册一个很大的输出,说像这样

我当前的剧本如下

---
- name: Initialize Kubernetes Master.
  shell: "kubeadm init --pod-network-cidr={{ flannel_pod_cidr }} --service-cidr {{ service_cidr }}"
  register: master_initialization

- debug:
    msg: "{{ master_initialization.stdout_lines }}"

,输出如下。

ok: [localhost] => {
    "msg": [
        "[init] Using Kubernetes version: v1.10.5", 
        "[init] Using Authorization modes: [Node RBAC]", 
        "[preflight] Running pre-flight checks.", 
        "[preflight] Starting the kubelet service", 
        "[certificates] Generated ca certificate and key.", 
        "[certificates] Generated apiserver certificate and key.", 
        "[certificates] apiserver serving cert is signed for DNS names [n0 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.3.120]", 
        "[certificates] Generated apiserver-kubelet-client certificate and key.", 
        "[certificates] Generated sa key and public key.", 
        "[certificates] Generated front-proxy-ca certificate and key.", 
        "[certificates] Generated front-proxy-client certificate and key.", 
        "[certificates] Generated etcd/ca certificate and key.", 
        "[certificates] Generated etcd/server certificate and key.", 
        "[certificates] etcd/server serving cert is signed for DNS names [localhost] and IPs [127.0.0.1]", 
        "[certificates] Generated etcd/peer certificate and key.", 
        "[certificates] etcd/peer serving cert is signed for DNS names [n0] and IPs [192.168.3.120]", 
        "[certificates] Generated etcd/healthcheck-client certificate and key.", 
        "[certificates] Generated apiserver-etcd-client certificate and key.", 
        "[certificates] Valid certificates and keys now exist in \"/etc/kubernetes/pki\"", 
        "[kubeconfig] Wrote KubeConfig file to disk: \"/etc/kubernetes/admin.conf\"", 
        "[kubeconfig] Wrote KubeConfig file to disk: \"/etc/kubernetes/kubelet.conf\"", 
        "[kubeconfig] Wrote KubeConfig file to disk: \"/etc/kubernetes/controller-manager.conf\"", 
        "[kubeconfig] Wrote KubeConfig file to disk: \"/etc/kubernetes/scheduler.conf\"", 
        "[controlplane] Wrote Static Pod manifest for component kube-apiserver to \"/etc/kubernetes/manifests/kube-apiserver.yaml\"", 
        "[controlplane] Wrote Static Pod manifest for component kube-controller-manager to \"/etc/kubernetes/manifests/kube-controller-manager.yaml\"", 
        "[controlplane] Wrote Static Pod manifest for component kube-scheduler to \"/etc/kubernetes/manifests/kube-scheduler.yaml\"", 
        "[etcd] Wrote Static Pod manifest for a local etcd instance to \"/etc/kubernetes/manifests/etcd.yaml\"", 
        "[init] Waiting for the kubelet to boot up the control plane as Static Pods from directory \"/etc/kubernetes/manifests\".", 
        "[init] This might take a minute or longer if the control plane images have to be pulled.", 
        "[apiclient] All control plane components are healthy after 24.502257 seconds", 
        "[uploadconfig] Storing the configuration used in ConfigMap \"kubeadm-config\" in the \"kube-system\" Namespace", 
        "[markmaster] Will mark node n0 as master by adding a label and a taint", 
        "[markmaster] Master n0 tainted and labelled with key/value: node-role.kubernetes.io/master=\"\"", 
        "[bootstraptoken] Using token: ggemtm.cybal0z01y1arkgi", 
        "[bootstraptoken] Configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials", 
        "[bootstraptoken] Configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token", 
        "[bootstraptoken] Configured RBAC rules to allow certificate rotation for all node client certificates in the cluster", 
        "[bootstraptoken] Creating the \"cluster-info\" ConfigMap in the \"kube-public\" namespace", 
        "[addons] Applied essential addon: kube-dns", 
        "[addons] Applied essential addon: kube-proxy", 
        "", 
        "Your Kubernetes master has initialized successfully!", 
        "", 
        "To start using your cluster, you need to run the following as a regular user:", 
        "", 
        "  mkdir -p $HOME/.kube", 
        "  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config", 
        "  sudo chown $(id -u):$(id -g) $HOME/.kube/config", 
        "", 
        "You should now deploy a pod network to the cluster.", 
        "Run \"kubectl apply -f [podnetwork].yaml\" with one of the options listed at:", 
        "  https://kubernetes.io/docs/concepts/cluster-administration/addons/", 
        "", 
        "You can now join any number of machines by running the following on each node", 
        "as root:", 
        "", 
        "  kubeadm join 192.168.3.120:6443 --token ggemtm.cybal0z01y1arkgi --discovery-token-ca-cert-hash sha256:36bf317da504025cfed2835d42f817ca83d056fedd12d9d6e1522d7d28a4bd65"
    ]
}

我想得到最后一行,即

"  kubeadm join 192.168.3.120:6443 --token ggemtm.cybal0z01y1arkgi --discovery-token-ca-cert-hash sha256:36bf317da504025cfed2835d42f817ca83d056fedd12d9d6e1522d7d28a4bd65"

如何仅获取此行并将其存储为变量,以便可以在其他地方使用它?

任何帮助都会很有用...

预先感谢

1 个答案:

答案 0 :(得分:0)

您可以通过编辑anisble.cfg或将环境变量ANISBLE_LOG_PATH={PATH}重定向到日志文件,然后从python解析该文件。稍后,您可以在group_vars中设置变量,并在ansible中使用它。 所有这些都可以在运行时使用python完成。

要进一步阅读,可以检查ansible模块lineinfileblockinfile ansible模块。

相关问题