一、修改文件并将其复制到主机
1.1 常用文件模块
blockinfile | 将文本块添加到现有文件 |
---|---|
copy | 将文件复制到受管主机 |
fetch | 从受管主机拷贝文件到控制节点 |
file | 设置文件属性 |
lineinfile | 确保特定行位于某个文件 |
stat | 检索文件状态信息 |
synchronize | rsync 命令的一个打包程序 |
file | 模块处理文件,如果不存在就新建 |
cat file.yml
---
- name: Test
hosts: webservers
tasks:
- name: Touch a file
file:
path: /root/file
owner: student
group: student
mode: 0640
state: touch
修改文件属性
cat file.yml
---
- name: Test
hosts: webservers
tasks:
- name: Touch a file
file:
path: /root/file
owner: student
group: student
mode: 0640
state: touch
- name: Set SElinux
file:
path: /root/file
setype: samba_share_t
永久更改
cat file.yml
- name: Test
hosts: webservers
tasks:
- name: Touch a file
file:
path: /root/file
owner: student
group: student
mode: 0640
state: touch
- name: Set SElinux
file:
path: /root/file
setype: samba_share_t
- name: Set SElinux
sefcontext:
target: /root/file
setype: samba_share_t
state: present
在受管主机上复制和编辑文件
从受管主机中删除文件
- name: Delete file
file:
dest: /root/file
state: absent #absent 即删除
检测受管主机上的文件状态
检测文件的MD5 校验和
- name: Verify the status
stat:
path: /root/file
checksum_algorithm: md5
register: result
-debug:
msg: "The checksum is {{ result.stat.checksum }}"
运行(上面代码添加到file.yml 最后)
TASK [debug] *******************************************************************
ok: [servera.lab.example.com] => {
"msg": "The checksum is d41d8cd98f00b204e9800998ecf8427e"
}
同步控制节点和受管主机之间的文件
- name: synchronize file
synchronize:
src: file
dest: /root/file
二、使用jinja2 模板部署自定义文件
构建出模板可以更方便的管理文件
{% EXPR %} #表达式或者逻辑
{{ EXPR }} #最终向用户输出表达式或结果
{# COMMENT #} #注释
2.1 构建jinja2 模板
jinja2 模板由多个元素组成:数据、变量、表达式
模板中使用的变量可以在playbook 的vars 中指定
模板中所有的值都使用变量方式,将来会被受管主机对应的值替代
如:/etc/ssh/sshd_config 文件
Port 22 ==> Port {{ ssh_port }}
PermitRootLogin yes ==> {{ root_allowed }}
2.2 部署jinja2 模板
tasks:
- name: template
template:
src: /root/j2-template.j2
dest: /root/dest-config-file.txt
使用循环
jinja2 使用for 语句提供循环:
#1.
{% for user in users %}
{{ user }} #user 变量将遍历users
{% endfor %}
#2.
{% for myhost in groups[‘myhosts’] %} #列出myhosts 组中所有主机
{{ myhosts }}
{% endfor %}
#使用条件句
{% if finished %} #只有此条件为真,才会将result 变量的值放入文件
{{ result }}
{% endif %}
jinja2 的循环和条件只能在模板中使用,不能在playbook 中使用
2.3 变量过滤器
{{ output | to_json }} #以json 格式输出
{{ output | to_yaml }}
{{ output | from_json }} #对json 格式字符串进行解析
{{ output | from_yaml }}
三、管理大项目
3.1 使用通配符匹配多个主机
- hosts: '*'
- hosts: '*.example.com'
- hosts: '172.25.254.*'
3.2 通过列表匹配主机或主机组
- hosts: www1.example.com,www2.example.com,172.25.254.250
- hosts: webservers,westos
也可以将通配符和列表等一起使用
- hosts: webservers,&westos #即属于webserver 组,也属于westos 组
- hosts: westos,!servera.lab.example.com#匹配westos 组中所有主机,但是servera.lab.example.com 除外
- all,!servera.lab.example.com #所有主机除了servera.lab.example.com
四、管理动态清单
4.1 编写动态清单程序
将INI 格式的清单转换为JSON 格式
ansible-inventory -i inventory --list
使用forks 在ansible 中配置并行
ansible 最大同时连接数由ansible 配置文件中forks 参数控制
grep forks /etc/ansible/ansible.cfg
#forks = 5 #默认是5
ansible-config dump | grep -i forks
DEFAULT_FORKS(default) = 5
可以在命令行使用-f 或者–forks 参数来指定并行数
五、管理滚动更新
如果更新发生在负载均衡服务器,更新完成会重启,可能导致后端所有web 服务器停止服务,可以使用serial 关键字来分批运行
---
- name: Rolling update
hosts: webservers
serial: 2
tasks:
- name: Install apache
yum:
name: httpd
state: latest
notify: restart apache
handlers:
- name: restart apache
service:
name: httpd
state: restarted
serial 参数还有一个优点:在更新时如果出现问题,那么在前2 台发生问题是playbook 就会停止运行,后面的服务器不会执行,那么也就保证了服务的高可用
六、包含和导入文件
大型playbook 管理起来比较复杂,可以用模块化的方式管理
两种方法:包含、导入
导入playbook
例1:
- name: configure webserver
import_playbook: web.yml
例2:
- name: Play 1
hosts: localhost
tasks:
- debug:
msg: Play1
- name: Import Playbook
import_playbook: play2.yml
导入和包含任务的playbook
一个只有任务的playbook
cat tasks.yml
- name: Install apache
yum:
name: httpd
state: latest
- name: Start Apache
service:
name: httpd
state: started
导入任务
---
- name: Install web
hosts: webservers
tasks:
- import_tasks: tasks.yml
使用导入时,when 等条件语句应用于导入的每个任务;循环不能作用于导入的任务
包含任务
---
- name: Install web
hosts: webservers
tasks:
- include_tasks: tasks.yml
为外部play 和任务定义变量,提高复用性
一个安装软件包和配置开机启动的任务
---
- name: Install the {{ packages }}
yum:
name: "{{ packages }}"
state: latest
- name: Start the {{ service }}
service:
name: "{{ service }}"
enabled: true
state: started
可以用于导入
tasks:
- name: Import task
import_tasks: task.yml
vars:
package: httpd
service: httpd
管理大项目综合实验
cat ansible.cfg
[defaults]
inventory = ./inventory
cat inventory
servera.lab.example.com
serverb.lab.example.com
serverc.lab.example.com
ansible server*.lab.example.com --list-hosts
相同的模块:安装包
cat install_and_enabled.yml
---
- name: Install {{ packages }}
yum:
name: "{{ packages }}"
state: latest
- name: Enable and start {{ service }}
service:
name: "{{ service }}"
enabled: true
state: started
apache 配置
cat web_tasks.yml
---
- name: Install and start httpd
import_tasks: install_and_enabled.yml
vars:
packages: httpd
service: httpd
- name: Configure apache
copy:
src: files/example.conf
dest: /etc/httpd/conf.d/example.conf
owner: root
group: root
mode: 0644
notify:
- restart httpd
firewall 配置
cat firewall_tasks.yml
---
- name: Install and start firewalld
import_tasks: install_and_enabled.yml
vars:
packages: firewalld
service: firewalld
- name: Firewall permit apache
firewalld:
service: http
immediate: true
permanent: true
state: enabled
主playbook
cat playbook.yml
---
- name: Install and Configure web service
hosts: server*.lab.example.com
serial: 2
tasks:
- name: Import web_tasks.yml
import_tasks: tasks/web_tasks.yml
- name: Import the firewall_tasks.yml
import_tasks: tasks/firewall_tasks.yml
handlers:
- name: restart httpd
service:
name: httpd
state: restarted