Need to avoid using a hosts or inventory file using Ansible

18769 views
Skip to first unread message

Noah Parker

unread,
May 12, 2014, 7:15:07 PM5/12/14
to ansible...@googlegroups.com
Hello,

The way I am implementing Ansible will mean that I will not know the IP of the server I am configuring until I am launching the Playbook. I have tried passing in a variable for the IP for Ansible complains with:

ERROR: provided hosts list is empty

Here is a look at my .yml and command line:

---
- hosts: '{{ test }}'

ansible-playbook test.yml --extra-vars "test=192.168.1.24"

Is it possible to pass this in as a command line argument? 

I am currently using Ubuntu 12.04 as the Ansible server with Ansible v.1.4.4.

Thanks!

James Cammarata

unread,
May 13, 2014, 8:37:39 AM5/13/14
to ansible...@googlegroups.com
I would recommend using a dynamic inventory script that would provide the information, assuming it can be found via some local command or API?


--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9cde569e-b5c3-40d6-b9cc-81e20f0d95f0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
May 13, 2014, 8:38:28 AM5/13/14
to ansible...@googlegroups.com
you always need inventory, but you can do it int he command line: 
  
   -i '192.168.1.24,'​

the comma is required as it signifies its a list vs a directory/file

Michael DeHaan

unread,
May 13, 2014, 11:53:37 AM5/13/14
to ansible...@googlegroups.com
And we should never teach people that trick, because it's quite useful to have inventory.

It's kept around for historical reasons but may not be there indefinitely.




--
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 post to this group, send email to ansible...@googlegroups.com.

Noah Parker

unread,
May 16, 2014, 5:49:45 PM5/16/14
to ansible...@googlegroups.com
I actually tried this using command line and it never worked for me. Are there any dependencies for this to work? Such as an empty hosts or inventory file?

Michael DeHaan

unread,
May 18, 2014, 11:03:23 PM5/18/14
to ansible...@googlegroups.com
"I actually tried this using command line and it never worked for me"

What does "never worked for me" mean?




--
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 post to this group, send email to ansible...@googlegroups.com.

Noah Parker

unread,
May 18, 2014, 11:46:36 PM5/18/14
to ansible...@googlegroups.com
"never worked for me" means that I tried using the command several times using variations in syntax and it was not successful.


--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/G_9JRGp5jGE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Brian Coca

unread,
May 19, 2014, 12:10:39 AM5/19/14
to ansible...@googlegroups.com
what variations? what errors would you get? more information would be useful for debugging.

On Sun, May 18, 2014 at 11:46 PM, Noah Parker <nea...@gmail.com> wrote:
" means that I tried using the command several times using variations in syntax and it was not successful.




--
Brian Coca
Stultorum infinitus est numerus
0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
Pedo mellon a minno

Noah Parker

unread,
May 19, 2014, 3:23:08 PM5/19/14
to ansible...@googlegroups.com
root@MidLinux:/var# ansible-playbook -i '192.168.1.24,' SNMP.yml

PLAY [SNMP] *******************************************************************
skipping: no hosts matched

In the yml file, I specify SNMP for group. If I leave this blank, I get: 

ERROR: hosts declaration is required

SNMP is a real group in the hosts file. A hosts file is not practical for me since I have so many servers. Additionally, I would need a way to pass in SSH UN and PW since those parameters are also kept in the hosts file. Any help would be appreciated.

Matt Martz

unread,
May 19, 2014, 3:31:36 PM5/19/14
to ansible...@googlegroups.com
I am somewhat confused. You can't use an inventory file because you have too many hosts, but need to use one because you have too many usernames and passwords?

When you use: -i 192.168.1.24,

Ansible explicitly only knows of a single host called 192.168.1.24.  It has no concepts of groups or other hosts if you don't give it an inventory file.

Your hosts declaration would either need to be "all" or "192.168.1.24" otherwise it will never match its knowledge of your inventory.

Maybe you should looks into creating a dynamic inventory script to pull from an API or CMDB containing info about your hosts.
--
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 post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
ma...@sivel.net
http://sivel.net/

Noah Parker

unread,
May 19, 2014, 3:56:31 PM5/19/14
to ansible...@googlegroups.com
The way we are trying to use Ansible is to dynamically configure a server during instantiation of a VM. We won't know the IP or hostname of the VM until it is created and once the IP is known, then we want to tell Ansible what to do with it. A hosts/inventory can't be populated when we don't yet know what the IP will be. The current workaround is to launch the VM, grab the IP, modify the hosts file using sed, and then running the playbook to fully configure the server and then resetting the hosts file for next use. I feel like the hosts file dependency is really limiting how we want to use Ansible. 

We will always configure one server at a time, but in this same fashion and there could be many in a day.

