Need help on building ansible inventory file properly

78 views
Skip to first unread message

Yehuda Pinhas

unread,
Dec 30, 2019, 3:19:47 AM12/30/19
to Ansible Project
Hi,
I have been searching all over the internet and there isn't any good documentation that explains how to build my inventory properly. The official documentation is bad and there aren't any example on inet that I could find.

Can you please help me? I need to build inventory file that contains 6 groups total and each group will have around 100 hosts in it..
this is what I currently have.

all:
  hosts:
    POC-ENV:
      hosts:
        ansible_host/home/ansible/MACCABI/hosts/POC-ENV_list.yml
    Nexus:
      hosts:
        ansible_host/home/ansible/MACCABI/hosts/nexus_list.yml
    Switches:
      hosts:
        ansible_host/home/ansible/MACCABI/hosts/switch_list.yml
    Avaya:
      hosts:
        ansible_host/home/ansible/MACCABI/avaya_list.yml
    Branch-Switches:
      hosts:
        ansible_host/home/ansible/MACCABI/branch-switch_list.yml
    Branch-Routers:
      hosts:
        ansible_host/home/ansible/MACCABI/branch-router_list.yml

As you can see there are 6 groups: POC-ENV, Nexus, Switches, Avaya, Branch-Switches, Branch-Routers and I am trying to list all the hosts in a file and direct the inventory to read it from there since I have so many.


What am I doing wrong? can you fix my code?
Thanks for the helpers.











Vladimir Botka

unread,
Dec 30, 2019, 4:05:44 AM12/30/19
to Yehuda Pinhas, ansible...@googlegroups.com
On Mon, 30 Dec 2019 00:19:47 -0800 (PST)
Yehuda Pinhas <yuda....@gmail.com> wrote:

> I need to build inventory file that contains 6 groups total and each group
> will have around 100 hosts in it.. this is what I currently have.
>
> all:
> hosts:
> POC-ENV:
> hosts:
> ansible_host: /home/ansible/MACCABI/hosts/POC-ENV_list.yml
> Nexus:
> hosts:
> ansible_host: /home/ansible/MACCABI/hosts/nexus_list.yml
> Switches:
> hosts:
> ansible_host: /home/ansible/MACCABI/hosts/switch_list.yml
> Avaya:
> hosts:
> ansible_host: /home/ansible/MACCABI/avaya_list.yml
> Branch-Switches:
> hosts:
> ansible_host: /home/ansible/MACCABI/branch-switch_list.yml
> Branch-Routers:
> hosts:
> ansible_host: /home/ansible/MACCABI/branch-router_list.yml

It's possible to create the groups with "add_host"
https://docs.ansible.com/ansible/latest/modules/add_host_module.html

For example, create the dictionary "my_hosts" with all groups and hosts, and
"loop subelements"
https://docs.ansible.com/ansible/latest/plugins/lookup/subelements.html#subelements-traverse-nested-key-from-a-list-of-dictionaries

- hosts: localhost
vars:
my_hosts:
- group: 'POC_ENV'
hosts: "{{ lookup('file',
'/home/ansible/MACCABI/hosts/POC-ENV_list.yml').splitlines() }}"
- group: 'Nexus'
hosts: "{{ lookup('file',
'/home/ansible/MACCABI/hosts/nexus_list.yml').splitlines() }}"
...
tasks:
- add_host:
name: "{{ item.1 }}"
groups: "{{ item.0.group }}"
loop: "{{ lookup('subelements', my_hosts, 'hosts') }}"

(not tested)

Then use the groups in the next plays.


Notes:

* See "How to build your inventory"
https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#how-to-build-your-inventory
* 'POC-ENV', 'Branch-Switches' and 'Branch-Routers' are not valid names of
the groups. See "Creating valid variable names"
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#creating-valid-variable-names
* See "Developing dynamic inventory"
https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html#developing-dynamic-inventory

