尝试从文件中获取 IP 并将其用作 Ansible 中的清单

  • 本文关键字: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:
     .....
     .....

在运行上面播放后,我得到以下错误

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 }}"

它应该解决你的问题。

如致命错误消息"无法通过 ssh 连接到主机:ssh:无法解析主机名serverlist.stdout_lines"中报告,它正在尝试连接到"serverlist.stdout_lines",而不是连接到有效的 IP。

这是由将变量传递给with_items时出错引起的。在您的任务中:

with_items: serverlist.stdout_lines

它传递的是字符串serverlist.stdout_lines而不是其值。

With_items需要使用"{{ ... }}"进行变量定义(https://docs.ansible.com/ansible/2.7/user_guide/playbooks_loops.html#with-items(。

这是执行任务的正确方法:

- name: Add Instance IP Addresses to temporary inventory groups
  add_host:
    groups: dynamic_groups
    hostname: "{{item}}"
  with_items: "{{ serverlist.stdout_lines }}"

你可以简单地使用ansible-playbook -i inventory_file_name playbook.yaml。 inventory_file是包含您的组和 IP 的文件。

最新更新