一、
1、使用debug模块,显示当前受管主机的dns服务器的ip地址。
作业代码:
---
- host: all
tasks:
- debug:
var: ansible_facts.default_ipv4.address
2、将createuser.fact文件传输到受管主机上作为自定义事实变量文件(/etc/ansible/facts.d/),该文件的内容如下:
[general]
username = wujing
mima = $6$UAxRbhT3kyc=$AxQfYYP8dhCv750tH.rmrmv690ugT/lZU8OGEqSs7xZR0rEvSIurs4w/W88wUiY3hNnZBWS4uCaGUCdztI9An.
使用username和mima变量创建用户并设置该用户的密码。
作业代码:
---
- hosts: all
tasks:
- file:
path: /etc/ansible/facts.d/
state: directory
recurse: yes
- name: copy file
copy:
src: createuser.fact
dest: /etc/ansible/facts.d/createuser.fact
- debug:
var: ansible_facts.ansible_local.createuser.general.username
- name: create user
user:
name: "{{ ansible_fact.ansible_local.createuser.general.username }}"
password: "{{ ansible_fact.ansible_local.createuser.general.mima }}"
3、向受管主机的/home/file文件里面写入内容如下:
hostname=当前主机的名字
memory=当前主机的内存大小
BIOS version=当前主机的bios的版本
distribution=当前linux主机的发行版本信息
Size of disk device is 当前主机的磁盘大小
编辑debug.yml,即 “vim debug.yml”
---
- host: all
tasks:
- debug:
msg: "{{ ansible_facts.memtotal_mb }} {{ ansible_facts.bios_version }} {{ ansible_facts.distribution }} {{ ansible_facts.devices.nvme0n1.size }}"
作业代码:
---
- hosts: al
tasks:
- shell: touch /home/file
- name: hostname
lineinfile:
path: /home/file
regexp: '^hostname'
line: hostname={{ ansible_facts.hostname }}
- name: memory
lineinfile:
path: /home/file
regexp: '^memory'
line: memory={{ ansible_facts.memtotal_mb }}
- name: bios_version
lineinfile:
path: /home/file
regexp: '^bios_version'
line: bios_version={{ ansible_facts.bios_version }}
- name: distribution
lineinfile:
path: /home/file
regexp: '^distribution'
line: bios_version={{ ansible_facts.distribution }}
- name: devices.nvme0n1.size
lineinfile:
path: /home/file
regexp: '^devices.nvme0n1.size'
line: bios_version={{ ansible_facts.devices.nvme0n1.size }}
二、
1、如果当前受管主机的根分区容量大于1G,则安装httpd和mariadb-server软件包,如果httpd和mariadb服务未运行则运行该服务。
作业代码:
---
- hosts: all
tasks:
- name: install pkg
yum:
name:
- httpd
- mariadb-server
when: item.mount == "/" and item.size_total > 1000000000
loop: "{{ ansible_facts.mounts }}"
- name: start service
service:
name: "{{ item }}"
state: started
loop:
- httpd
- mariadb
2、将example.conf文件复制到/etc/httpd/conf.d/目录,example.conf文件内容如下:
<virtualhost *:80>
servername 0.0.0.0
documentroot /var/www/html
</virtualhost>
<directory /var/www/html>
allowoverride none
require all granted
</directory>
如果/etc/httpd/conf.d/目录下的文件更新,则重启httpd服务。配置/var/www/html/index.html文件内容如下:
zuoye
作业代码:
---
- host: node02
tasks:
- copy:
src: example.conf
dest: /etc/httpd/conf.d/example.conf
hostify: restart httpd
- copy:
content: "zuoye\n"
dext: /var/www/html/index.html
- service:
name: firewalld
state: stopped
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
“vim example.conf”
<virtualhost *:80>
servername 0.0.0.0
documentroot /var/www/html
</virtualhost>
<directory /var/www/html>
allowoverride none
require all granted
</directory>
3、创建一个playbook,要求如下:
该playbook运行在所有受控节点
该playbook覆盖/etc/message文件的内容
在dev主机组的主机上,内容是:Development
在test主机组的主机上,内容是:Test
作业代码:
---
- host: all
tasks:
- copy:
content: "development"
dest: /etc/message
when: inventory_hostname in groups.dev
- copy:
content: "Test "
dest: /etc/message
when: inventory_hostname in groups.test