Cheers,

-vlado

Vladimir Botka

unread,
Dec 30, 2019, 4:10:29 AM12/30/19
to Yehuda Pinhas, ansible...@googlegroups.com
On Mon, 30 Dec 2019 10:05:31 +0100
Vladimir Botka <vbo...@gmail.com> wrote:

> For example, create the dictionary "my_hosts"

Errata:

For example, create the list of dictionaries "my_hosts"

Yehuda Pinhas

unread,
Dec 30, 2019, 4:31:54 AM12/30/19
to Ansible Project
Hi again,
Thanks a lot for your reply.

I still don't understand what these code lines used for in my case:
    tasks:
      - add_host:
          name: "{{ item.1 }}"
          groups: "{{ item.0.group }}"
        loop: "{{ lookup('subelements', my_hosts, 'hosts') }}"

can you please explain?

I understand that you have wrote for me a lookup on the files mentioned for each lines which is exactly what I wanted. Now my intensions will be to copy and paste hosts of each environment to the appropriate host file.


Yehuda Pinhas

unread,
Dec 30, 2019, 5:07:25 AM12/30/19
to Ansible Project
Hi,
Can you please take a look on my errors and try to help me figuring out why I am getting them?

This is my inventory code:
  - hostslocalhost
    vars:
      my_hosts:
        - group'POC_ENV'
          hosts"{{ lookup('file',
                     '/home/ansible/MACCABI/hosts/POC_ENV.yml').splitlines() }}"
vars:
  ansible_ssh_private_key_file/home/ansible/.ssh/id_rsa
  ansible_ssh_common_args-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null
  ansible_useransible

This is my playbook code (task is to add a VLAN):
  namePlaying VLAN Configuration
  hostsPOC-ENV
  connectionlocal
  vars:
    vlan_id999
    vlan_nameTEST_VLAN_TEST

  tasks:
    - include_role:
        nameadd_vlan
        nameshow_vlan


This is the errors I get:

Ansible add vlans errors.PNG


Thanks!
















Jean-Yves LENHOF

unread,
Dec 30, 2019, 5:20:48 AM12/30/19
to ansible...@googlegroups.com

Hi,

It's difficult to understand what you want to do... Perhaps you should explain in plain english before trying to code it ?

In your code, there's a "hosts" directive in your inventory.... You should not have it in this file ("hosts" directive is for playbooks)

If you have multiple inventory, perhaps a way to select which inventory to select is to add an argument to your running playbook

ansible-playbook -i /home/ansible/MACCABI/hosts/POC_ENV.yml myplaybook.yml


Regards,

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/512c2e73-247b-400c-a6d9-fe5afb947d1f%40googlegroups.com.

Vladimir Botka

unread,
Dec 30, 2019, 5:41:23 AM12/30/19
to Yehuda Pinhas, ansible...@googlegroups.com
On Mon, 30 Dec 2019 01:31:54 -0800 (PST)
Yehuda Pinhas <yuda....@gmail.com> wrote:

> I still don't understand what these code lines used for in my case:
> tasks:
> - add_host:
> name: "{{ item.1 }}"
> groups: "{{ item.0.group }}"
> loop: "{{ lookup('subelements', my_hosts, 'hosts') }}"

Short answer: These code lines create the groups you want.

Details:

1) Quoting from "add_host"
https://docs.ansible.com/ansible/latest/modules/add_host_module.html

"Use variables to create new hosts and groups in inventory for use in later
plays of the same playbook."


2) For example, with this list of dictionaries

"my_hosts": [
{
"group": "POC_ENV",
"hosts": [
"POC_ENV_01",
"POC_ENV_02",
"POC_ENV_03"
]
},
{
"group": "Nexus",
"hosts": [
"nexus_01",
"nexus_02",
"nexus_03"
]
}
]


3) The debug task

