Commit 0dcd3a25 authored by Jakob Levisen Kvistgaard's avatar Jakob Levisen Kvistgaard
Browse files

added more service tasks and tags for easy play

parent f0638605
......@@ -7,9 +7,10 @@ all:
children:
robots:
hosts:
192.168.4.2:
# 192.168.4.2:
# 192.168.4.3:
# 192.168.4.4:
192.168.4.5:
vars:
robot_repos:
- drobotti-drivers
......
- name: Prime ubuntu distribution
hosts: all
remote_user: "{{ drobotti_user }}"
gather_facts: yes
tasks:
- include_role:
name: common
tasks_from: deploy_ssh_key
- include_role:
name: common
tasks_from: auth_on_sudo_remove
- include_role:
name: common
tasks_from: change_password
\ No newline at end of file
......@@ -3,6 +3,20 @@
The playbooks are only tested on ubuntu20.04 controller, but should work on any debian distribution..
#### ansible tags
* restart #fetch from git and restart
* setup #clean install deployment
* prime #Setup ssh keys, remove sudo auth
* remove #remove services running
#### ansible run
```
ansible-playbook -k -i inventory/hosts.yml -t <tag> robotti.yml
```
## dependencies
sudo apt install ansible
......
- name: Setup Rpi on DesktopRobotti
hosts: robots
remote_user: "{{ drobotti_user }}"
gather_facts: yes
tasks:
# - name: Deploy ssh key
# include_role:
# name: common
# tasks_from: deploy_ssh_key
# tags:
# - prime
# - name: Sudo permission
# include_role:
# name: common
# tasks_from: auth_on_sudo_remove
# tags:
# - prime
# - name: ROS2 setup locale
# include_role:
# name: ros2
# tasks_from: locales
# tags:
# - setup
# - name: Set ROS2 apt sources
# include_role:
# name: ros2
# tasks_from: apt_sources
# tags:
# - setup
# - name: Install ROS2 and build tools
# include_role:
# name: ros2
# tasks_from: install_ros2
# tags:
# - setup
# - name: Setup devices configuration
# include_role:
# name: robotti
# tasks_from: udev
# tags:
# - setup
# - name: Setup /boot/ configs
# include_role:
# name: robotti
# tasks_from: boot
# tags:
# - setup
# - name: Stop services
# include_role:
# name: robotti
# tasks_from: stop_service
# tags:
# - restart
# - name: Remove services
# include_role:
# name: robotti
# tasks_from: stop_service
# tags:
# - remove
# - name: Fetch repositories
# include_role:
# name: robotti
# tasks_from: git_packages
# tags:
# - setup
# - restart
# - name: Build ros2_packages
# include_role:
# name: robotti
# tasks_from: build
# tags:
# - setup
# - restart
- name: Deploy services
include_role:
name: robotti
tasks_from: create_service
tags:
- setup
- name: Start services
include_role:
name: robotti
tasks_from: start_service
tags:
- restart
\ No newline at end of file
- name: Permit sudo excution without authentication fix
become: yes
ansible.builtin.lineinfile:
create: yes
state: present
owner: root
group: root
line: "{{ drobotti_user }} ALL=(ALL:ALL) NOPASSWD: ALL"
path: "/etc/sudoers.d/{{ drobotti_user }}"
mode: '0400'
\ No newline at end of file
- tags:
- always
block:
- name: Permit sudo excution without authentication fix
become: yes
ansible.builtin.lineinfile:
create: yes
state: present
owner: root
group: root
line: "{{ drobotti_user }} ALL=(ALL:ALL) NOPASSWD: ALL"
path: "/etc/sudoers.d/{{ drobotti_user }}"
mode: '0400'
\ No newline at end of file
---
- name: Change user password
become: yes
ansible.builtin.user:
name: "{{ drobotti_user }}"
update_password: always
password: "{{ drobotti_password | password_hash('sha512') }}"
\ No newline at end of file
- tags:
- always
block:
- name: Change user password
become: yes
ansible.builtin.user:
name: "{{ drobotti_user }}"
update_password: always
password: "{{ drobotti_password | password_hash('sha512') }}"
\ No newline at end of file
- name: "Deploy {{ item }}"
become: true
ansible.builtin.apt:
update_cache: yes
state: present
pkg: "{{ item }}"
\ No newline at end of file
- tags:
- always
block:
- name: "Deploy {{ item }}"
become: true
ansible.builtin.apt:
update_cache: yes
state: present
pkg: "{{ item }}"
\ No newline at end of file
- name: Register controller user
ansible.builtin.shell:
cmd: whoami
register: whoami_controller
delegate_to: localhost
- tags:
- always
block:
- name: Register controller user
ansible.builtin.shell:
cmd: whoami
register: whoami_controller
delegate_to: localhost
- name: Register default ssh key (id_rsa)
ansible.builtin.stat:
path: "/home/{{ whoami_controller.stdout }}/.ssh/id_rsa"
register: ssh_key_stat_controller
delegate_to: localhost
- name: Register default ssh key (id_rsa)
ansible.builtin.stat:
path: "/home/{{ whoami_controller.stdout }}/.ssh/id_rsa"
register: ssh_key_stat_controller
delegate_to: localhost
- name: Generate ssh-key
community.crypto.openssh_keypair:
path: "/home/{{ whoami_controller.stdout }}/.ssh/id_rsa"
delegate_to: localhost
when: ssh_key_stat_controller.stat.exists == false
- name: Generate ssh-key
community.crypto.openssh_keypair:
path: "/home/{{ whoami_controller.stdout }}/.ssh/id_rsa"
delegate_to: localhost
when: ssh_key_stat_controller.stat.exists == false
- name: Set public key variable
set_fact:
user_public_key: "{{ lookup('file','/home/{{ whoami_controller.stdout }}/.ssh/id_rsa.pub') }}"
- name: Set public key variable
set_fact:
user_public_key: "{{ lookup('file','/home/{{ whoami_controller.stdout }}/.ssh/id_rsa.pub') }}"
- name: Deploy public key
ansible.builtin.lineinfile:
create: yes
state: present
line: "{{ user_public_key }}"
path: "{{ ansible_facts.user_dir }}/.ssh/authorized_keys"
mode: '0600'
\ No newline at end of file
- name: Deploy public key
ansible.builtin.lineinfile:
create: yes
state: present
line: "{{ user_public_key }}"
path: "{{ ansible_facts.user_dir }}/.ssh/authorized_keys"
mode: '0600'
\ No newline at end of file
- name: Gather package facts
ansible.builtin.package_facts:
manager: auto
\ No newline at end of file
- tags:
- always
block:
- name: Gather package facts
ansible.builtin.package_facts:
manager: auto
\ No newline at end of file
- name: Reload systemd
become: yes
ansible.builtin.systemd:
daemon_reload: yes
\ No newline at end of file
- name: Enable SPI for IMU
become: yes
ansible.builtin.lineinfile:
create: yes
owner: root
group: root
state: present
line: 'dtoverlay=i2c-gpio,bus=3'
path: '/boot/firmware/usercfg.txt'
mode: '0755'
register: setup_boot_cfg
- name: Reboot
become: yes
ansible.builtin.reboot:
reboot_timeout: 1200
when: 'setup_boot_cfg.changed == true'
\ No newline at end of file
- tags:
- always
block:
- name: Enable SPI for IMU
become: yes
ansible.builtin.lineinfile:
create: yes
owner: root
group: root
state: present
line: 'dtoverlay=i2c-gpio,bus=3'
path: '/boot/firmware/usercfg.txt'
mode: '0755'
register: setup_boot_cfg
- name: Reboot
become: yes
ansible.builtin.reboot:
reboot_timeout: 1200
when: 'setup_boot_cfg.changed == true'
\ No newline at end of file
- name: Install ros2 apt dependencies
include_role:
name: common
tasks_from: deploy_apt
with_items: '{{ ros2_apt_depencencies }}'
- name: Install python3-rosdep2
become: yes
ansible.builtin.apt:
state: present
pkg: python3-rosdep2
register: 'status_deploy_rosdep'
- name: Rosdep init
become: yes
ansible.builtin.shell:
executable: /bin/bash
cmd: rosdep init
when: 'status_deploy_rosdep.changed == true'
- name: Rosdep update sources
ansible.builtin.shell:
executable: /bin/bash
cmd: rosdep update
# TODO: Change to generic, when we we got same src structure?
- name: Install ros package depencies.
ansible.builtin.shell:
executable: /bin/bash
cmd: 'source /opt/ros/{{ ros_distro }}/setup.bash && rosdep install --from-paths src --ignore-src -y'
chdir: '{{ ansible_facts.user_dir }}/drobotti-drivers'
with_items: 'drobotti-drivers'
# Wiring pi dependency
# - name: Deploy wiringpi
# become: yes
# ansible.builtin.shell:
# cmd: 'dpkg -i {{ ansible_facts.user_dir }}/drobotti-drivers/{{ wiring_pi_dpkg_relative_path }}'
- name: Build packages
include_role:
name: ros2
tasks_from: colcon_build
with_items: '{{ packages_to_build }}'
- tags:
- always
block:
- name: Install ros2 apt dependencies
include_role:
name: common
tasks_from: deploy_apt
with_items: '{{ ros2_apt_depencencies }}'
- name: Install python3-rosdep2
become: yes
ansible.builtin.apt:
state: present
pkg: python3-rosdep2
register: 'status_deploy_rosdep'
#Rosdep gets initialized on install..
# - name: Rosdep init
# become: yes
# ansible.builtin.shell:
# executable: /bin/bash
# cmd: rosdep init
# when: 'status_deploy_rosdep.changed == true'
- name: Rosdep update sources
ansible.builtin.shell:
executable: /bin/bash
cmd: rosdep update
# TODO: Change to generic, when we we got same src structure?
- name: Install ros package depencies.
ansible.builtin.shell:
executable: /bin/bash
cmd: 'source /opt/ros/{{ ros_distro }}/setup.bash && rosdep install --from-paths src --ignore-src -y'
chdir: '{{ ansible_facts.user_dir }}/drobotti-drivers'
with_items: 'drobotti-drivers'
# Wiring pi dependency
# - name: Deploy wiringpi
# become: yes
# ansible.builtin.shell:
# cmd: 'dpkg -i {{ ansible_facts.user_dir }}/drobotti-drivers/{{ wiring_pi_dpkg_relative_path }}'
- name: Build packages
include_role:
name: ros2
tasks_from: colcon_build
with_items: '{{ packages_to_build }}'
- name: Create systemd service files
become: yes
ansible.builtin.template:
src: service.j2
dest: "/etc/systemd/system/{{ item.name }}.service"
mode: '0664'
with_items: '{{ service_files }}'
\ No newline at end of file
- tags:
- always
block:
- name: Create systemd service confiigs
become: yes
ansible.builtin.template:
src: service.j2
dest: "/etc/systemd/system/{{ item.name }}.service"
mode: '0664'
with_items: '{{ service_files }}'
notify:
- reload systemd
- name: Start services and enable on boot
become: yes
ansible.builtin.systemd:
name: "{{ item.name }}"
state: started
masked: no
enabled: yes
with_items: '{{ service_files }}'
\ No newline at end of file
- name: Install git
include_role:
name: common
tasks_from: deploy_apt
with_items:
- git
- name: Get updates from git
ansible.builtin.git:
repo: '{{ gitlab_repos_host }}/{{ item }}.git'
dest: '{{ ansible_facts.user_dir }}/{{ item }}'
with_items: '{{ robot_repos }}'
- tags:
- always
block:
- name: Install git
include_role:
name: common
tasks_from: deploy_apt
with_items:
- git
- name: Get updates from git
ansible.builtin.git:
repo: '{{ gitlab_repos_host }}/{{ item }}.git'
dest: '{{ ansible_facts.user_dir }}/{{ item }}'
with_items: '{{ robot_repos }}'
- tags:
- always
block:
- name: Stop services
become: yes
ansible.builtin.systemd:
name: "{{ item.name }}"
enabled: no
state: stopped
with_items: '{{ service_files }}'
- name: Remove service files
become: yes
ansible.builtin.file:
path: "/etc/systemd/system/{{ item.name }}.service"
state: absent
with_items: '{{ service_files }}'
notify:
- Reload systemd
\ No newline at end of file
- tags:
- always
block:
- name: Start services
become: yes
ansible.builtin.systemd:
name: "{{ item.name }}"
state: started
with_items: '{{ service_files }}'
\ No newline at end of file
- tags:
- always
block:
- name: Stop services
become: yes
ansible.builtin.systemd:
name: "{{ item.name }}"
state: stopped
with_items: '{{ service_files }}'
\ No newline at end of file
- name: "Add {{ drobotti_user }} user to dialout group"
become: yes
ansible.builtin.user:
name: "{{ drobotti_user }}"
groups:
- dialout
- tags:
- always
block:
- name: "Add {{ drobotti_user }} user to dialout group"
become: yes
ansible.builtin.user:
name: "{{ drobotti_user }}"
groups:
- dialout
- set_fact:
usb_udev_target: "KERNEL==\"ttyUSB*\""
i2c_udev_target: "KERNEL==\"i2c*\""
acm_udev_target: "KERNEL==\"ttyACM*\""
udev_string: "MODE=\"0666\" OWNER=\"root\" GROUP=\"dialout\""
- set_fact:
usb_udev_target: "KERNEL==\"ttyUSB*\""
i2c_udev_target: "KERNEL==\"i2c*\""
acm_udev_target: "KERNEL==\"ttyACM*\""
udev_string: "MODE=\"0666\" OWNER=\"root\" GROUP=\"dialout\""
- name: Setup usb udev rule
become: yes
ansible.builtin.lineinfile:
create: yes
state: present
line: "{{ usb_udev_target }} {{ udev_string }}"
path: "/etc/udev/rules.d/usb.rules"
mode: '0664'
- name: Setup usb udev rule
become: yes
ansible.builtin.lineinfile:
create: yes
state: present
line: "{{ usb_udev_target }} {{ udev_string }}"
path: "/etc/udev/rules.d/usb.rules"
mode: '0664'
- name: Setup i2c udev rule
become: yes
ansible.builtin.lineinfile:
create: yes
state: present
line: "{{ i2c_udev_target }} {{ udev_string }}"
path: "/etc/udev/rules.d/i2c.rules"
mode: '0664'
- name: Setup i2c udev rule
become: yes
ansible.builtin.lineinfile:
create: yes
state: present
line: "{{ i2c_udev_target }} {{ udev_string }}"
path: "/etc/udev/rules.d/i2c.rules"
mode: '0664'
- name: Setup acme udev rule
become: yes
ansible.builtin.lineinfile:
create: yes
state: present
line: "{{ acm_udev_target }} {{ udev_string }}"
path: "/etc/udev/rules.d/acm.rules"
mode: '0664'
- name: Setup acme udev rule
become: yes
ansible.builtin.lineinfile:
create: yes
state: present
line: "{{ acm_udev_target }} {{ udev_string }}"
path: "/etc/udev/rules.d/acm.rules"
mode: '0664'
- name: Reload udev
become: yes
ansible.builtin.shell:
cmd: udevadm control --reload-rules && udevadm trigger
\ No newline at end of file
- name: Reload udev
become: yes
ansible.builtin.shell:
cmd: udevadm control --reload-rules && udevadm trigger