修改文件并将其复制到主机
使用copy模块
(将服务器文件复制到客户端)
---
- name: copy
hosts: all
tasks:
- name: copy
fetch:
src: /home/student/ansible/2222.txt
dest: /root/1.txt
使用fetch模块
(将客户端文件复制到服务器,并且会复制整个目录)
---
- name: fetch
hosts: all
tasks:
- name: fetch
fetch:
src: /root/1.txt
dest: /home/student/ansible
使用blockinfile
(还有一个lineinfile,line:表示输出内容)
---
- name: blockinfile
hosts: all
tasks:
- name: 333
blockinfile:
path: /root/hostname
state: present
create: yes
block: |
主机名 "{{ansible_hostname}}"
练习:
1.在web组的主机上创建一个目录/webdev 2.此目录的属组是webdev 3.此目录的权限为 属主:rwx , 属组:rwx, others: r-x 4.为此目录设置sgid权限 5.将/webdev 目录链接到/var/www/html/webdev 6.创建一个文件/webdev/index.html ,并写入一行文本"development"
---
- name: web
hosts: all
tasks:
- name: create webdev
group:
name: webdev
state: present
- name: create file
file:
path: /webdev
state: directory
group: webdev
owner: "2775" #2为sgid 补充:4为suid
setype: httpd_sys_content_t
- name: install httpd
yum:
name: httpd
state: present
- name: service httpd
service:
name: httpd
state: started
enabled: yes
- name: service firewalld
service:
name: firewalld
state: started
enabled: yes
- name: firewalld permanent
firewalld:
service: http
state: enabled
permanent: yes
immediate: yes
- name: line in file
file:
src: /webdev
dest: /var/www/html/webdev
state: link
- name: create index.html
lineinfile:
path: /webdev/index.html
line: development
create: yes
使用Jinja2模板部署自定义文件
jinja2 是在python中被广泛使用的一个模板引擎,ansible 可以通过jinja2模板来实现动态表达式和变量引用。 template模块 使用来复制模板到受管主机的,template 使用jinja2模板语言,会对模板文件中的变量进行替换。
需求:在目标主机上生成一个文件/tmp/host.txt, 文件中包含目标主机的 ip主机名。
创建一个模板文件
vim ip.j2
IP: "{{ansible_default_ipv4.address}}"
HOSTNAME: "{{ansible_hostname}}"
deamon: "{{ansible_fqdn}}"
使用template 模块
vim j2.yml
---
- name: j2
hosts: all
tasks:
- template:
src: ip.j2
dest: /root/ip.txt
when: ansible_hostname in groups.a
jinja2 模板语法
shell语句中的判断:
#!/bin/bash
ip=172.1.1.1
if [ $ip != "172.1.1.1" ];then
echo "ip地址不正确"
else
echo "ip地址正确"
fi
jinja2中的if判断
语法格式:
{% if 判断条件 %}
操作1 mkdir 222.txt
操作2 rm -rf 222.txt
{% elif 判断条件 %}}
操作1
操作2
{% else %}
操作1
操作2
{% endif %}
jinja2中的for循环
shell脚本中的for循环
for i in a b c;
do 命令 $i ; done
jinja2中的for循环
{% for i in groups.a %}
{{ansible变量}}{{ansible变量}}
{% endfor %}
案例(生成hosts文件):
在目标主机的/tem文件夹中输出myhost文件,其中内容格式和/etc/hosts文件格式相同
vim hosts.j2
{% for i in groups.all %}
{{hostvars[i].ansible_default_ipv4.address}} {{hostvars[i].ansible_fqdn}} {{hostvars[i].ansible_hostname}}
{% endfor %}
vim hosts.yml
---
- hosts: all
tasks:
- template:
src: hosts.j2
dest: /tmp/myhosts
when: ansible_hostname in groups.a
案例:(生成硬件报告):
注:regexp用于匹配,格式regexp: “^匹配内容.*”
---
- name: 生成硬件报告
hosts: all
tasks:
- name: copy file
copy:
src: hwreport.txt
dest: /root/hwreport.txt
- name: hostname
lineinfile:
line: "Inventroy hostname:{{ansible_hostname}}"
regexp: "^Inventroy hostname.*" #regexp用于匹配,格式regexp: "^匹配内容.*"
path: /root/hwreport.txt
- name: MB
lineinfile:
line: "Total memory in MB:{{ansible_memtotal_mb}}"
regexp: "^Total memory in MB.*"
path: /root/hwreport.txt
- name: BIOS
lineinfile:
line: "BIOS version:{{ansible_bios_version}}"
regexp: "^BIOS version.*"
path: /root/hwreport.txt
|