1、使用debug模块,显示当前受管主机的dns服务器的ip地址。
//配置事实变量
[xiaoming@centos7 chap04]$ cat play.yml
---
- name: paly1
hosts: web
tasks:
- name: print fact
debug:
var: ansible_facts.dns.nameservers
//运行
[xiaoming@centos7 chap04]$ ansible-playbook play.yml
PLAY [paly1] **********************************************************************************************
TASK [Gathering Facts] ************************************************************************************
ok: [node01]
ok: [node02]
TASK [print fact] *****************************************************************************************
ok: [node01] => {
"ansible_facts.dns.nameservers": [
"192.168.168.2",
"192.168.40.2",
"8.8.8.8"
]
}
ok: [node02] => {
"ansible_facts.dns.nameservers": [
"192.168.168.2",
"192.168.180.1",
"192.21.101.108",
"202.96.128.86",
"202.96.128.166"
]
}
2、将createuser.fact文件传输到受管主机上作为自定义事实变量文件(/etc/ansible/facts.d/),该文件的内容如下:
[general]
username = wujing
mima = $6$UAxRbhT3kyc=$AxQfYYP8dhCv750tH.rmrmv690ugT/lZU8OGEqSs7xZR0rEvSIurs4w/W88wUiY3hNnZBWS4uCaGUCdztI9An.
使用username和mima变量创建用户并设置该用户的密码。
1、在控制节点编辑好createuser.fact文件
[xiaoming@centos7 chap04]$ cat createuser.fact
[general]
username = wujing
mima = $6$UAxRbhT3kyc=$AxQfYYP8dhCv750tH.rmrmv690ugT/lZU8OGEqSs7xZR0rEvSIurs4w/W88wUiY3hNnZBWS4uCaGUCdztI9An.
2、到yml文件配置模块
[xiaoming@centos7 chap04]$ cat play.yml
---
- name: paly1
hosts: node01
tasks:
- name: test //利用copy模块将文件传到受控节点处
copy:
src: /home/xiaoming/ansible/chap04/createuser.fact
dest: /etc/ansible/facts.d/
- debug: //打印
var: ansible_facts.ansible_local.createuser.general
//利用user模块,并且加上自定义事实语句从而可以创建用户和设置用户密码
- name: creat user
user:
name: "{{ ansible_facts.ansible_local.createuser.general.username }}"
password: "{{ ansible_facts.ansible_local.createuser.general.mima }}"
3、测试:
受控主机 node01
[root@node01 facts.d]# vim createuser.fact
[root@node01 facts.d]# pwd
/etc/ansible/facts.d
[root@node01 facts.d]# ll
总用量 4
-rw-r--r--. 1 root root 138 11月 28 00:52 createuser.fact
[root@node01 facts.d]# cat createuser.fact
[general]
username = wujing
mima = $6$UAxRbhT3kyc=$AxQfYYP8dhCv750tH.rmrmv690ugT/lZU8OGEqSs7xZR0rEvSIurs4w/W88wUiY3hNnZBWS4uCaGUCdztI9An.
4、给文件createuser.fact加密
[xiaoming@centos7 chap04]$ ansible-vault encrypt createuser.fact
#查看文件输入密码:
[xiaoming@centos7 chap04]$ ansible-vault view createuser.fact
Vault password:
3、向受管主机的/home/file文件里面写入内容如下:
hostname=当前主机的名字
memory=当前主机的内存大小
BIOS version=当前主机的bios的版本
distribution=当前linux主机的发行版本信息
Size of disk device is 当前主机的磁盘大小
方法一:
[xiaoming@centos7 chap04]$ vim play.yml
//利用 lineinfile 模块,并且利用事实变量去写入内容
---
- name: paly1
hosts: node01
tasks:
- file:
path: /home/file1
state: touch //创建文件
- name: write hostname //主机名
lineinfile:
path: /home/file
regexp: '^hostname='
line: hostname= "{{ ansible_facts.hostname }}"
- name: write memory //主机的内存大小
lineinfile:
path: /home/file
regexp: '^memory='
line: memory= "{{ ansible_facts.memtotal_mb }}"
- name: write BIOS //主机的bios的版本
lineinfile:
path: /home/file
regexp: '^bios='
line: bios= "{{ ansible_facts.bios_version }}"
- name: write distribution //linux主机的发行版本信息
lineinfile:
path: /home/file
regexp: '^distribution='
line: distribution= "{{ ansible_facts.distribution }}"
- name: write devices //当前主机的磁盘大小
lineinfile:
path: /home/file
regexp: '^devices='
line: devices= "{{ ansible_facts.devices.nvme0n1.size }}"
受控主机查看:
[root@node01 facts.d]# vim /home/file
成功写入。