ansible常用模块

ansible常用模块

练习

主机ip
serer192.168.87.128
node1192.168.87.129

使用所学的模块撰写ansible临时命令(ad-hoc模式)

一、部署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 zhengbichao

[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 zhengbichao"'
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 zhengbichao
[root@node1 www]# curl 192.168.87.129
my name is zhengbichao

实现在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.87.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.87.0/24 service name=http accept to enabled"
}
[student@server ansible]$ curl http://node1
my name is zhengbichao

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

seven凡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值