大网数据广告活动
服务器运维问答

Ansible multiple hosts in one role

· 52CE 我爱测

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 indicate…

问题原文

原帖链接 · PrestonDocks · CC BY-SA 4.0

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: [email omitted]: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.

已采纳解答

原帖链接 · Henrik Pingel · CC BY-SA 4.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-06-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。