user10373379
user10373379

Reputation: 215

Trying to get IPs from file and use it as Inventory in Ansible

I get list of IP address in test.text file from which I am trying to get the IP in loop and then try to get in group or variable and use it as hosts (dynamic_groups)

Below is my playlist

---
- 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:
     .....
     .....

After running above playbbok I am getting below error

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}

What Am I missing here?

Upvotes: 0

Views: 1207

Answers (3)

TinaC
TinaC

Reputation: 276

As reported in fatal error message "Failed to connect to the host via ssh: ssh: Could not resolve hostname serverlist.stdout_lines", it is trying to connect to "serverlist.stdout_lines", not to a valid IP.

This is caused by an error when passing variable to with_items. In your task:

with_items: serverlist.stdout_lines

it is passing serverlist.stdout_lines string and not its value.

With_items requires variable definition using "{{ ... }}" (https://docs.ansible.com/ansible/2.7/user_guide/playbooks_loops.html#with-items).

This is the correct way for your task:

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

Upvotes: 1

cognitive
cognitive

Reputation: 41

You can simply use ansible-playbook -i inventory_file_name playbook.yaml for this. inventory_file is the file containing your groups and ips.

Upvotes: 0

Jaydeep Chaudhari
Jaydeep Chaudhari

Reputation: 430

Below is correct way to use variable

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

It should solve your problem.

Upvotes: 1

Related Questions