An indispensable Playbook for securing and maintaining hosts for later versions

I am trying to make some Ansible boot files that will provide the environment (n databases, m web servers, etc.) and save these nodes later so that I can run deployments against them. The best I can come up with is https://gist.github.com/geowa4/7686681 (copied below). This works in that it creates machines for different types of servers and saves them in the hosts file. My deployment scenarios, for which I will use the hosts file I just created, make sure the correct packages are installed and configured correctly before deploying the source code. Is this the only way to configure and deploy with Ansible? What if I want to dynamically add a new web server to the mix? Do I have to manually edit static host files? So far, with a dynamic inventory script for Rackspace, it simply lists a whole group of servers without the ability to group them by type. If I could get it, I would be delighted.

hosts.j2:

[a] {% for a in a_provision.instances %} {{ a.rax_accessipv4 }} {% endfor %} [b] {% for b in b_provision.instances %} {{ b.rax_accessipv4 }} {% endfor %} 

main.yml:

 --- - name: a - build request local_action: module: rax username: username api_key: key name: test-a count: 1 flavor: 3 image: a-image-id files: /root/.ssh/authorized_keys: ~/.ssh/id_rsa.pub state: present wait: yes wait_timeout: 1000 networks: - private - public register: a_provision - name: b - build request local_action: module: rax username: username api_key: key name: test-b flavor: 5 image: b-image-id files: /root/.ssh/authorized_keys: ~/.ssh/id_rsa.pub state: present wait: yes wait_timeout: 1000 networks: - private - public register: b_provision - name: add new nodes to hosts configuration template: 'src=hosts.j2 dest=provisioned_hosts' 
+6
source share
1 answer

The Rackspace module and dynamic inventory in recent versions of Ansible (I use 1.4.1) allow you to group servers!

The rax module accepts the "group" parameter, which is stored in the metadata of the created server, which the Rackspace dynamic inventory plugin will then extract to create Ansible groups , so subsequent games can use the group names you specify.

However, it seems that inventory is only requested at the beginning of the game. To work with recently launched servers within the same startup, you need to use the add-host module to add them to your inventory at runtime

 - name: build webservers local_action: module: rax name: webserver group: webservers exact_count: true credentials: ~/.rackspace_cloud_credentials flavor: 2 image: df27d481-63a5-40ca-8920-3d132ed643d9 files: /root/.ssh/authorized_keys: ~/.ssh/id_rsa.pub state: present disk_config: manual wait: yes wait_timeout: 10000 register: webserversvar - name: add newly provisioned webservers to a group local_action: add_host hostname={{ item.accessIPv4 }} groupname=webservers with_items: webserversvar.instances - name: build databases local_action: module: rax name: database group: databases exact_count: true credentials: ~/.rackspace_cloud_credentials flavor: 2 image: df27d481-63a5-40ca-8920-3d132ed643d9 files: /root/.ssh/authorized_keys: ~/.ssh/id_rsa.pub state: present disk_config: manual wait: yes wait_timeout: 10000 register: databasesvar - name: add newly provisioned databases to a group local_action: add_host hostname={{ item.accessIPv4 }} groupname=databases with_items: databasesvar.instances 

Here 's an AWS entry for this that covers many of the same high-level concepts, even if the vendor is different.

+5
source

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


All Articles