使用 Ansible 对文件行进行可变化?
Variabalizing a file line with Ansible?
我希望将一个 ip 放入变量中以传递到模板中,不幸的是,某个地方在注册变量时出现问题?
- name: Get Controller[0] ip
command: "cat {{ controller0 }} |grep -A1 controller0 |tail -1 |awk '{print}'"
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}'
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}'"
with_file:
- "{{playbook_dir}}/../ssh.cfg"
register: controller
- debug: var=controller
想通了
- name: Get the file contents
shell: "echo -ne '{{ item }}' |grep -A1 controller0 |tail -1 |awk '{print}'"
with_file:
- "{{playbook_dir}}/../ssh.cfg"
register: controller
我希望将一个 ip 放入变量中以传递到模板中,不幸的是,某个地方在注册变量时出现问题?
- name: Get Controller[0] ip
command: "cat {{ controller0 }} |grep -A1 controller0 |tail -1 |awk '{print}'"
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}'
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}'"
with_file:
- "{{playbook_dir}}/../ssh.cfg"
register: controller
- debug: var=controller
想通了
- name: Get the file contents
shell: "echo -ne '{{ item }}' |grep -A1 controller0 |tail -1 |awk '{print}'"
with_file:
- "{{playbook_dir}}/../ssh.cfg"
register: controller