Ansible@一個高效的組態管理工具--Ansible configure management--翻譯(五),ansible--ansible
無書面許可請勿轉載進階Playbook
Extra variablesYou may have seen in our template example in the previous chapter that we used avariable called group_names . This is one of the magic variables that are provided byAnsible itself. At the time of writing there are seven such variables, described in thefollowing sections.
外部變數
你在之前的模板例子裡已經看到過我們有一個叫做group_names的變數,這是Ansible提供的一個神奇的變數,像這種變數目前為止總共有7個,接下來我們就將逐一介紹他們!
hostvars allows you to retrieve variables about all the hosts that the current playhas dealt with. If the setup module hasn't yet been run on that host in the currentplay, only its variables will be available. You can access it like you would accessother complex variables, such as ${hostvars.hostname.fact} , so to get the Linuxdistribution running on a server named ns1 , it would be ${hostvars.ns1.ansible_distribution} . The following example sets a variable called zone master to theserver named ns1 . It then calls the template module, which would use this to set themasters for each zone.---#1- name: Setup DNS Servers#2hosts: allnameservers#3tasks:#4- name: Install BIND#5yum: name=named state=installed#6- name: Setup Slaves#7hosts: slavenamesservers#8tasks:#9- name: Get the masters IP#10set_fact: dns_master="{{hostvars.ns1.ansible_default_ipv4.address }}"- name: Configure BIND#12template: dest=/etc/named.confsrc/templates/named.conf.j2#11#13Using hostvars, you can further abstract templates from yourenvironment. If you nest your variable calls, then instead of placing anIP address in the variable section of the play, you can add the hostname.To find the address of a machine named in the variable the_machineyou would use, {{ hostvars.[the_machine].default_ipv4.address }}.
hostvars 變數
hostvas運行你檢索所有當前play已經處理的所有主機,如果setup模組還沒運行,那麼只有hostvar變數可用。它可以用${hostvars.hostname.fact}這種形式來訪問複雜的變數,比如用${hostvars.ns1.ansible_distribution}來訪問ns1這台伺服器的發行版本。下面的例子設定一個dns master伺服器叫ns1,調用模板模組來為每個zone設定mast伺服器:
---
- name: Setup DNS Servers
hosts: allnameservers
tasks:
- name: Install BIND
yum: name=named state=installed
- name: Setup Slaves
hosts: slavenamesservers
tasks:
- name: Get the masters IP
set_fact: dns_master="{{
hostvars.ns1.ansible_default_ipv4.address }}"
- name: Configure BIND
template: dest=/etc/named.conf
src/templates/named.conf.j2
The groups variableThe groups variable contains a list of all hosts in the inventory grouped by theinventory group. This lets you get access to all the hosts that you have configured.This is potentially a very powerful tool. It allows you to iterate across a whole groupand for every host apply an action to the current machine.---- name: Configure the databasehosts: dbserversuser: roottasks:- name: Install mysqlyum: name={{ item }} state=installedwith_items:- mysql-server- MySQL-python- name: Start mysqlservice: name=mysqld state=started enabled=true- name: Create a user for all app serverswith_items: groups.appserversmysql_user: name=kate password=test host={{hostvars.[item].ansible_eth0.ipv4.address }}state=presentYou can even use this variable to create known_hosts files for all of your machinescontaining the host keys of all the other machines. This would allow you to then SSHfrom one machine to another without confirming the identity of the remote host. Itwould also handle removing machines when they leave service or updating them whenthey are replaced. The following is a template for a known_hosts file that does this:{% for host in groups['all'] %}{{ hostvars[host]['ansible_hostname'] }}{{hostvars[host]['ansible_ssh_host_key_rsa_public'] }}{% endfor %}The playbook that uses this template would look like this:---hosts: alltasks:- name: Setup known hostshosts: alltasks:- name: Create known_hoststemplate: src=templates/known_hosts.j2dest=/etc/ssh/ssh_known_hosts owner=root group=rootmode=0644
groups變數
group變數包含裝置清單組內的所有主機,它允許我們同時訪問所有我們配置的主機,這是一個非常強力的工具,讓我們可以曆遍組內的每個主機並在上面應用操作。
---
- name: Configure the database
hosts: dbservers
user: root
tasks:
- name: Install mysql
yum: name={{ item }} state=installed
with_items:
- mysql-server
- MySQL-python
- name: Start mysql
service: name=mysqld state=started enabled=true
- name: Create a user for all app servers
with_items: groups.appservers
mysql_user: name=kate password=test host={{
hostvars.[item].ansible_eth0.ipv4.address }}
state=present
你甚至可以使用這個變數,建立一個known_hosts檔案,包含所有這台主機已知的其他主機,然後應用給你的所有主機。這樣當你使用ssh從一台機器登陸到另外一台的時候就不需要身分識別驗證了。它也可以處理在服務斷開或則因更新時被替換的時候用來移除主機。下面是known_hosts檔案模板的代碼:
{% for host in groups['all'] %}
{{ hostvars[host]['ansible_hostname'] }}
{{hostvars[host]['ansible_ssh_host_key_rsa_public'] }}
{% endfor %}
在playbook中可以這樣使用這個模板:
---
hosts: all
tasks:
- name: Setup known hosts
hosts: all
tasks:
- name: Create known_hosts
template: src=templates/known_hosts.j2
dest=/etc/ssh/ssh_known_hosts owner=root group=root mode=0644
The group_names variableThe group_names variable contains a list of strings with the names of all thegroups the current host is in. This is not only useful for debugging, but also forconditionals detecting group membership. This was used in the last chapter toset up a nameserver.This variable is mostly useful for skipping a task or in a template as a condition. Forinstance, if you had two configurations for the SSH daemon, one secure and one lesssecure, but you only wanted the secure configuration on the machines in the securegroup, you would do it like this:- name: Setup SSHhosts: sshserverstasks:- name: For secure machinesset_fact: sshconfig=files/ssh/sshd_config_securewhen: "'secure' in group_names"- name: For non-secure machinesset_fact: sshconfig=files/ssh/sshd_config_defaultwhen: "'secure' not in group_names"- name: Copy over the configcopy: src={{ sshconfig }} dest=/tmp/sshd_configIn the previous example, we used the set_fact module to set the factfor each case, and then used the copy module. We could have usedthe copy module in place of the set_facts modules and used onefewer task. The reason this was done is that the set_fact moduleruns locally and the copy module runs remotely. When you use theset_facts module first and only call the copy module once, the copiesare made on all the machines in parallel. If you used two copy moduleswith conditions, then each would execute on the relevant machinesseparately. Since copy is the longer task of the two, it benefits the mostfrom running in parallel.
group_names變數
group_names是一個關於當前主機屬於哪些組的,以及這些組名相加所得到的字串列表的變數。它不僅僅用來debugging,也可以用來作為判斷群組成員的條件。上一章關於dns配置的例子中我們使用過。這個變數在用來跳過一些任務的執行或則作為模板的條件的時候非常有用。比如你有2個ssh的配置,一個安全等級比較高、另一個稍微低一些。下面的例子展示如何在高安全等級的組裝置來使用高安全等級的配置:
- name: Setup SSH
hosts: sshservers
tasks:
- name: For secure machines
set_fact: sshconfig=files/ssh/sshd_config_secure
when: "'secure' in group_names"
- name: For non-secure machines
set_fact: sshconfig=files/ssh/sshd_config_default
when: "'secure' not in group_names"
- name: Copy over the config
copy: src={{ sshconfig }} dest=/tmp/sshd_config
在上述例子中,我們在2個條件中分別設定fact然後再部署一個copy,這樣做的原因是因為set_fact是在本地執行,而copy是在遠程執行,當運行時,copy模組是並行啟動並執行,否則當我們在2個條件中分別使用copy,那麼它將單獨運行。如果copy模組啟動並執行時間較長的話,並行啟動並執行效能將會更好一些!
The inventory_hostname variableThe inventory_hostname variable stores the hostname of the server as recorded inthe inventory. You should use this if you have chosen not to run the setup moduleon the current host, or if for various reasons the value detected by the setup moduleis not correct. This is useful when you are doing the initial setup of the machine andchanging the hostname.The inventory_hostname_short variableThe inventory_hostname_short variable is the same as the previous variable;however, it only includes the characters up to the first dot. So for host.example.com , it would return host .
inventory_hostname變數
inventory_hostname變數儲存了在裝置配置清單中伺服器的主機名稱,當你選擇不使用setup模組或則因為其他原因setup模組不能啟動並執行時候,這很有用。另外,當你正在初始化一個台主機並修改它的hostname的時候也很有用。
inventory_hostname_short變數
inventory_hostname_short變數跟inventory_hostname一樣,只是去掉網域名稱,比如inventory_hostname 是host.example 那麼inventory_hostname_short就是 host
The inventory_dir variableThe inventory_dir variable is the path name of the directory containing theinventory file.The inventory_file variableThe inventory_file variable is the same as the previous one, except it also includesthe filename.
inventory_dir
inventory_dir是裝置資訊清單檔的路徑
inventory_file
inventory_file是裝置資訊清單檔的檔案名稱