2

How do I write a single role and connect to different hosts as the script progresses?

This is an example of what I would like to do, but I am getting an error as shown below.

ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.

The error appears to be in '/etc/ansible/roles/customer_setups/microlight/customer_app_template/tasks/main.yml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

# tasks file for /etc/ansible/roles/customer_app_template
- hosts: synbill-apps
  ^ here

---
# tasks file for /etc/ansible/roles/customer_app_template
- hosts: synbill-apps
  tasks:
  - name: Clone syndicatebilling repository into a new customer app
    git:
      repo: git@bitbucket.org:xxxx/xxx.git
      version: syndicateBookings
      dest: /var/www/html/microlight
      force: yes
  - name: run npm install
    npm:
      path: /var/www/html/microlight
  - name: copy .env file
    copy:
      src: files/.env
      dest: /var/www/html/microlight/.env
  - name: create new app key
    command: chdir=/var/www/html/microlight adonis key:generate
  - name: migrate database
    command: chdir=/var/www/html/microlight adonis migration:run --force
  - name: start the app
    command: chdir=/var/www/html/microlight pm2 start server.js --name placeholder_domain
  - name: save the pm2 config
    command: chdir=/var/www/html/microlight pm2 save
- hosts: synbill-db
  - name: "Install Mysql Client package"
    apt:
      name: "{{ item }}"
      state: present
    with_items:
      - mysql-client
      - python3-dev
      - libmysqlclient-dev
      - python3-mysqldb
  - name: Create syndicate database
    mysql_db:
      name: microlight
      state: present
      login_user: simon
      login_password: xxxxxxxxxxx
- hosts: synbill-apps
  .....now the db is setup do some more work on the apps server
- hosts: synbill-webserver
  .... now setup the virtual host
- hosts: synbill-db
  .... now update the database to so user knows everything is setup.
PrestonDocks
  • 215
  • 3
  • 11

1 Answers1

1

The tasks in the tasks.yml file don't look like a good fit for a role abstraction. I would just write a playbook.

In order to be able to execute the playbook you will need to add a `tasks section here:

- hosts: synbill-db
  tasks:
  - name: "Install Mysql Client package"

To abstract the code with role(s) the easiest way I see, would be to write to write two roles, one for app and one for db.

Henrik Pingel
  • 9,380
  • 2
  • 28
  • 39
  • Thanks, I already have 4 roles I want to pull them into a single role. will your solution work in /roles/somerole/tasks/main.yml – PrestonDocks Jun 06 '19 at 22:10
  • No, it won't. That's not the idea of roles in Ansible. While you could add [conditions](https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html) to single tasks in a role there is no point in merging tasks which should apply to different hosts into a single role. – Henrik Pingel Jun 07 '19 at 07:08
  • Thank you, it worked well putting it all in a single playbook.yml – PrestonDocks Jun 07 '19 at 16:47