目标1
1、使用debug模块,显示当前受管主机的dns服务器的ip地址。
2、将createuser.fact文件传输到受管主机上作为自定义事实变量文件(/etc/ansible/facts.d/),该文件的内容如下:
[general]
username = wujing
mima = $6$UAxRbhT3kyc=$AxQfYYP8dhCv750tH.rmrmv690ugT/lZU8OGEqSs7xZR0rEvSIurs4w/W88wUiY3hNnZBWS4uCaGUCdztI9An.
使用username和mima变量创建用户并设置该用户的密码。
3、向受管主机的/home/file文件里面写入内容如下:
hostname=当前主机的名字
memory=当前主机的内存大小
BIOS version=当前主机的bios的版本
distribution=当前linux主机的发行版本信息
Size of disk device is 当前主机的磁盘大小
目标2
1、如果当前受管主机的根分区容量大于1G,则安装httpd和mariadb-server软件包,如果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
3、创建一个playbook,要求如下:
该playbook运行在所有受控节点 该playbook覆盖/etc/message文件的内容 在dev主机组主机上,内容是:Development 在test主机组的主机上,内容是:Test
目标1
一、 使用debug模块,显示当前受管主机的dns服务器的ip地址
编写playbook
[root@cent-83master ansible]# vim web.yml
---
- name: first play
hosts: web
tasks:
- name: print dns ip
debug:
var: ansible_facts.dns.nameservers #受管主机的dns服务器IP
校验playbook
[root@cent-83master ansible]# ansible-playbook --syntax-check web.ym
playbook: web.yml
执行playbook
[root@cent-83master ansible]# ansible-playbook web.yml #执行playbook文件
PLAY [fist play] ***********************************************************************
TASK [Gathering Facts] *****************************************************************
ok: [node01]
ok: [node02]
TASK [print dns ip] ********************************************************************
ok: [node01] => {
"ansible_facts.dns.nameservers": [
"114.114.114.114"
]
}
ok: [node02] => {
"ansible_facts.dns.nameservers": [
"8.8.8.8"
]
}
PLAY RECAP *****************************************************************************
node01 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
二、 将createuser.fact文件传输到受管主机上作为自定义事实变量文件(/etc/ansible/facts.d/)
在控制主机上编写createuser.fact文件
[root@cent-83master ansible]# vim createuser.fact
[general]
username = wujing
mima = $6$UAxRbhT3kyc=$AxQfYYP8dhCv750tH.rmrmv690ugT/lZU8OGEqSs7xZR0rEvSIurs4w/W88wUiY3hNnZBWS4uCaGUCdztI9An.
编写用于传输的playbook文件
[root@cent-83master ansible]# vim createuser.yml
---
- name: first play
hosts: web
tasks:
- name: create dirctory
file:
path: /etc/ansible/facts.d/
state: dirctory
- name: transfer file
copy:
src: ./createuser.fact
dest: /etc/ansible/facts.d/
校验playbook
[root@cent-83master ansible]# ansible-playbook --syntax-check createuser.yml
playbook: createuser.yml
[root@cent-83master ansible]#
执行playbook
[root@cent-83master ansible]# ansible-playbook createuser.yml
PLAY [fist play] ***********************************************************************
TASK [Gathering Facts] *****************************************************************
ok: [node01]
ok: [node02]
TASK [create directory] ****************************************************************
changed: [node01]
changed: [node02]
TASK [transfer file] *******************************************************************
changed: [node01]
changed: [node02]
PLAY RECAP *****************************************************************************
node02 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node01 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
编写用于创建用户的playbook文件
[root@cent-83master ansible]# vim useradd.yml
---
- name: first play
hosts: web
tasks:
- name: add user
user:
name: "{{ansible_facts.ansible_local.createuser.general.username}}"
password: "{{ansible_facts.ansible_local.createuser.general.mima}}"
测试playbook
[root@cent-83master ansible]# ansible-playbook --syntax-check useradd.yml
playbook: useradd.yml
[root@cent-83master ansible]#
调用playbook
[root@cent-83master ansible]# ansible-playbook useradd.yml
PLAY [fist play] ***********************************************************************
TASK [Gathering Facts] *****************************************************************
ok: [node01]
ok: [node02]
TASK [add user] ************************************************************************
ok: [node01]
ok: [node02]
PLAY RECAP *****************************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node01 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#查看受管主机上用户创建情况
[root@cent-83master ansible]# ansible web -a 'tail -1 /etc/passwd' -o
node1 | CHANGED | rc=0 | (stdout) wujing:x:2002:2002::/home/wujing:/bin/bash
node0 | CHANGED | rc=0 | (stdout) wujing:x:1001:1001::/home/wujing:/bin/bash
三、向受管主机的/home/file文件里面写入信息
[root@cent-83master ansible]# vim write.yml
---
- name: first play
hosts: web
tasks:
- name: write hostname
copy:
content: "hostname={{ansible_facts.hostname}}"
dest: /home/file
- name: write memory
lineinfile:
path: /home/file
line: "memory={{ansible_facts.memtotal_mb}}"
- name: write bios version
lineinfile:
path: /home/file
line: "BIOS version={{ansible_facts.bios_version}}"
- name: write distribution
lineinfile:
path: /home/file
line: "distribution={{ansible_facts.distribution}}"
- name: write version 8.3 device size
lineinfile:
path: /home/file
line: "Size of disk device is {{ansible_facts.devices.nvme0n1.size}}"
- name: version 7.9 device size
lineinfile:
path: /home/file
line: "Size of disk device is {{ansible_facts.devices.sda.size}}"
when: ansible_facts.device.sda is defined
检验playbook
[root@cent-83master ansible]# ansible-playbook --syntax-check write.yml
playbook: write.yml
[root@cent-83master ansible]#
执行playbook
[root@cent-83master ansible]# ansible-playbook write.yml
PLAY [fist play] ***********************************************************************
TASK [Gathering Facts] *****************************************************************
ok: [node01]
ok: [node02]
TASK [write hostname] ******************************************************************
changed: [node01]
changed: [node02]
TASK [write memory] ********************************************************************
changed: [node01]
changed: [node02]
TASK [write bios verion] ***************************************************************
changed: [node01]
changed: [node02]
TASK [write distribution] **************************************************************
changed: [node01]
changed: [node02]
TASK [version8.2 devices size] *********************************************************
skipping: [node01]
changed: [node02]
TASK [version7.9 devices size] *********************************************************
skipping: [node02]
changed: [node01]
PLAY RECAP *****************************************************************************
node02 : ok=6 changed=5 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
node01 : ok=6 changed=5 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
查看受管主机文件写入情况
[root@cent-83master ansible]# ansible web -a 'cat /home/file'
node01 | CHANGED | rc=0 >>
hostname=node01-79
memory=963
BIOS version=6.00
distribution=CentOS
Size of disk device is 20.00 GB
node02| CHANGED | rc=0 >>
hostname=node02-83
memory=981
BIOS version=6.00
distribution=CentOS
Size of disk device is 20.00 GB
目标2
一、 如果当前受管主机的根分区容量大于1G,则安装httpd和mariadb-server软件包,如果httpd和mariadb服务未运行则运行该服务。
编写playbook文件
[root@cent-83master ansible]# vim installpak.yml
---
- name: first play
hosts: web
vars:
var1:
- httpd
- mariadb-server
tasks:
- name: install httpd and mariadb-server
yum:
name: "{{var1}}"
stste: present
when: item.mount == "/" and item.size_total > 1*1024*1024*1024
loop: "{{ansible_facts.mounts}}"
- name: stsrt service
service:
name: "{{item}}"
stste: restarted
loop:
- httpd
- mariadb
校验playbook文件
[root@cent-83master ansible]# ansible-playbook --syntax-check installpak.yml
playbook: installpak.yml
[root@cent-83master ansible]#
实行playbook
[root@cent-83master ansible]# ansible-playbook installpak.yml
PLAY [fist play] ***********************************************************************
TASK [Gathering Facts] *****************************************************************
ok: [node01]
ok: [node02]
TASK [install httpd and mariadb-server] ************************************************
skipping: [node01] => (item={u'block_used': 35591, u'uuid': u'79a01676-149c-4ed9-a369-7cd9686988fd', u'size_total': 1063256064, u'block_total': 259584, u'mount': u'/boot', u'block_available': 223993, u'size_available': 917475328, u'fstype': u'xfs', u'inode_total': 524288, u'inode_available': 523962, u'device': u'/dev/sda1', u'inode_used': 326, u'block_size': 4096, u'options': u'rw,relatime,attr2,inode64,noquota'})
ok: [node02] => (item={u'block_used': 517483, u'uuid': u'3512f9fe-769a-4964-859e-f400a11f0bd8', u'size_total': 18238930944, u'block_total': 4452864, u'mount': u'/', u'block_available': 3935381, u'size_available': 16119320576, u'fstype': u'xfs', u'inode_total': 8910848, u'inode_available': 8857782, u'device': u'/dev/mapper/cl-root', u'inode_used': 53066, u'block_size': 4096, u'options': u'rw,relatime,attr2,inode64,noquota'})
skipping: [node02] => (item={u'block_used': 53527, u'uuid': u'8f9369c6-bfa6-4307-8526-9ed499730763', u'size_total': 1023303680, u'block_total': 249830, u'mount': u'/boot', u'block_available': 196303, u'size_available': 804057088, u'fstype': u'ext4', u'inode_total': 65536, u'inode_available': 65227, u'device': u'/dev/nvme0n1p1', u'inode_used': 309, u'block_size': 4096, u'options': u'rw,relatime'})
ok: [node01] => (item={u'block_used': 420408, u'uuid': u'6d78b4af-a3f8-42cb-8907-12e5ca9024e0', u'size_total': 18238930944, u'block_total': 4452864, u'mount': u'/', u'block_available': 4032456, u'size_available': 16516939776, u'fstype': u'xfs', u'inode_total': 8910848, u'inode_available': 8876610, u'device': u'/dev/mapper/centos-root', u'inode_used': 34238, u'block_size': 4096, u'options': u'rw,relatime,attr2,inode64,noquota'})
TASK [start service] *******************************************************************
changed: [node01] => (item=httpd)
changed: [node02] => (item=httpd)
changed: [node01] => (item=mariadb)
changed: [node02] => (item=mariadb)
PLAY RECAP *****************************************************************************
node02 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node01 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
二、将example.conf文件复制到/etc/httpd/conf.d/目录
编写playbook文件
[root@cent-83master ansible]# vim zuoye.yml
---
- name: first play
hosts: web
tasks:
- name: transfer conf file
copy:
src: ./example.conf
dest: /etc/httpd/conf.d/
notify: restarted httpd
- name: write index.html
copy:
content: 'zuoye'
dest: /var/www/html/index.html
handlers:
- name: restart httpd
service:
name: httpd
stste: restarted
校验playbook
[root@cent-83master ansible]# ansible-playbook --syntax-check zuoye.yml
playbook: zuoye.yml
[root@cent-83master ansible]#
执行playbook
[root@cent-83master ansible]# ansible-playbook zuoye.yml
PLAY [play] ****************************************************************************
TASK [Gathering Facts] *****************************************************************
ok: [node01]
ok: [node02]
TASK [transfer conf file] **************************************************************
changed: [node01]
changed: [node02]
TASK [write index.html] ****************************************************************
changed: [node01]
changed: [node02]
RUNNING HANDLER [restarted httpd] ******************************************************
changed: [node01]
changed: [node02]
PLAY RECAP *****************************************************************************
node02 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node01 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
测试文件执行情况
[root@cent-83master ansible]# curl http://node02
zuoye
[root@cent-83master ansible]# curl http://node01
zuoye
三、创建一个playbook,要求如下:
该playbook运行在所有受控节点 该playbook覆盖/etc/message文件的内容 在dev主机组主机上,内容是:Development 在test主机组的主机上,内容是:Test
创建playbook
[root@cent-83master ansible]# vim change.yml
---
- name: first play
hosts: all
tsks:
- name: write development
copy:
content: "Development\n"
dett: /etc/message
when: inventory_hostname in groups.dev #只执行dev组的
- name: write test
copy:
content: "Test\n"
dest: /etc/message
when: inventory_hostname in group.test #只执行test组的
校验playbook
[root@cent-83master ansible]# ansible-playbook --syntax-check change.yml
playbook: change.yml
执行playbook
[root@cent-83master ansible]# ansible-playbook change.yml
PLAY [first play] **********************************************************************
TASK [Gathering Facts] *****************************************************************
ok: [node01]
ok: [server83]
ok: [node02]
TASK [write development] ***************************************************************
skipping: [node01]
skipping: [server83]
changed: [node02]
TASK [write test] **********************************************************************
skipping: [node02]
skipping: [server83]
changed: [node01]
PLAY RECAP *****************************************************************************
node02 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
node01 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
server83 : ok=1 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
检查是否写入成功
[root@cent-83master ansible]# ansible all -a 'cat /etc/message'
node01 | CHANGED | rc=0 >>
Test
server83 | FAILED | rc=1 >>
cat: /etc/message: No such file or directorynon-zero return code
node02 | CHANGED | rc=0 >>
Development