- debug:
msg:
- "name: {{ item.1 }}"
- "groups: {{ item.0.group }}"
loop: "{{ lookup('subelements', my_hosts, 'hosts') }}"

gives output that explains the plugin "subelements"

"msg": [
"name: POC_ENV_01",
"groups: POC_ENV"
]
"msg": [
"name: POC_ENV_02",
"groups: POC_ENV"
]
"msg": [
"name: POC_ENV_03",
"groups: POC_ENV"
]
"msg": [
"name: nexus_01",
"groups: Nexus"
]
"msg": [
"name: nexus_02",
"groups: Nexus"
]
"msg": [
"name: nexus_03",
"groups: Nexus"
]


4) The add_host tasks

- add_host:
name: "{{ item.1 }}"
groups: "{{ item.0.group }}"
loop: "{{ lookup('subelements', my_hosts, 'hosts') }}"

create groups that can be used in the next plays. For example the play

- hosts: POC_ENV
tasks:
- debug:
var: inventory_hostname

gives

ok: [POC_ENV_01] => {
"inventory_hostname": "POC_ENV_01"
}
ok: [POC_ENV_02] => {
"inventory_hostname": "POC_ENV_02"
}
ok: [POC_ENV_03] => {
"inventory_hostname": "POC_ENV_03"
}

HTH,

-vlado

Vladimir Botka

unread,
Dec 30, 2019, 6:26:29 AM12/30/19
to Yehuda Pinhas, ansible...@googlegroups.com
On Mon, 30 Dec 2019 02:07:24 -0800 (PST)
Yehuda Pinhas <yuda....@gmail.com> wrote:

> Can you please take a look on my errors and try to help me figuring out why
> I am getting them?

Sure. As a working example, the playbook

- hosts: localhost
vars:

my_hosts:
- group: 'POC_ENV'
hosts: "{{ lookup('file',
'/home/ansible/MACCABI/hosts/POC-ENV_list.yml').splitlines() }}"
tasks:
- add_host:
name: "{{ item.1 }}"
groups: "{{ item.0.group }}"
ansible_ssh_private_key_file: /home/ansible/.ssh/id_rsa
ansible_ssh_common_args: -o StrictHostKeyChecking=no
-o UserKnownHostsFile=/dev/null
ansible_user: ansible
loop: "{{ lookup('subelements', my_hosts, 'hosts') }}"

- hosts: POC_ENV
tasks:
- debug:
msg: "{{ msg.split('\n') }}"
vars:
msg: |
host [{{ inventory_hostname }}]
key [{{ ansible_ssh_private_key_file}}]
args [{{ ansible_ssh_common_args }}]
user [{{ ansible_user }}]

gives

ok: [POC_ENV_01] => {
"msg": [
"host [POC_ENV_01]",
"key [/home/ansible/.ssh/id_rsa]",
"args [-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null]",
"user [ansible]",
""
]
}
ok: [POC_ENV_02] => {
"msg": [
"host [POC_ENV_02]",
"key [/home/ansible/.ssh/id_rsa]",
"args [-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null]",
"user [ansible]",
""
]
}
ok: [POC_ENV_03] => {
"msg": [
"host [POC_ENV_03]",
"key [/home/ansible/.ssh/id_rsa]",
"args [-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null]",
"user [ansible]",
""
]
}


Notes:

* In your code "hosts: POC-ENV" the name of the group is not valid.
* See "add_host" examples how to add variables to the inventory
https://docs.ansible.com/ansible/latest/modules/add_host_module.html#examples
* Run "ansible-lint playbook.yml" before posting the code

HTH,

-vlado

Vladimir Botka

unread,
Dec 30, 2019, 6:55:32 AM12/30/19
to Yehuda Pinhas, ansible...@googlegroups.com
On Mon, 30 Dec 2019 12:26:17 +0100
Vladimir Botka <vbo...@gmail.com> wrote:

