Ansible@一个高效的配置管理工具--Ansible configure management--翻译(五)
无书面许可请勿转载
高级Playbook
Extra variables
You may have seen in our template example in the previous chapter that we used a
variable called group_names . This is one of the magic variables that are provided by
Ansible itself. At the time of writing there are seven such variables, described in the
following sections.
额外的变量
你在之前的模板样例里已经看到过我们有一个叫做group_names的变量,这是Ansible提供的一个奇妙的变量,像这样的变量眼下为止总共同拥有7个,接下来我们就将逐一介绍他们!
hostvars allows you to retrieve variables about all the hosts that the current play
has dealt with. If the setup module hasn't yet been run on that host in the current
play, only its variables will be available. You can access it like you would access
other complex variables, such as ${hostvars.hostname.fact} , so to get the Linux
distribution running on a server named ns1 , it would be ${hostvars.ns1.ansible_
distribution} . The following example sets a variable called zone master to the
server named ns1 . It then calls the template module, which would use this to set the
masters for each zone.
---
#1
- name: Setup DNS Servers
#2
hosts: allnameservers
#3
tasks:
#4
- name: Install BIND
#5
yum: name=named state=installed
#6
- name: Setup Slaves
#7
hosts: slavenamesservers
#8
tasks:
#9
- name: Get the masters IP
#10
set_fact: dns_master="{{
hostvars.ns1.ansible_default_ipv4.address }}"
- name: Configure BIND
#12
template: dest=/etc/named.conf
src/templates/named.conf.j2
#11
#13
Using hostvars, you can further abstract templates from your
environment. If you nest your variable calls, then instead of placing an
IP 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_machine
you would use, {{ hostvars.[the_machine].default_ipv4.
address }}.
hostvars 变量
hostvas能够让你检索,全部当前play已经处理的主机,假设setup模块还没执行。那么仅仅有hostvar变量可用。它能够用${hostvars.hostname.fact}这样的形式来訪问复杂的变量,比方用${hostvars.ns1.ansible_distribution}来訪问ns1这台server的发行版本号。以下的样例设置一个dns masterserver叫ns1,调用模板模块来为每一个zone设置mastserver:
---
- 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 variable
The groups variable contains a list of all hosts in the inventory grouped by the
inventory 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 group
and for every host apply an action to the current machine.
---
- 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
You can even use this variable to create known_hosts files for all of your machines
containing the host keys of all the other machines. This would allow you to then SSH
from one machine to another without confirming the identity of the remote host. It
would also handle removing machines when they leave service or updating them when
they 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: 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
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 variable
The group_names variable contains a list of strings with the names of all the
groups the current host is in. This is not only useful for debugging, but also for
conditionals detecting group membership. This was used in the last chapter to
set up a nameserver.
This variable is mostly useful for skipping a task or in a template as a condition. For
instance, if you had two configurations for the SSH daemon, one secure and one less
secure, but you only wanted the secure configuration on the machines in the secure
group, you would do it like this:
- 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
In the previous example, we used the set_fact module to set the fact
for each case, and then used the copy module. We could have used
the copy module in place of the set_facts modules and used one
fewer task. The reason this was done is that the set_fact module
runs locally and the copy module runs remotely. When you use the
set_facts module first and only call the copy module once, the copies
are made on all the machines in parallel. If you used two copy modules
with conditions, then each would execute on the relevant machines
separately. Since copy is the longer task of the two, it benefits the most
from 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 variable
The inventory_hostname variable stores the hostname of the server as recorded in
the inventory. You should use this if you have chosen not to run the setup module
on the current host, or if for various reasons the value detected by the setup module
is not correct. This is useful when you are doing the initial setup of the machine and
changing the hostname. The inventory_hostname_short variable
The 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变量保存了在设备配置清单中server的主机名,当你选择不使用setup模块或则由于其它原因setup模块不能执行的时候,这非常实用。
另外,当你正在初始化一个台主机并改动它的hostname的时候也非常实用。
inventory_hostname_short变量
inventory_hostname_short变量跟inventory_hostname一样,仅仅是去掉域名。比方inventory_hostname 是host.example 那么inventory_hostname_short就是 host
The inventory_dir variable
The inventory_dir variable is the path name of the directory containing the
inventory file.
The inventory_file variable
The inventory_file variable is the same as the previous one, except it also includes
the filename.
inventory_dir
inventory_dir是设备清单文件的路径
inventory_file
inventory_file是设备清单文件的文件名称
Ansible@一个高效的配置管理工具--Ansible configure management--翻译(五)的更多相关文章
- Ansible@一个高效的配置管理工具--Ansible configure management--翻译(一)
未经书面许可,请勿转载 --- Ansible is the simplest way to automate apps and IT infrastructure 这是Ansible官方站 ...
- Ansible@一个高效的配置管理工具--Ansible configure management--翻译(三)
未经书面许可.请勿转载 一张图简单概括 Simple Playbooks Ansible is useful as a command-line tool for making small chang ...
- Ansible@一个高效的配置管理工具--Ansible configure management--翻译(十一)
无书面授权,请勿转载 第五章 自己定义模块 Using a module Now that we have written our very first module for Ansible, we ...
- Ansible@一个高效的配置管理工具--Ansible configure management--翻译(八)
如无书面授权,请勿转载 第四章,大型项目中Ansible的使用 Roles If your playbooks start expanding beyond what includes can hel ...
- Ansible@一个高效的配置管理工具--Ansible configure management--翻译(七)
如无书面授权,请勿转载 Larger Projects Until now, we have been looking at single plays in one playbook file. Th ...
- Ansible@一个有效的配置管理工具--Ansible configure management--翻译(四)
不要未经书面许可转载 第三章是长,因为,我会分几个部分来翻译. Advanced Playbooks So far the playbooks that we have looked at are s ...
- Ansible@一个有效的配置管理工具--Ansible configure management--翻译(十二)
如果没有书面授权,请勿转载 第五章 自己定义模块 External inventories In the first chapter we saw how Ansible needs an inven ...
- Ansible@一个有效的配置管理工具--Ansible configure management--翻译(十)
未经书面许可,.请勿转载 Custom Modules Until now we have been working solely with the tools provided to us by A ...
- Ansible 运维自动化 ( 配置管理工具 )
背景 出差背景,要搞项目的自动化部署.因为只直接对接生产分发,机器又非常多,这样以往使用的bat只能作为应急方案了,还是得考虑使用专业化的工具来做这个事情! 当下有许多的运维自动化工具( 配置管理 ) ...
随机推荐
- 2018年最重要的HTML5开发手册,传播正能量
今天给大家推荐这个HTML5开发手册,希望能帮助正在学习web前端的人,鄙人也是刚学习前端没多久,借助于一点资讯平台能够结识更多前端大牛,这是我的web前端/HTML5/javscript技术学习群: ...
- react 使用antd的TreeSelect树选择组件实现多个树选择循环
需求说明,一个帐号角色可以设置管理多个项目的菜单权限 且菜单接口每次只能查询特定项目的菜单数据[无法查全部] 开发思路: 1,获取项目接口数组,得到项目数据 2,循环项目数据,以此为参数递归查询菜单数 ...
- 51nod 1182 完美字符串【字符串排序+哈希】
1182 完美字符串 题目来源: Facebook Hacker Cup选拔 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 约翰认为字符串的完美度等 ...
- 牛客小白月赛6 指纹锁(set容器的骚操作)
原题地址: 题目描述 HA实验有一套非常严密的安全保障体系,在HA实验基地的大门,有一个指纹锁. 该指纹锁的加密算法会把一个指纹转化为一个不超过1e7的数字,两个指纹数值之差越小,就说 ...
- 探究堆喷射(heap spray)
博客园的自动保存系统真心不咋地,写的差不多的文章蓝屏之后就没有了,醉了! 浏览器是互联网世界最主要的软件之一,从IE6到IE11安全攻防在不断升级,防御措施的实施促使堆喷射技巧不断变化.写这篇博文想好 ...
- Android Developer -- Bluetooth篇 开发实例之一 扫描设备
第一步:声明Bluetooth Permissions <!-- 设置蓝牙访问权限 --> <uses-permission android:name="android.p ...
- Python学习笔记——对象
Python 的对象定义方式如下: class Person: def __init__(self, name): self.name = name ...
- Volley缓存说明——一个请求两次回调
从上一篇文章Android 异步网络请求框架-Volley了解volley的一些出来过程,当然也包含网络请求和缓存处理的流程,但是在此需要单独做一些说明. 我在使用过程中忽略了一个事情,就是一个网络请 ...
- lodop 控件实现web打印功能
WEB套打可选方案不多,理想的更少,利用免费控件Lodop+JavaScript实现精确套打,算是较为经典的选择.这种方案其实比较简单,利用一个htm文件就可以实现模板设计过程,几乎是“空手套”式的开 ...
- 机器学习入门之四:机器学习的方法--SVM(支持向量机)(转载)
转自 飞鸟各投林 SVM(支持向量机) 支持向量机算法是诞生于统计学习界,同时在机器学习界大放光彩的经典算法. 支持向量机算法从某种意义上来说是逻辑回归算法的强化:通过给予逻辑回归算法更严格的优化条件 ...