Playbook

這裡就是Ansible的重頭戲了,你就是靠這份文件來控制伺服器一切的運作。

這份文件的格式是YAML,由於YAML高可讀性的特點,撰寫一份Playbook不是一件難事。

Ansible Playbook能夠做到事情實在有很多,執行程式碼、執行指令、安裝程式包,甚至開關程式也能做到。現在先講一下Playbook的格式:

#
-
  name: Execute command to display date on web_node1
  hosts: web_node1
  tasks:
    -
      name: Execute a date command
      command: date
-
  name: Execute a command to display hosts file contents on web_node2
  hosts: web_node2
  tasks:
    -
      name: Execute a command to display hosts file
      command: cat /etc/hosts

每一個執行程序都是一個dictionary,如果name跟host倒轉次序是沒有問題,但task中的次序就不可以了,因為task是一個list (在yaml中,list是有次序的!),因此請小心每個task執行的次序。上面的例子是用web_node1執行date來顯示web_node1中的日期,然後在web_node2執行cat來顯示web_node2中的/etc/hosts。

Last updated

Was this helpful?