文件处理
文件处理的模块(常用)
Blockinfile 该模块可以管理员在指定的文件中插入“一段文本”
Copy 该模块主要用于拷贝文件,将控制节点上的文件拷贝到被管理主机
Fatch 该模块从远程主机中拉取文件到被管理的节点
Lineinfile 支持正则表达式
File 修改文件权限
State 查看文件状态
Synchronize ------>等价于 rsync 批量同步文件
Sefcontext 修改文件上下文永久生效,修改系统的 selinux 相关的配置文件
使用 JINJA2 模版部署自定义文件
· 修改文件时调用 JINJA2模版
JINJA2介绍
使用 JINJA2 模版
[student@workstation file-template]$ cat motd.j2 This is the system {{ ansible_facts['fqdn'] }}. This is a {{ ansible_facts['distribution'] }} version {{ ansible_facts['distribution_version'] }} system. Only use this system with permission. Please report issues to: {{ system_owner }}. [student@workstation file-template]$ cat motd.yml --- - name: configure SOE hosts: all remote_user: devops 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
练习:
Generate a hosts file
Download an initial template file from
http://content.example.com/materials/hosts.j2 to /home/devops/ansible
Complete the template so that it can be used to generate a file with a line for
each inventory host in the same format as /etc/hosts
Create a playbook called /home/devops/ansible/hosts.yml that uses this template
to generate the file /etc/myhosts on hosts in the dev host group.
When the playbook is run, the file /etc/myhosts on hosts in the dev host group should
have a line for each managed host:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.250.10 servera.lab.example.com servera
172.25.250.11 serverb.lab.example.com serverb
172.25.250.12 serverc.lab.example.com serverc
172.25.250.13 serverd.lab.example.com serverd
172.25.250.14 servere.lab.example.com servere
172.25.250.15 serverf.lab.example.com serverf
NOTE: The order in which the inventory host names appear is not important.
登录被节点验证