Skip to content

Instantly share code, notes, and snippets.

@kraeml
Created March 20, 2024 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kraeml/f27391f29bfc9451dc4212d23bd3d2ad to your computer and use it in GitHub Desktop.
Save kraeml/f27391f29bfc9451dc4212d23bd3d2ad to your computer and use it in GitHub Desktop.
Ansible bootstrap
---
- name: Erstellen von LXC Maschinen
hosts: lokal
vars:
lxc_containers:
- ansible-lxc
- db2
- web
- informaticker
tasks:
- name: Erstelle LXC Maschinen
become: true
lxc_container:
name: "{{ item }}"
state: started
loop: "{{ lxc_containers }}"
register: ansible_lxc_container
- debug:
var: item
loop: "{{ ansible_lxc_container['results'] }}"
- name: Netzwerkdienste neu starten
become: true
service:
name: "{{ item }}"
state: restarted
loop:
- lxc-net
#- systemd-resolved
#- systemd-networkd
- name: DHCP Leas erneuern
become: true
shell: "lxc-attach {{ item }} -- dhclient eth0"
loop: "{{ lxc_containers }}"
- name: Erstelle LXC Maschinen und sammle die Daten wie IPv4-Addr
become: true
lxc_container:
name: "{{ item }}"
state: started
loop: "{{ lxc_containers }}"
register: ansible_lxc_container
- debug:
var: item
loop: "{{ ansible_lxc_container['results'] }}"
- name: "Inventory aus template"
template:
src: lxc_inventory.j2
dest: lxc_inventory_template
- name: Erstelle lxc_inventory
copy:
dest: lxc_inventory
content: |
[lxc]
[lxc:vars]
- name: Hosteinträge
lineinfile:
path: lxc_inventory
insertbefore: [lxc:vars]
regexp: "^{{ item.lxc_container.name }} ansible_host={{ item.lxc_container.ips[0] }}"
line: "{{ item.lxc_container.name }} ansible_host={{ item.lxc_container.ips[0] }}"
loop: "{{ ansible_lxc_container['results'] }}"
- name: Gruppenvariable anhängen
blockinfile:
path: lxc_inventory
insertafter: [lxc:vars]
block: |
ansible_user=ubuntu
ansible_password=ubuntu
ansible_ssh_common_args="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
ansible_become_pass=ubuntu
...
[lxc]
{# Hier ist die Laufvariable host nicht item wie im Playboook #}
{% for host in ansible_lxc_container['results'] %}
{{ host.lxc_container.name }} ansible_host={{ host.lxc_container.ips[0] }}
{% endfor %}
[lxc:vars]
ansible_user=ubuntu
ansible_password=ubuntu
ansible_ssh_common_args="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
ansible_become_pass=ubuntu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment