管理变量和事实
1、使用debug模块,显示当前受管主机的dns服务器的ip地址。
yaml配置
---
- name : Obtaining the dns
hosts : all
tasks :
- name : print dns
debug :
var : ansible_facts.dns.nameservers
执行结果
[ xiaoming@master-85 ~] $ ansible-playbook dns.yml
PLAY [ Obtaining the dns] *****************************************************************************************************************************************************************************************
TASK [ Gathering Facts] *******************************************************************************************************************************************************************************************
ok: [ node2]
ok: [ node1]
TASK [ print dns] *************************************************************************************************************************************************************************************************
ok: [ node1] = > {
"ansible_facts.dns.nameservers" : [
"192.168.21.2"
]
}
ok: [ node2] = > {
"ansible_facts.dns.nameservers" : [
"192.168.21.2"
]
}
PLAY RECAP *******************************************************************************************************************************************************************************************************
node1 : ok = 2 changed = 0 unreachable = 0 failed = 0 skipped = 0 rescued = 0 ignored = 0
node2 : ok = 2 changed = 0 unreachable = 0 failed = 0 skipped = 0 rescued = 0 ignored = 0
2、将createuser.fact文件传输到受管主机上作为自定义事实变量文件(/etc/ansible/facts.d/),该文件的内容如下:
文件内容
[ general]
username = wujing
mima = $6 $UAxRbhT3kyc = $AxQfYYP8dhCv750tH .rmrmv690ugT/lZU8OGEqSs7xZR0rEvSIurs4w/W88wUiY3hNnZBWS4uCaGUCdztI9An.
配置文件
---
- name : touch /etc/ansible/facts.d/createuser.fact
hosts : all
tasks :
- name : facts.d
file :
path : /etc/ansible/facts.d
state : directory
- name : copy content
copy :
content : |
[general]
username=wujing
mima=$6$UAxRbhT3kyc=$AxQfYYP8dhCv750tH.rmrmv690ugT/lZU8OGEqSs7xZR0rEvSIurs4w/W88wUiY3hNnZBWS4uCaGUCdztI9An.
dest : /etc/ansible/facts.d/createuser.fact
- name :
user :
name : "{{ ansible_facts.ansible_local.createuser.general.username }}"
password : "{{ ansible_facts.ansible_local.createuser.general.mima }}"
- name :
command : id { { ansible_facts.ansible_local.createuser.general.username } }
register : whoname
- debug :
var : whoname
执行结果
[ xiaoming@master-85 ~] $ ansible-playbook user.yml
PLAY [ touch /etc/ansible/facts.d/createuser.fact] ***************************************************************************************************************************************************************
TASK [ Gathering Facts] *******************************************************************************************************************************************************************************************
ok: [ node2]
ok: [ node1]
TASK [ facts.d] ***************************************************************************************************************************************************************************************************
ok: [ node2]
ok: [ node1]
TASK [ copy content] **********************************************************************************************************************************************************************************************
ok: [ node2]
ok: [ node1]
TASK [ user] ******************************************************************************************************************************************************************************************************
ok: [ node2]
ok: [ node1]
TASK [ command] ***************************************************************************************************************************************************************************************************
changed: [ node2]
changed: [ node1]
TASK [ debug] *****************************************************************************************************************************************************************************************************
ok: [ node1] = > {
"whoname" : {
"changed" : true,
"cmd" : [
"id" ,
"wujing"
] ,
"delta" : "0:00:00.004530" ,
"end" : "2022-11-26 15:17:56.484263" ,
"failed" : false,
"rc" : 0 ,
"start" : "2022-11-26 15:17:56.479733" ,
"stderr" : "" ,
"stderr_lines" : [ ] ,
"stdout" : "uid=2002(wujing) gid=2002(wujing) groups=2002(wujing)" ,
"stdout_lines" : [
"uid=2002(wujing) gid=2002(wujing) groups=2002(wujing)"
]
}
}
ok: [ node2] = > {
"whoname" : {
"changed" : true,
"cmd" : [
"id" ,
"wujing"
] ,
"delta" : "0:00:00.002787" ,
"end" : "2022-11-26 15:17:56.714160" ,
"failed" : false,
"rc" : 0 ,
"start" : "2022-11-26 15:17:56.711373" ,
"stderr" : "" ,
"stderr_lines" : [ ] ,
"stdout" : "uid=2002(wujing) gid=2002(wujing) groups=2002(wujing)" ,
"stdout_lines" : [
"uid=2002(wujing) gid=2002(wujing) groups=2002(wujing)"
]
}
}
PLAY RECAP *******************************************************************************************************************************************************************************************************
node1 : ok = 6 changed = 1 unreachable = 0 failed = 0 skipped = 0 rescued = 0 ignored = 0
node2 : ok = 6 changed = 1 unreachable = 0 failed = 0 skipped = 0 rescued = 0 ignored = 0
3、向受管主机的/home/file文件里面写入内容如下:
内容
hostname = 当前主机的名字
memory = 当前主机的内存大小
BIOS version = 当前主机的bios的版本
distribution = 当前linux主机的发行版本信息
Size of disk device is 当前主机的磁盘大小
配置文件
---
- name : /home/file to node1
hosts : all
tasks :
- name : file to Redhat8
copy :
content : |
hostname={{ ansible_facts.hostname }}
memory={{ ansible_facts.memtotal_mb }}
BIOS version={{ ansible_facts.bios_version }}
distribution={{ ansible_facts.distribution }}
Size of disk device is {{ ansible_facts.devices.nvme0n1.size }}
dest : /home/file
when :
- ansible_distribution == "RedHat"
- ansible_distribution_major_version == "8"
- name : cat file1
command : cat /home/file
register : file1
when :
- ansible_distribution == "RedHat"
- ansible_distribution_major_version == "8"
- debug :
var : file1
when :
- ansible_distribution == "RedHat"
- ansible_distribution_major_version == "8"
- name : /home/file to node2
hosts : all
tasks :
- name : file to Centos7
copy :
content : |
hostname={{ ansible_facts.hostname }}
memory={{ ansible_facts.memtotal_mb }}
BIOS version={{ ansible_facts.bios_version }}
distribution={{ ansible_facts.distribution }}
Size of disk device is {{ ansible_facts.devices.sda.size }}
dest : /home/file
when :
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "7"
- name : cat file2
command : cat /home/file
register : file2
when :
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "7"
- debug :
var : file2
when :
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "7"
执行结果
[ xiaoming@master-85 ~] $ ansible-playbook copy.yml
PLAY [ /home/file to node1] *****************************************************************************************************************************************************************************************************************
TASK [ Gathering Facts] *********************************************************************************************************************************************************************************************************************
ok: [ node2]
ok: [ node1]
TASK [ file to Redhat8] *********************************************************************************************************************************************************************************************************************
skipping: [ node2]
ok: [ node1]
TASK [ cat file1] ***************************************************************************************************************************************************************************************************************************
skipping: [ node2]
changed: [ node1]
TASK [ debug] *******************************************************************************************************************************************************************************************************************************
ok: [ node1] = > {
"file1" : {
"changed" : true,
"cmd" : [
"cat" ,
"/home/file"
] ,
"delta" : "0:00:00.002454" ,
"end" : "2022-11-27 21:58:29.977069" ,
"failed" : false,
"rc" : 0 ,
"start" : "2022-11-27 21:58:29.974615" ,
"stderr" : "" ,
"stderr_lines" : [ ] ,
"stdout" : "hostname=node-1\n memory=1790\n BIOS version=6.00\n distribution=RedHat \n Size of disk device is 20.00 GB" ,
"stdout_lines" : [
"hostname=node-1" ,
"memory=1790" ,
"BIOS version=6.00" ,
"distribution=RedHat " ,
"Size of disk device is 20.00 GB"
]
}
}
skipping: [ node2]
PLAY [ /home/file to node2] *****************************************************************************************************************************************************************************************************************
TASK [ Gathering Facts] *********************************************************************************************************************************************************************************************************************
ok: [ node2]
ok: [ node1]
TASK [ file to Centos7] *********************************************************************************************************************************************************************************************************************
skipping: [ node1]
ok: [ node2]
TASK [ cat file2] ***************************************************************************************************************************************************************************************************************************
skipping: [ node1]
changed: [ node2]
TASK [ debug] *******************************************************************************************************************************************************************************************************************************
skipping: [ node1]
ok: [ node2] = > {
"file2" : {
"changed" : true,
"cmd" : [
"cat" ,
"/home/file"
] ,
"delta" : "0:00:00.002673" ,
"end" : "2022-11-27 21:58:31.753503" ,
"failed" : false,
"rc" : 0 ,
"start" : "2022-11-27 21:58:31.750830" ,
"stderr" : "" ,
"stderr_lines" : [ ] ,
"stdout" : "hostname=node-2\n memory=1823\n BIOS version=6.00\n distribution=CentOS\n Size of disk device is 20.00 GB" ,
"stdout_lines" : [
"hostname=node-2" ,
"memory=1823" ,
"BIOS version=6.00" ,
"distribution=CentOS" ,
"Size of disk device is 20.00 GB"
]
}
}
PLAY RECAP *********************************************************************************************************************************************************************************************************************************
node1 : ok = 5 changed = 1 unreachable = 0 failed = 0 skipped = 3 rescued = 0 ignored = 0
node2 : ok = 5 changed = 1 unreachable = 0 failed = 0 skipped = 3 rescued = 0 ignored = 0
任务控制
1、如果当前受管主机的根分区容量大于1G,则安装httpd和mariadb-server软件包,如果httpd和mariadb服务未运行则运行该服务。
配置文件
---
- name : pan duan /
hosts : all
tasks :
- name : install packages
yum :
name : "{{ packages }}"
vars :
packages :
- httpd
- mariadb- server
loop : "{{ ansible_mounts }}"
when :
- item.mount == "/"
- item.size_available > 1024*1024*1024
- name : start packages
service :
name : "{{ item }}"
enabled : true
state : started
loop :
- httpd
- mariadb
执行结果
[ xiaoming@master-85 ~] $ ansible-playbook server.yml
PLAY [ pan duan /] **************************************************************************************************************************************************************************************************************************
TASK [ Gathering Facts] *********************************************************************************************************************************************************************************************************************
ok: [ node2]
ok: [ node1]
TASK [ install packages] ********************************************************************************************************************************************************************************************************************
skipping: [ node2] = > ( item= { 'block_used' : 39957 , 'uuid' : '4ec34393-baab-4005-875a-eb662814fd65' , 'size_total' : 206213120 , 'block_total' : 50345 , 'mount' : '/boot' , 'block_available' : 10388 , 'size_available' : 42549248 , 'fstype' : 'xfs' , 'inode_total' : 83552 , 'options' : 'rw,seclabel,relatime,attr2,inode64,noquota' , 'device' : '/dev/sda1' , 'inode_used' : 328 , 'block_size' : 4096 , 'inode_available' : 83224 } )
ok: [ node2] = > ( item= { 'block_used' : 1967476 , 'uuid' : '5d2e1730-7a29-4ead-8251-08bf79501600' , 'size_total' : 21250441216 , 'block_total' : 5188096 , 'mount' : '/' , 'block_available' : 3220620 , 'size_available' : 13191659520 , 'fstype' : 'xfs' , 'inode_total' : 10381312 , 'options' : 'rw,seclabel,relatime,attr2,inode64,noquota' , 'device' : '/dev/mapper/centos-root' , 'inode_used' : 117933 , 'block_size' : 4096 , 'inode_available' : 10263379 } )
ok: [ node1] = > ( item= { 'mount' : '/' , 'device' : '/dev/mapper/rhel-root' , 'fstype' : 'xfs' , 'options' : 'rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota' , 'size_total' : 20935868416 , 'size_available' : 15540719616 , 'block_size' : 4096 , 'block_total' : 5111296 , 'block_available' : 3794121 , 'block_used' : 1317175 , 'inode_total' : 10227712 , 'inode_available' : 10102457 , 'inode_used' : 125255 , 'uuid' : 'c531d66b-450c-4a7c-a094-227858955630' } )
skipping: [ node1] = > ( item= { 'mount' : '/boot' , 'device' : '/dev/nvme0n1p1' , 'fstype' : 'xfs' , 'options' : 'rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota' , 'size_total' : 518684672 , 'size_available' : 260423680 , 'block_size' : 4096 , 'block_total' : 126632 , 'block_available' : 63580 , 'block_used' : 63052 , 'inode_total' : 256000 , 'inode_available' : 255690 , 'inode_used' : 310 , 'uuid' : 'd5580ebf-c87b-4677-9164-a4c941565b89' } )
TASK [ start packages] **********************************************************************************************************************************************************************************************************************
ok: [ node2] = > ( item= httpd)
ok: [ node1] = > ( item= httpd)
changed: [ node1] = > ( item= mariadb)
changed: [ node2] = > ( item= mariadb)
PLAY RECAP *********************************************************************************************************************************************************************************************************************************
node1 : ok = 3 changed = 1 unreachable = 0 failed = 0 skipped = 0 rescued = 0 ignored = 0
node2 : ok = 3 changed = 1 unreachable = 0 failed = 0 skipped = 0 rescued = 0 ignored = 0
[ xiaoming@master-85 ~] $ ansible-playbook server.yml
PLAY [ pan duan /] **************************************************************************************************************************************************************************************************************************
TASK [ Gathering Facts] *********************************************************************************************************************************************************************************************************************
ok: [ node1]
ok: [ node2]
TASK [ install packages] ********************************************************************************************************************************************************************************************************************
skipping: [ node2] = > ( item= { 'block_used' : 39957 , 'uuid' : '4ec34393-baab-4005-875a-eb662814fd65' , 'size_total' : 206213120 , 'block_total' : 50345 , 'mount' : '/boot' , 'block_available' : 10388 , 'size_available' : 42549248 , 'fstype' : 'xfs' , 'inode_total' : 83552 , 'options' : 'rw,seclabel,relatime,attr2,inode64,noquota' , 'device' : '/dev/sda1' , 'inode_used' : 328 , 'block_size' : 4096 , 'inode_available' : 83224 } )
ok: [ node2] = > ( item= { 'block_used' : 1967486 , 'uuid' : '5d2e1730-7a29-4ead-8251-08bf79501600' , 'size_total' : 21250441216 , 'block_total' : 5188096 , 'mount' : '/' , 'block_available' : 3220610 , 'size_available' : 13191618560 , 'fstype' : 'xfs' , 'inode_total' : 10381312 , 'options' : 'rw,seclabel,relatime,attr2,inode64,noquota' , 'device' : '/dev/mapper/centos-root' , 'inode_used' : 117943 , 'block_size' : 4096 , 'inode_available' : 10263369 } )
ok: [ node1] = > ( item= { 'mount' : '/' , 'device' : '/dev/mapper/rhel-root' , 'fstype' : 'xfs' , 'options' : 'rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota' , 'size_total' : 20935868416 , 'size_available' : 15529017344 , 'block_size' : 4096 , 'block_total' : 5111296 , 'block_available' : 3791264 , 'block_used' : 1320032 , 'inode_total' : 10227712 , 'inode_available' : 10102445 , 'inode_used' : 125267 , 'uuid' : 'c531d66b-450c-4a7c-a094-227858955630' } )
skipping: [ node1] = > ( item= { 'mount' : '/boot' , 'device' : '/dev/nvme0n1p1' , 'fstype' : 'xfs' , 'options' : 'rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota' , 'size_total' : 518684672 , 'size_available' : 260423680 , 'block_size' : 4096 , 'block_total' : 126632 , 'block_available' : 63580 , 'block_used' : 63052 , 'inode_total' : 256000 , 'inode_available' : 255690 , 'inode_used' : 310 , 'uuid' : 'd5580ebf-c87b-4677-9164-a4c941565b89' } )
TASK [ start packages] **********************************************************************************************************************************************************************************************************************
ok: [ node2] = > ( item= httpd)
ok: [ node1] = > ( item= httpd)
ok: [ node2] = > ( item= mariadb)
ok: [ node1] = > ( item= mariadb)
PLAY RECAP *********************************************************************************************************************************************************************************************************************************
node1 : ok = 3 changed = 0 unreachable = 0 failed = 0 skipped = 0 rescued = 0 ignored = 0
node2 : ok = 3 changed = 0 unreachable = 0 failed = 0 skipped = 0 rescued = 0 ignored = 0
2、将example.conf.j2文件复制到/etc/httpd/conf.d/目录,example.conf.j2文件内容如下:
文件
< virtualhost { { ansible_facts.default_ipv4.address } } :80 >
servername { { ansible_facts.default_ipv4.address } }
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
配置文件
---
- name : copy example.conf
hosts : all
tasks :
- name :
template :
src : /home/xiaoming/example.conf.j2
dest : /etc/httpd/conf.d/example.conf
notify :
- restart httpd server
- name :
template :
src : /home/xiaoming/index.html
dest : /var/www/html/index.html
handlers :
- name : restart httpd server
service :
name : httpd
state : restarted
执行结果
[ xiaoming@master-85 ~] $ ansible-playbook file-server.yml
PLAY [ copy example.conf] *****************************************************************************************************************************************************************************************************************************************************
TASK [ Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
ok: [ node1]
ok: [ node2]
TASK [ template] **************************************************************************************************************************************************************************************************************************************************************
changed: [ node2]
changed: [ node1]
TASK [ template] **************************************************************************************************************************************************************************************************************************************************************
ok: [ node2]
ok: [ node1]
RUNNING HANDLER [ restart httpd server] ***************************************************************************************************************************************************************************************************************************************
changed: [ node1]
changed: [ node2]
PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
node1 : ok = 4 changed = 2 unreachable = 0 failed = 0 skipped = 0 rescued = 0 ignored = 0
node2 : ok = 4 changed = 2 unreachable = 0 failed = 0 skipped = 0 rescued = 0 ignored = 0
3、创建一个playbook,要求如下: 该playbook运行在所有受控节点 该playbook覆盖/etc/message文件的内容 在dev主机组的主机上,内容是:Development 在test主机组的主机上,内容是:Test
配置文件
---
- name : play
hosts : all
tasks :
- name : copy Development
copy :
content : "Development\n"
dest : /etc/message
when : inventory_hostname in groups.dev
- name : copy Test
copy :
content : "Test\n"
dest : /etc/message
when : inventory_hostname in groups.test
- name : yan zheng
command : cat /etc/message
register : message
- debug :
var : message
执行结果
[ xiaoming@master-85 ~] $ ansible-playbook play.yml
PLAY [ play] ******************************************************************************************************************************************************************************************************
TASK [ Gathering Facts] *******************************************************************************************************************************************************************************************
ok: [ node2]
ok: [ node1]
TASK [ copy Development] ******************************************************************************************************************************************************************************************
skipping: [ node2]
ok: [ node1]
TASK [ copy Test] *************************************************************************************************************************************************************************************************
skipping: [ node1]
ok: [ node2]
TASK [ yan zheng] *************************************************************************************************************************************************************************************************
changed: [ node2]
changed: [ node1]
TASK [ debug] *****************************************************************************************************************************************************************************************************
ok: [ node1] = > {
"message" : {
"changed" : true,
"cmd" : [
"cat" ,
"/etc/message"
] ,
"delta" : "0:00:00.002647" ,
"end" : "2022-11-26 17:45:12.265146" ,
"failed" : false,
"rc" : 0 ,
"start" : "2022-11-26 17:45:12.262499" ,
"stderr" : "" ,
"stderr_lines" : [ ] ,
"stdout" : "Development" ,
"stdout_lines" : [
"Development"
]
}
}
ok: [ node2] = > {
"message" : {
"changed" : true,
"cmd" : [
"cat" ,
"/etc/message"
] ,
"delta" : "0:00:00.002167" ,
"end" : "2022-11-26 17:45:12.576830" ,
"failed" : false,
"rc" : 0 ,
"start" : "2022-11-26 17:45:12.574663" ,
"stderr" : "" ,
"stderr_lines" : [ ] ,
"stdout" : "Test" ,
"stdout_lines" : [
"Test"
]
}
}
PLAY RECAP *******************************************************************************************************************************************************************************************************
node1 : ok = 4 changed = 1 unreachable = 0 failed = 0 skipped = 1 rescued = 0 ignored = 0
node2 : ok = 4 changed = 1 unreachable = 0 failed = 0 skipped = 1 rescued = 0 ignored = 0