> On Mon, 30 Dec 2019 02:07:24 -0800 (PST)
> Yehuda Pinhas <yuda....@gmail.com> wrote:
>
> - add_host:
> name: "{{ item.1 }}"
> groups: "{{ item.0.group }}"
> ansible_ssh_private_key_file: /home/ansible/.ssh/id_rsa
> ansible_ssh_common_args: -o StrictHostKeyChecking=no
> -o UserKnownHostsFile=/dev/null
> ansible_user: ansible
> loop: "{{ lookup('subelements', my_hosts, 'hosts') }}"

It's possible to put the variables into the "group_vars". For example

- add_host:
name: "{{ item.1 }}"
groups: "{{ item.0.group }}"
loop: "{{ lookup('subelements', my_hosts, 'hosts') }}"

with the group_vars below give the same results

$ cat group_vars/POC_ENV.yml
---
ansible_ssh_private_key_file: /home/ansible/.ssh/id_rsa
ansible_ssh_common_args: -o StrictHostKeyChecking=no
-o UserKnownHostsFile=/dev/null
ansible_user: admin

HTH,

-vlado

Yehuda Pinhas

unread,
Dec 30, 2019, 7:23:26 AM12/30/19
to Ansible Project
Alright so this is how my playbook looks like now:

  namePlaying VLAN Configuration
  hostslocalhost
#  connection: local
  vars:
    vlan_id999
    vlan_nameTEST_VLAN_TEST
    my_hosts:
      - group'POC_ENV'
          hosts"{{ lookup('file','/etc/ansible/ansible_inventories/POC_ENV.yml').splitlines() }}"
      - group'Nexus'
          hosts"{{ lookup('file','/etc/ansible/ansible_inventories/nexus.yml').splitlines() }}"
      - group'switch'
          hosts"{{ lookup('file','/etc/ansible/ansible_inventories/switch.yml').splitlines() }}"
      - group'avaya'
          hosts"{{ lookup('file','/etc/ansible/ansible_inventories/avaya.yml').splitlines() }}"
      - group'branch_switch'
          hosts"{{ lookup('file','/etc/ansible/ansible_inventories/branch_switch.yml').splitlines() }}"
      - group'branch_router'
          hosts"{{ lookup('file','/etc/ansible/ansible_inventories/branch_router.yml').splitlines() }}"
      - group'megalab'
          hosts"{{ lookup('file','/etc/ansible/ansible_inventories/megalab.yml').splitlines() }}"
      - group'DC_Jaffa'
          hosts"{{ lookup('file','/etc/ansible/ansible_inventories/DC_Jaffa.yml').splitlines() }}"
      - group'DC_PT'
          hosts"{{ lookup('file','/etc/ansible/ansible_inventories/DC_PT.yml').splitlines() }}"

tasks:
  - add_host:
      name"{{ item.1 }}"
      groups"{{ item.0.group }}"
      ansible_ssh_private_key_file/home/ansible/.ssh/id_rsa
      ansible_ssh_common_args-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null
      ansible_useransible
    loop"{{ lookup('subelements', my_hosts, 'hosts') }}"

  - hostsPOC_ENV
    tasks:
      - debug:
          msg"{{ msg.split('\n') }}"
        vars:
          msg|
            host [{{ inventory_hostname }}]
            key [{{ ansible_ssh_private_key_file}}]
            args [{{ ansible_ssh_common_args }}]
            user [{{ ansible_user }}]
      - include_role:
         nameshow_vlan

# Final result = run show_vlan on requested predefined group


These are the errors that i'm receiving when running the command: $ ansible-playbook myplaybook.yml

Ansible add vlans errors.PNG



