Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ansibletags #2

Merged
merged 2 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add ansible tags to select behaviour
  • Loading branch information
gianluca-mascolo committed Nov 17, 2021
commit fde9de40e49d38564447821d045e957bad9a51fa
19 changes: 19 additions & 0 deletions playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,40 @@

- name: Configuring user environment
include_tasks: tasks/setup_environment.yml
tags: [ always ]

- name: Configure instance facts
include_tasks: tasks/configure_instances.yml
loop: "{{instances|dict2items}}"
loop_control:
loop_var: instance_item
tags: [ always ]

- name: Configuring virtual tap interfaces
include_tasks: tasks/setup_networking.yml
loop: "{{instances|dict2items}}"
loop_control:
loop_var: instance_item
tags: [ create_interfaces, create_all ]

- name: Create Firecracker instances
include_tasks: tasks/create_instances.yml
loop: "{{instances|dict2items}}"
loop_control:
loop_var: instance_item
tags: [ create, create_all ]

- name: Destroy Firecracker instances
include_tasks: tasks/destroy_instances.yml
loop: "{{instances|dict2items}}"
loop_control:
loop_var: instance_item
tags: [ never, destroy, destroy_all ]

- name: Destroy virtual tap interfaces
include_tasks: tasks/destroy_networking.yml
loop: "{{instances|dict2items}}"
loop_control:
loop_var: instance_item
tags: [ never, destroy_interfaces, destroy_all ]

5 changes: 4 additions & 1 deletion tasks/configure_instances.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
- name: "{{instance_item.key}}: Prepare interface list"
set_fact:
interface_list: []
tags: [ always ]

- name: "{{instance_item.key}}: Combine interface list"
set_fact:
interface_list: "{{ interface_list + [item|combine({'host_dev_name':'fc-'+((instance_item.key+'-'+item.iface_id) |hash('md5'))[0:8], 'guest_mac':('fc33'+((instance_item.key+'-'+item.iface_id) |hash('md5'))[0:8])|ansible.netcommon.hwaddr('linux') })] }}"
loop: "{{instances[instance_item.key]['network_interfaces']}}"
tags: [ always ]

- name: "{{instance_item.key}}: Merge interface list to instance"
set_fact:
instances: "{{ instances | combine({ instance_item.key: { 'network_interfaces': interface_list }},recursive=True) }}"
instances: "{{ instances | combine({ instance_item.key: { 'network_interfaces': interface_list }},recursive=True) }}"
tags: [ always ]
7 changes: 7 additions & 0 deletions tasks/create_instances.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
state: started
name: "firecracker-console@{{instance_item.key}}"
scope: user
tags: [ create, create_all ]

- name: "{{instance_item.key}}: Start Firecracker service"
systemd:
state: started
name: "firecracker@{{instance_item.key}}"
scope: user
tags: [ create, create_all ]

- name: "{{instance_item.key}}: Set socket fact"
set_fact:
instance_socket: "{{firecracker_runpath}}/firecracker-{{instance_item.key}}.socket"
tags: [ create, create_all ]

- name: "{{instance_item.key}}: Configuring Boot Source"
uri:
Expand All @@ -25,6 +28,7 @@
- 200
- 204
body_format: json
tags: [ create, create_all ]

- name: "{{instance_item.key}}: Configuring Machine"
uri:
Expand All @@ -37,6 +41,7 @@
- 204
body_format: json
when: instance_item.value['machine_config'] is defined
tags: [ create, create_all ]

- name: "{{instance_item.key}}: Configuring Networking"
uri:
Expand All @@ -50,6 +55,7 @@
body_format: json
loop: "{{instance_item.value['network_interfaces']}}"
when: instance_item.value['network_interfaces'] is defined
tags: [ create, create_all ]

- name: "{{instance_item.key}}: Start instance"
uri:
Expand All @@ -61,3 +67,4 @@
- 200
- 204
body_format: json
tags: [ create, create_all ]
14 changes: 14 additions & 0 deletions tasks/destroy_instances.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
- name: "{{instance_item.key}}: Stop Firecracker service"
systemd:
state: stopped
name: "firecracker@{{instance_item.key}}"
scope: user
tags: [ destroy, destroy_all ]

- name: "{{instance_item.key}}: Stop Firecracker console"
systemd:
state: stopped
name: "firecracker-console@{{instance_item.key}}"
scope: user
tags: [ destroy, destroy_all ]
16 changes: 16 additions & 0 deletions tasks/destroy_networking.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
- name: "{{instance_item.key}}: Set Tap interface down"
shell: "{{iputil_cmd}} link set dev {{item.host_dev_name}} up"
args:
removes: "/proc/sys/net/ipv4/conf/{{item.host_dev_name}}"
become: true
loop: "{{instance_item.value['network_interfaces']}}"
tags: [ destroy_interfaces, destroy_all ]

- name: "{{instance_item.key}}: Delete Tap interface"
shell: "{{iputil_cmd}} link delete dev {{item.host_dev_name}}"
args:
removes: "/proc/sys/net/ipv4/conf/{{item.host_dev_name}}"
become: true
loop: "{{instance_item.value['network_interfaces']}}"
tags: [ destroy_interfaces, destroy_all ]
4 changes: 4 additions & 0 deletions tasks/setup_environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@
path: "{{ ansible_env.HOME }}/.config/systemd/user"
state: directory
recurse: yes
tags: [ always ]

- name: Create Firecracker console service
template:
src: templates/firecracker-console-systemd.j2
dest: "{{ ansible_env.HOME }}/.config/systemd/user/[email protected]"
mode: '0644'
tags: [ always ]

- name: Create Firecracker daemon service
template:
src: templates/firecracker-systemd.j2
dest: "{{ ansible_env.HOME }}/.config/systemd/user/[email protected]"
mode: '0644'
tags: [ always ]

- name: Reload systemd user config
systemd:
daemon_reload: yes
scope: user
tags: [ always ]
9 changes: 6 additions & 3 deletions tasks/setup_networking.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
args:
creates: "/proc/sys/net/ipv4/conf/{{item.host_dev_name}}"
become: true
loop: "{{instances[instance_item.key]['network_interfaces']}}"
loop: "{{instance_item.value['network_interfaces']}}"
tags: [ create_interfaces, create_all ]

- name: "{{instance_item.key}}: Add Tap interface to bridge"
shell: "{{iputil_cmd}} link set {{item.host_dev_name}} master {{bridge_interface}}"
become: true
loop: "{{instances[instance_item.key]['network_interfaces']}}"
loop: "{{instance_item.value['network_interfaces']}}"
tags: [ create_interfaces, create_all ]

- name: "{{instance_item.key}}: Set Tap interface up"
shell: "{{iputil_cmd}} link set dev {{item.host_dev_name}} up"
become: true
loop: "{{instances[instance_item.key]['network_interfaces']}}"
loop: "{{instance_item.value['network_interfaces']}}"
tags: [ create_interfaces, create_all ]