在受管节点上创建文件或目录
修改文件并将其复制到主机
##常用文件模块
blockinfile | 将文本块添加到现有文件 |
copy | 将文件复制到受管主机 |
fetch | 从受管主机拷贝文件到控制节点 |
file | 设置文件属性 |
lineinfile | 确保特定行位于某个文件 |
stat | 检索文件状态信息 |
synchronize | rsync 命令的一个打包程序 |
##file 模块处理文件,如果不存在就新建
cat file.yml
---
- name: test
hosts: web
tasks:
- name: Touch a file
file:
path: /root/file
owner: thermal
group: thermal
mode: 0640
state: touch
#运行结果
##修改文件属性
- name: test
hosts: web
tasks:
- name: Touch a file
file:
path: /root/file
owner: thermal
group: thermal
mode: 0640
state: touch
- name: set SElinux
file:
path: /root/file
setype: samba_share_t
#运行结果
这样的更改只是临时的,相当于使用chcon命令,刷新后就变回去了:
永久更改:
- name: always set SElinux
sefcontext:
target: /root/file
setype: samba_share_t
state: present
##在受管主机上复制和编辑文件
blockinfile copy fetch lineinfile
##从受管主机中删除文件
---
- name: Delete file
hosts: all
tasks:
- name: delete
file:
dest: /root/file
state: absen #absent 即删除
##检测受管主机上的文件状态
检测文件的 MD5 校验和
path: /root/file
checksum_algorithm: md5
register: result
- debug:
msg: "checksum is {{ result.stat.checksum }}"
运行(上面代码添加到 file.yml 最后)
##同步控制节点和受管主机之间的文件
- name: synchronize file
synchronize:
src: file
dest: /root/file
相当于copy
使用 jinja2 模板部署自定义文件
构建出模板可以更方便的管理文件
{% EXPR %} #表达式或者逻辑
{{ EXPR }} #最终向用户输出表达式或结果
{# COMMENT #} #注释
##构建 jinja2 模板
jinja2 模板由多个元素组成:数据、变量、表达式
模板中使用的变量可以在 playbook 的 vars 中指定
模板中所有的值都使用变量方式,将来会被受管主机对应的值替代
如:/etc/ssh/sshd_config 文件
Port 22 ==> Port {{ ssh_port }}
PermitRootLogin yes ==> {{ root_allowed }}
##部署 jinja2 模板
tasks:
- name: template
template:
src: /root/sshd_template.j2
dest: /dev/ssh/sshd_config
##控制结构
#使用循环
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 中使用
##变量过滤器
{{ output | to_json }} #以 json 格式输出
{{ output | to_yaml }} #以ymal格式输出
{{ output | from_json }} #对 json 格式字符串进行解析
{{ output | from_yaml }} #对ymal 格式字符串进行解析
##练习
vim motd.j2
This is the system {{ ansible_facts['fqdn'] }}
This is a {{ ansible_facts['distribution'] }} version {{ ansible_facts['distribution_version'] }} system.
System owner is {{ system_owner }}.
vim motd.yml
---
- name: configure motd
hosts: all
remote_user: thermal
become: true
vars:
- system_owner: [email protected]
tasks:
- name: configure /etc/motd
template:
src: motd.j2
dest: /etc/motd
owner: root
group: root
mode: 0644
#总结:
1.file 模块库包含创建、复制、编辑、修改等权限和其他属性
2.使用 jinja2 模板动态构建文件来部署
3.jinja2 模板由两个元素构成:变量和表达式,在使用 jinja2 模板时,他们被替换为值
4.通过 jinja2 过滤器,模板表达式可以从一种数据格式转换为另一种
管理大项目
利用主机模式选择主机
#使用通配符匹配多个主机
- hosts: '*' 匹配所有主机
- hosts: '*.example.com' 匹配所有以example。com结尾的主机
- hosts: '172.25.254.*' 匹配所有172.25.254 网段的主机
#通过列表匹配主机或主机组
- 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 除外
- hosts: all,!servera.lab.example.com 所有主机除了 servera.lab.example.com
管理动态清单
github 有很多动态清单脚本
##编写动态清单程序
将 INI 格式的清单转换为 JSON 格式
转换
ansible-inventory -i inventory --list
配置并行
##使用 forks 在 ansible 中配置并行
ansible 最大同时连接数由 ansible 配置文件中 forks 参数控制
可见默认是 5
可以在命令行使用 -f 或者 --forks 参数来指定并行数
##管理滚动更新
如果更新发生在负载均衡服务器,更新完成会重启,可能导致后端所有 web 服务器停止服务,
可以使用 serial 关键字来分批运行
例如:
---
- name: Rolling update
hosts: webservers
serial: 2 2台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
##导入和包含任务的 playbook
#一个只有任务的 playbook
vim 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 用法同于import_tasks
##为外部 play 和任务定义变量
为了提高复用性,我们可以把 tasks.yml 中的内容定义变量。
一个安装软件包和配置开机启动的任务:
- name: Install the {{ packages }}
yum:
name: "{{ packages }}"
state: latest
- name: Start the {{ service }}
service:
name: "{{ service }}"
enabled: true
state: started
可以用于导入主playbook
tasks:
- name: Import task
import_tasks: task.yml
vars:
package: httpd
service: httpd 定义的变量。
#管理大项目综合实验:
ansible rhel*.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 fire_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: rhel*.com
serial: 2 #一次只更新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
运行后:
先执行前两台;
第三台在执行一次。
这是我们当前的目录结构。