I just tried this command and it worked this time, however, since we prefer to use UN and PW for SSH instead of keys (same reason as above, and we don't want to have to establish SSH connection and accept key each time we run Ansible on a new host) we need a method of specifying SSH credentials to the command line using this fake inventory method: ansible-playbook SNMP.yml -i 192.168.1.24,




--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/G_9JRGp5jGE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Brian Coca

unread,
May 19, 2014, 4:36:00 PM5/19/14
to ansible...@googlegroups.com
just change the play to hosts: all, since you are always going to execute it with -i 'ip,' it should not be an issue.

Noah Parker

unread,
May 19, 2014, 5:32:06 PM5/19/14
to ansible...@googlegroups.com
I am able to target the host, however, I am not able to authenticate with the host. I get:

GATHERING FACTS ***************************************************************
fatal: [IP] => SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue

I need to be able to pass in: ansible_ssh_user=root ansible_ssh_pass=password since these are stored in the host file, which is being overridden. I am able to pass -k which asks for a password, but I will sending this command from another machine and will not be able to monitor the terminal to enter the password. Please assist. Once this is working, I will be all set!



--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/G_9JRGp5jGE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Scott Anderson

unread,
May 20, 2014, 12:51:14 PM5/20/14
to ansible...@googlegroups.com
Can you create the VM within the Ansible script, add the new IP to a host group, and go from there? I do this all the time in my Ansible scripts.

An additional suggestion: start using SSH keys instead of username/password and you might find that security management is significantly easier. I don't know what your use case involves, however, so this might not work for you.

Example host group script:

---

# Create the maintenance instance for this environment. The server
# address is added in the logging group and used for later plays.
- hosts: 127.0.0.1
  connection: local
  vars_files:
    - config.yml
  tasks:
    - include: tasks/create_instance.yml ami_instance_name={{ app_aws_moniker }}-ec2-logging instance_group=logging instance_security_group={{ environment_log_security_group }}

# Configures the maintenance instance as a web and application server.
- hosts: logging
  connection: ssh
  remote_user: ubuntu
  vars_files:
    - config.yml
    - vars/aws/base.yml
    - vars/aws/dns.yml
    - vars/aws/vpc.yml
  roles:
    - { role: common }
    - { role: rsyslog_server }
    - { role: rsyslog_base }

Regards,
-scott
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.

To post to this group, send email to ansible-project@googlegroups.com.

Noah Parker

unread,
May 21, 2014, 1:25:44 PM5/21/14
to ansible...@googlegroups.com
We are using Ansible in a way which requires UN/Pass vs. keys for us. Is there a way we can send that into the command line as a "fake inventory file" as well?


Scott Anderson

unread,
May 21, 2014, 1:29:52 PM5/21/14
to ansible...@googlegroups.com
I’m not sure I can envision how a particular use of Ansible would require user/pass instead of keys, but ok. :-)

You can pass any information to Ansible on the command line by either using —extra-vars or accessing environment variables within the script.

Regards,
-scott

James Martin

unread,
May 21, 2014, 6:08:20 PM5/21/14
to ansible...@googlegroups.com
Use a dynamic inventory script if you don't know the IP. That is what
it is designed for. If you want to pass ssh usernames and passwords
(extremely extremely insecure, please use SSH Keys), you can do that
with ansible_ssh_user and ansible_ssh_pass inventory variables. You
will need to have sshpass installed for this to work.

- James
> --
> 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 post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/B04769AF-5A7A-41CA-B79F-D590D286087B%40gmail.com.

Scott Anderson

unread,
May 22, 2014, 1:08:00 PM5/22/14
to ansible...@googlegroups.com
Dynamic inventory scripts are not appropriate if the IP does not exist yet, as in the case of creating a new VM.

Regards,
-scott

James Martin

unread,
May 22, 2014, 3:05:49 PM5/22/14
to ansible...@googlegroups.com
Launch your playbooks in two stages -- one for provisioning the VM,
and one for configuring the VM. Once the VM is provisioned, an IP
will be allocated and a dynamic inventory script would work.

You might also want to look at the add_host module:

http://docs.ansible.com/add_host_module.html
> --
> 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 post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/4aced7ef-8a85-47dd-9eb8-f6ad1932560c%40googlegroups.com.

Scott Anderson

unread,
May 22, 2014, 3:07:05 PM5/22/14
to ansible...@googlegroups.com
add_host is what I use for this purpose.

Regards,
-scott
> You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/G_9JRGp5jGE/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAF%3D2pTYBKJjLh5_5-nKhwUAL8ojx_OEgPa90%3Dz1-vEAJHb9YCw%40mail.gmail.com.

Michael DeHaan

unread,
May 23, 2014, 8:29:44 AM5/23/14
to ansible...@googlegroups.com
"Dynamic inventory scripts are not appropriate if the IP does not exist yet, as in the case of creating a new VM."

Which is why "add_host" exists to add things to a group.




Scott Anderson

unread,
May 23, 2014, 9:11:10 AM5/23/14
to ansible...@googlegroups.com
Seems to be an echo in here… :-)

-scott

Reply all
Reply to author
Forward
0 new messages