使用Ansible使文件行杂乱无章吗?

时间:2018-11-08 00:32:51

标签: ansible ansible-template

我想在变量中添加一个ip以便传递到模板中,不幸的是某处某个地方搞乱了注册变量?

- name: Get Controller[0] ip
  command: "cat {{ controller0 }} |grep -A1 controller0 |tail -1 |awk '{print$2}'"
  register: controller0
  with_file:
    - "{{ playbook_dir }}/../ssh.cfg"

- debug: var=controller0

错误

fatal: [100.24.12.41]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'controller0' is undefined

    The error appears to have been in '~/Terraform/terraform-kubernetes/ansible/roles/worker/tasks/main.yml': line 110, column 3, but may
    be elsewhere in the file depending on the exact syntax problem.

    The offending line appears to be:


    - name: Get the file contents
      ^ here
    "}

本地grep输出

$ cat ssh.cfg |grep -A1  controller0 |tail -1 |awk '{print$2}'
35.171.150.231 

模板是

[Unit]
Description=Kubernetes Kube Proxy
Documentation=https://github.com/GoogleCloudPlatform/kubernetes

[Service]
ExecStart=/usr/bin/kube-proxy \
  --master=https://{{ controller0 }}:6443 \
  --kubeconfig=/var/lib/kubelet/kubeconfig \
  --proxy-mode=iptables \
  --v=2

Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

编辑

- name: Get the file contents
  command: "echo -ne '{{ item }}' |grep -A1 controller0 |tail -1 |awk '{print$2}'"
  with_file:
    - "{{playbook_dir}}/../ssh.cfg"
  register: controller

- debug: var=controller

1 个答案:

答案 0 :(得分:0)

想通了

- name: Get the file contents
  shell: "echo -ne '{{ item }}' |grep -A1 controller0 |tail -1 |awk '{print$2}'"
  with_file:
    - "{{playbook_dir}}/../ssh.cfg"
  register: controller