cancel
Showing results for 
Search instead for 
Did you mean: 
Post a Question
Employee
Employee

Use a list of hosts in an Ansible Playbook

I need to add a lot of hosts to an R80.10 Mgmt and I don't want to have to write the add-host command over and over again. I've created a list of hosts with the parameters name, ip-address, and color in it. 

Example:

{name: host1, ip-address: 1.1.1.1, color: yellow}
{name: host2, ip-address: 1.1.1.2, color: yellow}
{name: host3, ip-address: 1.1.1.3, color: yellow}

How can I use this in an ansible playbook?

Tags (2)
0 Kudos
3 Replies

Re: Use a list of hosts in an Ansible Playbook

Have you tried importing .csv? you can include just about everything you need.

Employee
Employee

Re: Use a list of hosts in an Ansible Playbook

I'm hoping to use an Ansible playbook and use a YAML inventory file. If I use a .csv and use the mgmt_cli commands then I have to transfer that .csv to the management first. I want to avoid having any unnecessary files on the management server. 

So far here is what my variables file looks like:

---
hosts:
- {host_name: "host1", ip_address: "1.1.1.1", host_color: "yellow"}
- {host_name: "host2", ip_address: "1.1.1.2", host_color: "yellow"}

And here is the relevant section from my playbook for loading the variables file and then running adding the hosts:

- name: "Adding host variables"
  include_vars:
     file: hosts.yml
     name: hosts_file

- name: "Adding hosts to mgmt database"
  check_point_mgmt:
  session-data: "{{login_response}}"
  command: add-host
  parameters:
      name: "{{item.host_name}}"
      ip-address: "{{item.ip_address}}"
      color: "{{item.host_color}}"
  with_items:
  - "{{hosts_file}}"

When I run that playbook I get the following error:

TASK [Adding host variables] ***************************************************
ok: [127.0.0.1]

TASK [Adding hosts to mgmt database] *******************************************
fatal: [127.0.0.1]: FAILED! => {"failed": true, "msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'color'\n\nThe error appears to have been in '/home/jesmith4/ansible/Playbooks/test_hosts.yml': line 22, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: \"Adding hosts to mgmt database\"\n ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'dict object' has no attribute 'host_color'"}

I don't believe it's loading the variables properly. I've been scouring the internet looking for an answer but no luck yet. If anyone has any ideas on how to make it work let me know and I'll give it a try.

0 Kudos
Employee
Employee

Re: Use a list of hosts in an Ansible Playbook

SOLVED:

I was able to solve it.

Here is my variable file syntax (hosts.yml):

---
host_table:
- name: first
  host_name: host1
  ip_address: 1.1.1.1
  color: yellow
- name: second
  host_name: host2
  ip_address: 1.1.1.2
  color: yellow

I load the variable files at the beginning of the playbook:

- hosts: "localhost"
  gather_facts: no
  vars_files:
  - vars.yml
  - hosts.yml

And here is the playbook section for adding the hosts:

- name: "Adding hosts to mgmt database"

  check_point_mgmt:
     session-data: "{{login_response}}"
     command: add-host
     parameters:
        name: "{{item.host_name}}"
        ip-address: "{{item.ip_address}}"
        color: "{{item.color}}"
  with_items: "{{host_table}}"