1. 程式人生 > 實用技巧 >playbook編寫(四)

playbook編寫(四)

檔案處理

檔案處理的模組(常用)

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.

登入被節點驗證