Notes:
 - Please note that my ansible.cfg files directs to a inventory.yml file while I have many inventories (listed in the playbook code POC_ENV.yml, avaya.yml, etc.
This is the settings on ansible.cfg:
inventory      = /etc/ansible/inventory.yml



I'm not sure how to proceed,
Thanks in advance.





Vladimir Botka

unread,
Dec 30, 2019, 8:19:03 AM12/30/19
to Yehuda Pinhas, ansible...@googlegroups.com
On Mon, 30 Dec 2019 04:23:26 -0800 (PST)
Yehuda Pinhas <yuda....@gmail.com> wrote:

> vars:
> vlan_id: 999
> vlan_name: TEST_VLAN_TEST
> my_hosts:
> - group: 'POC_ENV'
> hosts:

Wrong indentation. Fix:

my_hosts:
- group: 'POC_ENV'
hosts:

HTH,

-vlado

Yehuda Pinhas

unread,
Dec 30, 2019, 8:39:28 AM12/30/19
to Ansible Project

Alright!
Thanks for your help so far, now it's working without errors.
Ignore previous messages and lets talk about the current status.

The result I get is:

Ansible add vlans errors.PNG



The result I'm expecting to see is that the ansible will run on each line inside POC_ENV.yml the task show_vlan.
The task show_vlan redirects to the "roles" folder where there is a show_vlan.yml file with the commands I want to execute on the remote host (from POC_ENV per line)
I want to store the result of the show_vlan.yml in a variable and print it.


This is the show_vlan.yml content:
    - nameShow VLAN
      ios_command:
        commandsshow vlan brief
      registershow_vlan

    - debugvar=show_vlan.stdout_lines

What am I missing next?















 

Vladimir Botka

unread,
Dec 30, 2019, 8:52:28 AM12/30/19
to Yehuda Pinhas, ansible...@googlegroups.com
On Mon, 30 Dec 2019 05:39:27 -0800 (PST)
Yehuda Pinhas <yuda....@gmail.com> wrote:

> The result I get is:
> [image: Ansible add vlans errors.PNG]

The output of the task "add_host" is weird. "item.1" is empty in couple
of lines or keeps the variables instead of the host's name.

(I hesitate to hope that it helps)

-vlado

J Hawkesworth

unread,
Dec 31, 2019, 4:15:13 AM12/31/19
to Ansible Project
I suggest using a directory that contains the files you need to build up your inventory.  You can also use symlinks I think
This is described in the documentation in the section starting 'Aggregating inventory sources with a directory' here:

I think this might be a simpler way to achieve what you want rather than using add_host and including lots of inventory files directly into your playbook.

It does depend on not having hostnames that clash of course.

Hope this helps,

Jon

Yehuda Pinhas

unread,
Dec 31, 2019, 5:13:25 AM12/31/19
to Ansible Project
Hi J Hawkesworth,
Can you explain exactly in which file do I need to add what code?

Considering I have the inventories POC_ENV.yml, avaya.yml, nexus.yml for example. What file do I need to edit? the ansible.cfg and the inventory file? can you provide the exact solution?

currently my ansible.cfg file directs to /etc/ansible/inventory.yml

and my inventory.yml is empty (which is the master inventory as i understand and from it I am supposed to direct ansible to each one of the inventories mentioned above such as POC_ENV.yml,etc.)





 J Hawkesworth:

J Hawkesworth

unread,
Dec 31, 2019, 5:24:40 AM12/31/19
to Ansible Project
I suggest doing this:

mkdir /etc/ansible/inventory
# copy POC_ENV.yml, avaya.yml, nexus.yml to /etc/ansible/inventory
changing your ansible.cfg so that it looks for inventory in

/etc/ansible/inventory

IIRC that will load all the inventory files in that directory

I have not tried this using .yaml format inventory file but it works fine using ini format so I suspect it will work ok using .yaml format inventory.

If you run
ansible-playbook -vvvvvv a_test_playbook.yml

the first few lines will show you where it is looking for inventory and what plugins it is using to parse and load your inventory.

Hope this helps,

Jon
Message has been deleted
Message has been deleted

Yehuda Pinhas

unread,
Dec 31, 2019, 9:22:37 AM12/31/19
to Ansible Project
Hi,
I have followed the instruction and it's working so my inventories are now working like I wanted. My end goal is to ssh to the specified machine in POC_ENV inventory (TEST-AGG-SW) and run the command "show vlan brief" and return it to stdout.
For some reason it is not working and im not even sure if my code runs the show vlan br but not post it to stdout or not sure if it even performs the SSH to the switch or not.

My configuration:

This is my playbook:
---
  - namePlaying VLAN Configuration
    hostsPOC_ENV
    connectionlocal
    vars:
      vlan_id999
      vlan_nameTEST_VLAN_TEST
    tasks:
    - include_role:
        namevlan_creation

# Final result = run show_vlan on requested predefined group

This is the inventory file that he take the switch TEST-AGG-SW from:
all:
  children:
    POC_ENV:
      hosts:
        TEST-AGG-SW

  vars:
    ansible_ssh_private_key_file/home/ansible/.ssh/id_rsa
    ansible_ssh_common_args-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null
    ansible_useransible



This is the role that is located on /etc/ansible/roles/vlan_creation/tasks/show_vlan.yml:
---
  - nameShow VLAN
    ios_command:
        commandsshow vlan brief
        registershow_vlan

  - debugmsg="{{ show_vlan.stdout }}"
Basically, all it does its the execute the show vlan brief command on the switch and register it in show_vlan variable and supposed to print it to my screen with stdout_lines.


The playbook result and the playbook result with -vvvvv is posted below this sentence.

[ansible@Netauto-Dev ansible]$  ansible-playbook playbook.yml       

PLAY [Playing VLAN Configuration] *********************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************
ok: [TEST-AGG-SW]

TASK [include_role : vlan_creation] *******************************************************************************************************

PLAY RECAP ********************************************************************************************************************************
TEST-AGG-SW                : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[ansible@Netauto-Dev ansible]$ 







[ansible@Netauto-Dev ansible]$  ansible-playbook -vvvvv playbook.yml
ansible-playbook 2.9.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/ansible/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.5 (default, Aug  7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
Using /etc/ansible/ansible.cfg as config file
setting up inventory plugins
host_list declined parsing /etc/ansible/inventory/DC_Jaffa.yml as it did not pass its verify_file() method
script declined parsing /etc/ansible/inventory/DC_Jaffa.yml as it did not pass its verify_file() method
Parsed /etc/ansible/inventory/DC_Jaffa.yml inventory source with ini plugin
setting up inventory plugins
host_list declined parsing /etc/ansible/inventory/DC_PT.yml as it did not pass its verify_file() method
script declined parsing /etc/ansible/inventory/DC_PT.yml as it did not pass its verify_file() method
Parsed /etc/ansible/inventory/DC_PT.yml inventory source with ini plugin
setting up inventory plugins
host_list declined parsing /etc/ansible/inventory/POC_ENV.yml as it did not pass its verify_file() method
script declined parsing /etc/ansible/inventory/POC_ENV.yml as it did not pass its verify_file() method
Parsed /etc/ansible/inventory/POC_ENV.yml inventory source with yaml plugin
setting up inventory plugins
host_list declined parsing /etc/ansible/inventory/avaya.yml as it did not pass its verify_file() method
script declined parsing /etc/ansible/inventory/avaya.yml as it did not pass its verify_file() method
Parsed /etc/ansible/inventory/avaya.yml inventory source with ini plugin
setting up inventory plugins
host_list declined parsing /etc/ansible/inventory/branch_router.yml as it did not pass its verify_file() method
script declined parsing /etc/ansible/inventory/branch_router.yml as it did not pass its verify_file() method
Parsed /etc/ansible/inventory/branch_router.yml inventory source with ini plugin
setting up inventory plugins
host_list declined parsing /etc/ansible/inventory/branch_switch.yml as it did not pass its verify_file() method
script declined parsing /etc/ansible/inventory/branch_switch.yml as it did not pass its verify_file() method
Parsed /etc/ansible/inventory/branch_switch.yml inventory source with ini plugin
setting up inventory plugins
host_list declined parsing /etc/ansible/inventory/megalab.yml as it did not pass its verify_file() method
script declined parsing /etc/ansible/inventory/megalab.yml as it did not pass its verify_file() method
Parsed /etc/ansible/inventory/megalab.yml inventory source with ini plugin
setting up inventory plugins
host_list declined parsing /etc/ansible/inventory/nexus.yml as it did not pass its verify_file() method
script declined parsing /etc/ansible/inventory/nexus.yml as it did not pass its verify_file() method
Parsed /etc/ansible/inventory/nexus.yml inventory source with ini plugin
setting up inventory plugins
host_list declined parsing /etc/ansible/inventory/switch.yml as it did not pass its verify_file() method
script declined parsing /etc/ansible/inventory/switch.yml as it did not pass its verify_file() method
Parsed /etc/ansible/inventory/switch.yml inventory source with ini plugin
Loading callback plugin default of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/default.pyc

PLAYBOOK: playbook.yml ********************************************************************************************************************
Positional arguments: playbook.yml
become_method: sudo
inventory: (u'/etc/ansible/inventory',)
forks: 5
tags: (u'all',)
verbosity: 5
connection: smart
timeout: 10
1 plays in playbook.yml

PLAY [Playing VLAN Configuration] *********************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************
task path: /etc/ansible/playbook.yml:2
<TEST-AGG-SW> ESTABLISH LOCAL CONNECTION FOR USER: ansible
<TEST-AGG-SW> EXEC /bin/sh -c 'echo ~ansible && sleep 0'
<TEST-AGG-SW> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ansible/.ansible/tmp/ansible-tmp-1577801505.27-201001816618347 `" && echo ansible-tmp-1577801505.27-201001816618347="` echo /home/ansible/.ansible/tmp/ansible-tmp-1577801505.27-201001816618347 `" ) && sleep 0'
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/basic.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/namespace.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/ansible_collector.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/default_collectors.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/__init__.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/common/text/formatters.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/common/validation.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/common/text/converters.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/pycompat24.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/common/__init__.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/common/text/__init__.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/common/process.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/parsing/convert_bool.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/common/_utils.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/common/_collections_compat.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/parsing/__init__.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/common/_json_compat.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/_text.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/common/sys_info.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/common/parameters.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/six/__init__.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/common/file.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/common/collections.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/distro/__init__.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/distro/_distro.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/collector.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/timeout.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/hardware/darwin.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/other/__init__.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/network/linux.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/ssh_pub_keys.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/network/aix.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/network/dragonfly.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/network/hurd.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/hardware/linux.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/__init__.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/network/darwin.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/date_time.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/hardware/dragonfly.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/hardware/hurd.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/lsb.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/hardware/hpux.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/apparmor.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/hardware/sunos.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/pkg_mgr.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/local.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/hardware/freebsd.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/virtual/netbsd.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/network/sunos.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/hardware/base.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/virtual/__init__.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/network/freebsd.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/virtual/hpux.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/hardware/netbsd.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/python.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/caps.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/virtual/dragonfly.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/env.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/network/netbsd.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/virtual/linux.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/chroot.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/platform.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/fips.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/hardware/__init__.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/network/hpux.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/virtual/openbsd.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/other/ohai.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/dns.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/service_mgr.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/virtual/base.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/virtual/sunos.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/hardware/aix.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/network/fc_wwn.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/cmdline.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/hardware/openbsd.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/network/base.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/network/iscsi.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/virtual/freebsd.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/network/openbsd.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/other/facter.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/distribution.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/network/nvme.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/selinux.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/system/user.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/network/__init__.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/sysctl.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/utils.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/network/generic_bsd.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/virtual/sysctl.py
Using module_utils file /usr/lib/python2.7/site-packages/ansible/module_utils/facts/compat.py
<TEST-AGG-SW> Attempting python interpreter discovery
<TEST-AGG-SW> EXEC /bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v '"'"'/usr/bin/python'"'"'; command -v '"'"'python3.7'"'"'; command -v '"'"'python3.6'"'"'; command -v '"'"'python3.5'"'"'; command -v '"'"'python2.7'"'"'; command -v '"'"'python2.6'"'"'; command -v '"'"'/usr/libexec/platform-python'"'"'; command -v '"'"'/usr/bin/python3'"'"'; command -v '"'"'python'"'"'; echo ENDFOUND && sleep 0'
<TEST-AGG-SW> EXEC /bin/sh -c '/usr/bin/python && sleep 0'
Using module file /usr/lib/python2.7/site-packages/ansible/modules/system/setup.py
<TEST-AGG-SW> PUT /home/ansible/.ansible/tmp/ansible-local-71896bKTy1p/tmpFpm9_h TO /home/ansible/.ansible/tmp/ansible-tmp-1577801505.27-201001816618347/AnsiballZ_setup.py
<TEST-AGG-SW> EXEC /bin/sh -c 'chmod u+x /home/ansible/.ansible/tmp/ansible-tmp-1577801505.27-201001816618347/ /home/ansible/.ansible/tmp/ansible-tmp-1577801505.27-201001816618347/AnsiballZ_setup.py && sleep 0'
<TEST-AGG-SW> EXEC /bin/sh -c '/usr/bin/python /home/ansible/.ansible/tmp/ansible-tmp-1577801505.27-201001816618347/AnsiballZ_setup.py && sleep 0'
<TEST-AGG-SW> EXEC /bin/sh -c 'rm -f -r /home/ansible/.ansible/tmp/ansible-tmp-1577801505.27-201001816618347/ > /dev/null 2>&1 && sleep 0'
ok: [TEST-AGG-SW]
META: ran handlers

TASK [include_role : vlan_creation] *******************************************************************************************************
task path: /etc/ansible/playbook.yml:9
META: ran handlers
META: ran handlers

PLAY RECAP ********************************************************************************************************************************
TEST-AGG-SW                : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[ansible@Netauto-Dev ansible]$ 




As you can see in the file it seems like the ansible runs successfully but not working, it must be one of the two scenarios:
1. The show vlan brief command result is not posted correctly to stdout
2. The show vlan brief command doesn't really run at all on the TEST-AGG-SW.

Finally I'll attach picture of ping proof that I can communicate with the TEST-AGG-SW:

ansible ping proof.PNG



What am I missing?

Thanks in advance!




J Hawkesworth

unread,
Dec 31, 2019, 4:03:27 PM12/31/19
to Ansible Project
Hi

I can see a couple of things that might need changing to get this to work:

Try changing your playbook so it looks like this:

---
 - name: Playing VLAN Configuration
   hosts: POC_ENV
   connection: local
   vars:
     vlan_id: 999
     vlan_name: TEST_VLAN_TEST
   tasks:
   - include_role:
       name: vlan_creation
       tasks_from
: show_vlan.yml


I think 'include_role' defaults to looking for a task file called 'main.yml' under tasks under the role subdir.

Secondly I think you might need to change the indentation in your tasks file so it looks like the following ( the 'register' directive isn't specific to 'ios_command' it can be applied to any module so its indentation needs to match the level of the module name):

---
  - nameShow VLAN
    ios_command:
       commandsshow vlan brief
    registershow_vlan

  - debugmsg="{{ show_vlan.stdout }}"

I hope that helps,

Jon
Message has been deleted

Yehuda Pinhas

unread,
Jan 2, 2020, 3:07:37 AM1/2/20
to Ansible Project
Hi Jon,
Thank you so much, it's working now.

Also, much thanks Vladimir for his assistance.

Take care.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages