Ansible

Ansible 多個主機在一個角色中

  • June 6, 2019

隨著腳本的進行,如何編寫單個角色並連接到不同的主機?

這是我想做的一個例子,但我收到一個錯誤,如下所示。

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.

文件中的任務tasks.yml看起來不太適合角色抽象。我只想寫一本劇本。

為了能夠執行劇本,您需要在此處添加一個“任務”部分:

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

用角色抽象程式碼我看到的最簡單的方法是編寫兩個角色,一個用於應用程序,一個用於數據庫。

引用自:https://serverfault.com/questions/970405