#ansible常用模块
文章目录
一、部署web服务器
1、部署yum仓库
2、安装httpd
3、讲/var/www/html目录做一个软链接,到/www
4、在/www中新建index.html,内容为my name is chenyu(chenyu为你们自己名字的全拼)
5、实现在ansible中能够使用http 😕/node1访问到该网页内容
检查连接
[student@server ansible]$ ansible node1 -m ping
node1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
部署yum仓库
[root@node1 ~]# cd /etc/yum.repos.d/
[root@node1 yum.repos.d]# rm -rf *
[student@server ansible]$ ansible node1 -m yum_repository -a 'file=server name=11 description=11 baseurl=file:///mnt/BaseOS enabled=yes gpgcheck=no'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "11",
"state": "present"
}
[student@server ansible]$ ansible node1 -m yum_repository -a 'file=server name=22 description=22 baseurl=file:///mnt/AppStream enabled=yes gpgcheck=no'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "22",
"state": "present"
}
挂载
[student@server ansible]$ ansible node1 -m mount -a 'src=/dev/cdrom path=/mnt fstype=iso9660 state=mounted'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dump": "0",
"fstab": "/etc/fstab",
"fstype": "iso9660",
"name": "/mnt",
"opts": "defaults",
"passno": "0",
"src": "/dev/cdrom"
}
查看
[root@node1 yum.repos.d]# cat server.repo
[11]
baseurl = file:///mnt/BaseOS
enabled = 1
gpgcheck = 0
name = 11
[22]
baseurl = file:///mnt/BaseOS
enabled = 1
gpgcheck = 0
name = 22
[root@node1 ~]# cat /etc/fstab | grep /mnt
/dev/cdrom /mnt iso9660 defaults 0 0
[root@node1 ~]# df -h | grep /mnt
/dev/sr0 9.0G 9.0G 0 100% /mnt
安装httpd
[student@server ansible]$ ansible node1 -m yum -a 'name=httpd state=present'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: apr-util-1.6.1-6.el8.x86_64",
"Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
"Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
"Installed: mailcap-2.1.48-3.el8.noarch",
"Installed: mod_http2-1.15.7-2.module_el8.3.0+477+498bb568.x86_64",
"Installed: httpd-2.4.37-30.module_el8.3.0+462+ba287492.0.1.x86_64",
"Installed: httpd-filesystem-2.4.37-30.module_el8.3.0+462+ba287492.0.1.noarch",
"Installed: apr-1.6.3-11.el8.x86_64",
"Installed: httpd-tools-2.4.37-30.module_el8.3.0+462+ba287492.0.1.x86_64",
"Installed: centos-logos-httpd-80.5-2.el8.noarch"
]
}
设置开机自启
[student@server ansible]$ ansible node1 -m service -a 'name=httpd state=started enabled=yes'
将/var/www/html目录做一个软链接,到/www
[student@server ansible]$ ansible node1 -m file -a 'dest=/www src=/var/www/html state=link '
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/www",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"secontext": "unconfined_u:object_r:root_t:s0",
"size": 13,
"src": "/var/www/html",
"state": "link",
"uid": 0
}
[root@node1 ~]# ll / | grep www
lrwxrwxrwx. 1 root root 13 Oct 24 16:34 www -> /var/www/html
在/www中新建index.html,内容为my name is humingyang
[student@server ansible]$ ansible node1 -m file -a 'path=/www/index.html state=touch'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/www/index.html",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:httpd_sys_content_t:s0",
"size": 0,
"state": "file",
"uid": 0
}
[student@server ansible]$ ansible node1 -m lineinfile -a 'path=/www/index.html line="my name is humingyang"'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
查看
[root@node1 www]# ll
total 4
-rw-r--r--. 1 root root 23 Oct 24 16:56 index.html
[root@node1 www]# cat index.html
my name is humingyang
[root@node1 www]# curl 192.168.100.20
my name is humingyang
实现在ansible中能够使用http /node1访问到该网页内容
[student@server ansible]$ ansible node1 -m firewalld -a 'service=http permanent=yes state=enabled immediate=yes'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "Permanent and Non-Permanent(immediate) operation, Changed service http to enabled"
}
[student@server ansible]$ ansible node1 -m firewalld -a 'rich_rule="rule family=ipv4 source address=192.168.100.0/24 service name=http accept" permanent=yes state=enabled immediate=yes'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "Permanent and Non-Permanent(immediate) operation, Changed rich_rule rule family=ipv4 source address=192.168.100.0/24 service name=http accept to enabled"
}
[student@server ansible]$ curl http://node1
my name is humingyang