正在尝试从文件中获取IP并将其用作Ansible中的库存

正在尝试从文件中获取IP并将其用作Ansible中的库存,ansible,Ansible,我在test.text文件中获取IP地址列表,我正试图从中获取循环中的IP 然后尝试进入组或变量并将其用作主机(动态组) 下面是我的播放列表 --- - name: provision stack hosts: localhost connection: local gather_facts: no serial: 1 tasks: - name: Get Instance IP Addresses From File shell: cat /home/u

我在test.text文件中获取IP地址列表,我正试图从中获取循环中的IP 然后尝试进入组或变量并将其用作主机(动态组)

下面是我的播放列表

---
- 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:
     .....
     .....
在PlaybOK上运行后,我得到以下错误

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

它应该可以解决您的问题。

下面是使用变量的正确方法

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

它应该可以解决您的问题。

您只需使用ansible playbook-i inventory\u file\u name playbook.yaml即可。库存文件是包含您的组和IP的文件

您只需使用ansible playbook-i inventory\u file\u name playbook.yaml即可。库存文件是包含您的组和IP的文件

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

这是由于将变量传递给with_项时出错造成的。在您的任务中:

with_items: serverlist.stdout_lines
- name: Add Instance IP Addresses to temporary inventory groups
  add_host:
    groups: dynamic_groups
    hostname: "{{item}}"
  with_items: "{{ serverlist.stdout_lines }}"
它正在传递serverlist.stdout\u行字符串,而不是其值

With_项需要使用“{…}}”()进行变量定义

这是完成任务的正确方法:

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

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

这是由于将变量传递给with_项时出错造成的。在您的任务中:

with_items: serverlist.stdout_lines
- name: Add Instance IP Addresses to temporary inventory groups
  add_host:
    groups: dynamic_groups
    hostname: "{{item}}"
  with_items: "{{ serverlist.stdout_lines }}"
它正在传递serverlist.stdout\u行字符串,而不是其值

With_项需要使用“{…}}”()进行变量定义

这是完成任务的正确方法:

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

你确定每个数字都在不同的行中吗?>这是什么“一些命令”任务?因为这是它尝试执行ssh时的错误,所以您可能没有正确阅读。您确定每个数字都在不同的行中吗?>这是什么“Some Command”任务?因为这是它尝试执行ssh时的错误,所以您可能没有正确阅读是的,正如@Jaydeep之前建议的那样。谢谢。是的,这是@Jaydeep之前建议的。非常感谢。