Ansible with JSON hosts file

25 views
Skip to first unread message

Mahesh N

unread,
Jul 21, 2020, 5:22:56 PM7/21/20
to Ansible Project
Hi Team, 
I have the issue while running the Ansible playbook. There is no error with playbook, but the host.json will be in below format. the execution is happening only for one (Apple2), it is skipping (Apple1) even running with -l (limit) to apple1. Its picking the vars of Apple2. 
FYI: Both are on the same host, but need two processes running with different vars.

Any idea is appreciated!!!


{

   "all":{

      "hosts":{

      },

      "children":{

         “Apple1":{

            "hosts":{

               "10.10”.10.1:null

            },

            "vars":{

               "abs_path”:”/a/b/c”,

               "Version":"6.8.9",

               "processors":"12”,

            }

         },

         “Apple2”:{

            "hosts":{

               "10.10”.10.1:null

            },

            "vars":{

               "abs_path”:”/x/yz”,

               "Version":"6.8.9",

               "processors":"11",

            }

         }

      }

   }

}


Thanks 

Matt Martz

unread,
Jul 21, 2020, 5:36:08 PM7/21/20
to ansible...@googlegroups.com
hosts are unique in Ansible, and one thing to note is that the inventory doesn't maintain hierarchy, like the YAML/JSON format may lead you to believe.

As such, group vars are merged down on to the hosts at inventory processing time.  As such, your `vars` are applied down onto 10.10.10.1, with 1 group overwriting the other.

You can get around this by using a host alias, and then putting the address in `ansible_host` under `vars`.  You could just make `Apple1` and `Apple2` those aliases, instead of making them groups:

{
    "all": {
        "hosts": {
            "Apple1": {
                "ansible_host": "10.10.10.1",
                "Version": "6.8.9",
                "abs_path": "/a/b/c",
                "processors": "12"
            },
            "Apple2": {
                "ansible_host": "10.10.10.1",
                "Version": "6.8.9",
                "abs_path": "/x/yz",
                "processors": "11"
            }
        }
    }
}

--
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/4624d093-6b5f-49b2-b605-f4c3dcc5d60ao%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

Mahesh N

unread,
Jul 21, 2020, 5:51:38 PM7/21/20
to Ansible Project
Thanks for the info @Matt
I'm in need of running 5 such groups in each host, and I couldn't make 5 alias for the single host. Is there any other work around.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Matt Martz

unread,
Jul 21, 2020, 6:03:08 PM7/21/20
to ansible...@googlegroups.com
You could still use groups, you just need unique hosts.  So if you had 5 groups and 5 hosts, you would need 25 hosts basically.  Remember that group vars merge down on the hosts, and the host is singular, so the same entry in multiple groups is the same host.

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/79fa3f60-5174-43eb-8cc8-257acddf844bo%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages