我有一台有 2 台伺服器的主機:一台安裝了 CentOS,另一台安裝了 Ubuntu。
我決定在兩台伺服器上安裝 apache、nginx 和 php-fpm 並編寫了 3 個劇本。
- ubuntu.yml(/home/ansible/playbook):
---
- name: Ubuntu Playbook
hosts: ubun
become: true
vars:
- packages:
- nginx
- apache2
- php-fpm
tasks:
- name: Update apt package
apt:
name: "*"
state: latest
update_cache: yes
- name: Install Packages
apt:
pkg: "{{ packages }}"
state: latest
update_cache: yes
- name: Apache Service Start
service:
name: nginx
state: restarted
enabled: yes
- centos.yml(/home/ansible/playbook):
---
- name: CentOS Playbook
hosts: cent
become: true
vars:
packages:
- epel-release
- httpd
- nginx
- php-fpm
tasks:
- name: Update yum package
yum:
name: "*"
state: latest
update_cache: yes
- name: Install Packages
yum:
name: "{{ packages }}"
state: latest
update_cache: yes
- name: Apache Service Start
service:
name: nginx
state: restarted
enabled: yes
- base.yml(/home/ansible/playbook):
---
- name: Base Playbook
hosts: aws
become: true
tasks:
- name: Performing Tasks for CentOS
when: ansible_facts['distribution'] == 'CentOS'
include_tasks: centos.yml
- name: Performing Tasks for Ubuntu
when: ansible_facts['distribution'] == 'Ubuntu'
include_tasks: ubuntu.yml
我的 3 個 Ansible 群組是:
[aws] 其中包含伺服器
[cent] 其中包含 CentOS 伺服器
[ubun] 包含 Ubuntu 伺服器
我嘗試單獨進行空運行centos.yml
,ubuntu.yml
並且它有效,但是當我嘗試空運行時,base.yml
會出現以下錯誤:
FAILED! => {"reason": "unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>\n\nThe error appears to be in '/home/ansible/playbook/centos.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: CentOS Playbook\n ^ here\n"}
FAILED! => {"reason": "unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>\n\nThe error appears to be in '/home/ansible/playbook/ubuntu.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: Ubuntu Playbook\n ^ here\n"}
我已經嘗試替換import_tasks
為include_tasks
但我得到了同樣的錯誤。
答案1
我的解決方案是將這些合併到一個遊戲中,因為它們做同樣的事情。
---
- name: php-fpm play
hosts: aws
become: true
vars:
- repo:
Debian:
- apt # already installed, but need something here
RedHat:
- epel-release
- packages:
Debian:
- apache2
#- nginx # cannot have 2 listening on port 80
- php-fpm
RedHat:
- httpd
#- nginx
- php-fpm
- services:
Debian: apache2
RedHat: httpd
tasks:
# TODO move update * task to different play
- name: Install repo
# Seperate package transaction for EPEL
# so it is available in the next task
package:
name: "{{ repo[ansible_os_family] }}"
- name: Install Web Server Packages
# Keyed by OS family fact to also support RHEL and Debian
package:
name: "{{ packages[ansible_os_family] }}"
state: latest
- name: Web Service Start
service:
name: "{{ services[ansible_os_family] }}"
state: restarted
enabled: yes
不能讓多個伺服器偵聽連接埠 80 和 443。如果您想要其中一個代理程式另一個或其他什麼,您將需要部署一個設定檔來變更連接埠。
使用package:
委託給實際套件管理器的操作。使套件安裝任務能夠在不同作業系統上運作。不能update_cache
這樣做,但是 yum 在像 apt 那樣添加存儲庫時不需要它。
變數結構是作業系統系列特定值的字典。這使得套件和服務名稱能夠按事實索引。作業系統系列,因此除了 CentOS 和 Ubuntu 之外,它也適用於 RHEL 和 Debian。
縮排是錯誤的。模組參數需要縮排低於任務級別指令的級別,例如name:
.
不能include_tasks
完整播放,僅適用於import_playbook
.include_tasks
當您有角色時會更容易,角色有此類檔案的任務目錄。 (遊戲等級的任務是一回事,但我贊成讓角色做所有事情。)
要使其有用,還需要做更多的工作。
當你需要使用template
安裝配置時,EL 將配置/etc/httpd/
放在/etc/apache2/
.
許多 Web 伺服器角色都是開源的,如果您想要想法,可以看看。查看星系。
考慮將您的任務轉移到角色,以實現重複使用。
答案2
如果你以角色為例更好,這裡有固定的程式碼:
---
- hosts: aws
remote_user: myuser
become: true
tasks:
- name: Performing Tasks for CentOS
include_tasks: centos.yml
when: ansible_facts['distribution'] == 'CentOS'
- name: Performing Tasks for Ubuntu
include_tasks: ubuntu.yml
when: ansible_facts['distribution'] == 'Ubuntu'
centos.yml 任務:
---
- name: installing httpd
yum: pkg=httpd state=present
ubuntu.yml 任務:
- name: installing apache2
apt: pkg=apache2 state=present
在任務文件中,您只需要任務,不需要主機、任務等。