ansible - 尝试从文件中获取 IP 并将其用作 Ansible 中的库存
问题描述
我在 test.text 文件中获取 IP 地址列表,我试图从中获取 IP 循环,然后尝试进入组或变量并将其用作主机(dynamic_groups)
下面是我的播放列表
---
- name: provision stack
hosts: localhost
connection: local
gather_facts: no
serial: 1
tasks:
- name: Get Instance IP Addresses From File
shell: cat /home/user/test.text
register: serverlist
- debug: msg={{ serverlist.stdout_lines }}
- name: Add Instance IP Addresses to temporary inventory groups
add_host:
groups: dynamic_groups
hostname: "{{item}}"
with_items: serverlist.stdout_lines
- hosts: dynamic_groups
become: yes
become_user: root
become_method: sudo
gather_facts: True
serial: 1
vars:
ansible_connection: "{{ connection_type }}"
ansible_ssh_user: "{{ ssh_user_name }}"
ansible_ssh_private_key_file: "{{ ssh_private_key_file }}"
tasks:
.....
.....
在 playbbok 上运行后,我得到以下错误
TASK [debug] *****************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
"192.168.1.10",
"192.168.1.11",
"192.168.1.50"
]
}
TASK [Add Instance IP Addresses to temporary inventory groups] ***************************************************************************************************************************************************************************
changed: [localhost] => (item=serverlist.stdout_lines)
PLAY [dynamic_groups] *********************************************************************************************************************************************************************************************************************
TASK [Some Command] **********************************************************************************************************************************************************************************************************************
fatal: [serverlist.stdout_lines]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname serverlist.stdout_lines: Name or service not known", "unreachable": true}
我在这里想念什么?
解决方案
以下是使用变量的正确方法
- name: Add Instance IP Addresses to temporary inventory groups
add_host:
groups: working_hosts
hostname: "{{item}}"
with_items: "{{ serverlist.stdout_lines }}"
它应该可以解决您的问题。
推荐阅读
- javascript - 使用 react-transition-group 退出和进入动画的问题
- typescript - 在不重复的情况下弃用枚举值
- python - Python DataFrame df.to_json(orient='split') - 不维护列标签的顺序
- angular - 如何在 Ionic 3 中重置 Ion-Select 表单域?
- c - 防止字符串化输出中的空格
- random - 从具有概率的列表中选择 X 个元素
- mysql - 我可以将用户输入语言转换为数据库的默认排序规则吗?
- java - 如何更改进度条状态
- google-apps-script - 如何转置然后拆分数据(带标题)
- c++ - Qt、CMake 和 Visual Studio 行为