How can I use ec2.py and localhost at the same time

How can I use ec2.py to get a dynamic list of ec2 hosts, and also use localhost as the host. I searched Google again and again and could not find a suitable solution. I came across some close answers, but everything seems to require breaking down your tasks into two separate books that cannot be run at the same time.

+6
source share
4 answers

I tried to solve a similar problem.

I had a bunch of hosts that accepted connections from the Ansible host. For security reasons, the Ansible host could not connect to itself. However, I needed to run a shared game on all hosts.

I specified all the hosts, excluded the Ansible host, and added localhost:

- name: Common hosts: "tag_name_*:!tag_name_ansible:localhost" roles: - common 

Adding localhost worked fine even when using the EC2 script inventory. I am using Ansible 1.7 with an updated inventory of EC2 script.

+7
source

This is not the only solution - valid offers have been made, but what I almost always do when using the EC2 inventory plugin uses the Ansible function, which I think many people don’t know about: you can use the catalog as an inventory.

Ansible looks for executables and flat files in a directory and combines their results. This is very useful because you can use a flat file to create nice aliases for dynamic groups and add a local host there, possibly setting some variables for it.

 $ tree inventory/staging inventory/staging β”œβ”€β”€ base β”œβ”€β”€ ec2.ini β”œβ”€β”€ ec2.py └── group_vars -> ../group_vars 

An excerpt from the base file is as follows:

 [localhost] # I need to tell Ansible which Python on my system has boto for AWS 127.0.0.1 ansible_python_interpreter=/usr/local/bin/python # The EC2 plugin will populate these groups, but we need to add empty entries # here to make aliases for them below. [tag_Stage_staging] [tag_Role_webserver] [staging:children] tag_Stage_staging [webservers:children] tag_Role_webserver 

Then you just point to the directory for the inventory:

 $ ansible -i inventory/staging webservers -m ec2_facts # OR $ export ANSIBLE_HOSTS=inventory/staging $ ansible webservers -m ec2_facts 
+25
source

I think you can achieve what you want to do using the -l option of the ansible-playbook command.

We do something similar, and the text game team we use is similar to this

 ansible-playbook -l tag_foo_bar:localhost -U username -i ec2.py playbook.yml 

The part after -l is just a template ( http://docs.ansible.com/intro_patterns.html ), which means "restricting the execution of all instances with the tag", foo = bar 'And the localhost host. "The important part is the colon, which simply means "and."

+3
source

@ches, this will not work if you have multiple environments with instances tagged with Role = webserver. Your team:

 $ ansible -i inventory/staging webservers -m ec2_facts 

will display each instance marked as a web server, not just those that belong to the staging environment. If you want to pick up only web servers from the queue, you still need to run:

 $ ansible 'staging:&webservers' -i inventory/staging -m ec2_facts 

except when I missed something.

0
source

Source: https://habr.com/ru/post/970272/


All Articles