自動化部署之Ansibler之YAML與Ansible基本元素

YANL 介紹

YAML:另一種標記語言。是用來寫配置文件的語言,非常簡潔和強大。
YAML語法和其他語言類似,也可以表達散列表、標量等數據結構。
結構通過空格來展示;序列裏配置項通過-來代表;Map裏鍵值用:來分隔;YAML的擴展名爲yaml

基本語法規則:

  1. 大小寫敏感
  2. 使用縮進表示層級關係
  3. 縮進時不允許使用Tab鍵,只允許使用空格。
  4. 縮進的空格數目不重要,只要相同層級的元素左側對齊即可

YAML支持的數據結構:

1.對象:鍵值對的集合,又稱爲映射(mapping)/ 哈希(hashes) / 字典(dictionary)
例如:name:Example Developer
鍵 值

2.數組:一組按次序排列的值,又稱爲序列(sequence) / 列表(list)
例如:-Apple
-Orange

3.純量:單個的、不可再分的值
例如:number:12.30
sure:true

ansible基本元素

ansible默認的主機清單是/etc/ansible/hosts文件
主機清單可以手動設置,也可以通過Dynamic Inventory動態生成
一般主機名使用FQDN

vim /etc/ansible/hosts
[webserver]      #方括號設置組名
www1.example.org    #定義被監控主機,這邊可以是主機名也可以是IP地址,主機名需要修改/etc/hosts文件
www2.example.org:2222     #冒號後定義遠程連接端口,默認是ssh的22端口

如果是名稱類似的主機,可以使用列表的方式標識各個主機
[webserver]
www[01:50].example.org ansible_ssh_user=root ansible_ssh_pass=123456

[dbbservers]
db-[a:f].example.org //支持匹配a b c ... f

變量

Inventory中變量
(1)主機變量
[webserver]
www1.magedu.com http_port=80 maxRequestsChild=808
www2.magedu.com http_port=8080 maxRequestsChild=909
(2)組變量
[servers:vars]
ntp_server=ntp.example.org
nfs_server=nfs.example.org
(3)組嵌套
[apache]
http1.example.org
http2.example.org

[nginx]
ngx1.example.org
ngx2.example.org

[webservers:children]   #組變量等於說當調用了 webservers:children 等於調用了四個服務
apache
nginx
#組變量等於說當調用了 webservers:children 等於調用了四個服務
調用了 [apache]組裏面兩臺主機 和[nginx]裏面兩臺主機

inventory變量參數

參數 說明
ansible_ssh_host 將要連接的遠程主機名.與你想要設定的主機的別名不同的話,可通過此變量設置.
ansible_ssh_port ssh端口號.如果不是默認的端口號,通過此變量設置.
ansible_ssh_user 默認的 ssh 用戶名
ansible_ssh_pass ssh 密碼(這種方式並不安全,我們強烈建議使用 --ask-pass 或 SSH 密鑰)
ansible_ssh_private_key_file ssh 使用的私鑰文件.適用於有多個密鑰,而你不想使用 SSH 代理的情況.
ansible_ssh_common_args 此設置附加到sftp,scp和ssh的缺省命令行
ansible_sftp_extra_args 此設置附加到默認sftp命令行。
ansible_scp_extra_args 此設置附加到默認scp命令行。
ansible_ssh_extra_args 此設置附加到默認ssh命令行。
ansible_ssh_pipelining 確定是否使用SSH管道。 這可以覆蓋ansible.cfg中得設置。
ansible_shell_type 目標系統的shell類型.默認情況下,命令的執行使用 'sh' 語法,可設置爲 'csh' 或 'fish'.
ansible_python_interpreter 目標主機的 python 路徑.適用於的情況: 系統中有多個 Python, 或者命令路徑不是"/usr/bin/python",比如 *BSD, 或者 /usr/bin/python
ansible_*_interpreter 這裏的"*"可以是ruby 或perl 或其他語言的解釋器,作用和ansible_python_interpreter 類似
ansible_shell_executable 這將設置ansible控制器將在目標機器上使用的shell,覆蓋ansible.cfg中的配置,默認爲/bin/sh。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章