文章目录
使用所学的模块撰写 ansible 临时命令(ad-hoc 模式)
部署 web 服务器:
- 部署 yum 仓库
- 安装 httpd
- 将 /var/www/html 目录做一个软链接,到 /www
- 在 /www 中新建 index.html,内容为 my name is kangyimei
- 实现在 ansible 中能够使用 http://node1 访问到该网页内容
切换用户,进入 ansible 目录
[root@server ~]# su - student
Last login: Fri Oct 21 11:36:00 CST 2022 on pts/0
[student@server ~]$ cd ansible/
删除再新建 /etc/yum.repos.d 目录
[student@server ansible]$ ansible node1 -m file -a 'path=/etc/yum.repos.d/ state=absent'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"path": "/etc/yum.repos.d/",
"state": "absent"
}
[student@server ansible]$ ansible node1 -m file -a 'path=/etc/yum.repos.d/ state=directory'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/etc/yum.repos.d/",
"secontext": "unconfined_u:object_r:system_conf_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}
挂载
[student@server ansible]$ ansible node1 -m mount -a 'path=/mnt src=/dev/cdrom 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"
}
配置本地仓库
[student@server ansible]$ ansible node1 -m yum_repository -a 'file=AppStream name=appstream description=aa baseurl=file:///mnt/AppStream enabled=yes gpgcheck=no'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "appstream",
"state": "present"
}
[student@server ansible]$ ansible node1 -m yum_repository -a 'file=BaseOS name=baseos description=bb baseurl=file:///mnt/BaseOS enabled=yes gpgcheck=no'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "baseos",
"state": "present"
}
安装 httpd
[student@server ansible]$ ansible node1 -m yum -a 'name=httpd state=installed'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: mod_http2-1.15.7-2.module_el8.3.0+477+498bb568.x86_64",
"Installed: apr-util-openssl-1.6.1-6.el8.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",
"Installed: mailcap-2.1.48-3.el8.noarch",
"Installed: apr-util-1.6.1-6.el8.x86_64",
"Installed: apr-util-bdb-1.6.1-6.el8.x86_64"
]
}
创建软链接
[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
}
新建文件,并写入内容
[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 kangyimei"'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
启动 httpd 服务并设置开机自启
[student@server ansible]$ ansible node1 -m service -a 'name=httpd state=started enabled=yes'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveState": "inactive",
"AllowedCPUs": "",
"AllowedMemoryNodes": "",
"BlockIOAccounting": "no",
"BlockIOWeight": "[not set]",
"CPUAccounting": "no",
"CPUAffinity": "",
"CPUAffinityFromNUMA": "no",
"CPUQuotaPerSecUSec": "infinity",
"CPUQuotaPeriodUSec": "infinity",
"CPUSchedulingPolicy": "0",
"CPUSchedulingPriority": "0",
"CPUSchedulingResetOnFork": "no",
"CPUShares": "[not set]",
"CPUUsageNSec": "[not set]",
"CPUWeight": "[not set]",
"ControlPID": "0",
"DefaultMemoryLow": "0",
"DefaultMemoryMin": "0",
"Delegate": "no",
"DevicePolicy": "auto",
"EffectiveCPUs": "",
"EffectiveMemoryNodes": "",
"Environment": "LANG=C",
"ExecMainCode": "0",
"ExecMainExitTimestampMonotonic": "0",
"ExecMainPID": "0",
"ExecMainStartTimestampMonotonic": "0",
"ExecMainStatus": "0",
"ExecReload": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -k graceful ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"ExecStart": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -DFOREGROUND ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",
"FileDescriptorStoreMax": "0",
"GID": "[not set]",
"GuessMainPID": "yes",
"IOAccounting": "no",
"IOSchedulingClass": "0",
"IOSchedulingPriority": "0",
"IOWeight": "[not set]",
"IPAccounting": "no",
"IPEgressBytes": "18446744073709551615",
"IPEgressPackets": "18446744073709551615",
"IPIngressBytes": "18446744073709551615",
"IPIngressPackets": "18446744073709551615",
"LimitAS": "infinity",
"LimitASSoft": "infinity",
"LimitCORE": "infinity",
"LimitCORESoft": "infinity",
"LimitCPU": "infinity",
"LimitCPUSoft": "infinity",
"LimitDATA": "infinity",
"LimitDATASoft": "infinity",
"LimitFSIZE": "infinity",
"LimitFSIZESoft": "infinity",
"LimitLOCKS": "infinity",
"LimitLOCKSSoft": "infinity",
"LimitMEMLOCK": "65536",
"LimitMEMLOCKSoft": "65536",
"LimitMSGQUEUE": "819200",
"LimitMSGQUEUESoft": "819200",
"LimitNICE": "0",
"LimitNICESoft": "0",
"LimitNOFILE": "262144",
"LimitNOFILESoft": "1024",
"LimitNPROC": "7001",
"LimitNPROCSoft": "7001",
"LimitRSS": "infinity",
"LimitRSSSoft": "infinity",
"LimitRTPRIO": "0",
"LimitRTPRIOSoft": "0",
"LimitRTTIME": "infinity",
"LimitRTTIMESoft": "infinity",
"LimitSIGPENDING": "7001",
"LimitSIGPENDINGSoft": "7001",
"LimitSTACK": "infinity",
"LimitSTACKSoft": "8388608",
"LogLevelMax": "-1",
"LogRateLimitBurst": "0",
"LogRateLimitIntervalUSec": "0",
"MainPID": "0",
"MemoryAccounting": "yes",
"MemoryCurrent": "[not set]",
"MemoryHigh": "infinity",
"MemoryLimit": "infinity",
"MemoryLow": "0",
"MemoryMax": "infinity",
"MemoryMin": "0",
"MemorySwapMax": "infinity",
"NFileDescriptorStore": "0",
"NRestarts": "0",
"NUMAMask": "",
"NUMAPolicy": "n/a",
"Nice": "0",
"NonBlocking": "no",
"NotifyAccess": "main",
"OOMScoreAdjust": "0",
"PermissionsStartOnly": "no",
"RemainAfterExit": "no",
"Restart": "no",
"RestartUSec": "100ms",
"Result": "success",
"RootDirectoryStartOnly": "no",
"RuntimeMaxUSec": "infinity",
"SecureBits": "0",
"Slice": "system.slice",
"StandardError": "inherit",
"StandardInput": "null",
"StandardInputData": "",
"StandardOutput": "journal",
"StartupBlockIOWeight": "[not set]",
"StartupCPUShares": "[not set]",
"StartupCPUWeight": "[not set]",
"StartupIOWeight": "[not set]",
"StatusErrno": "0",
"SyslogFacility": "3",
"SyslogLevel": "6",
"SyslogLevelPrefix": "yes",
"SyslogPriority": "30",
"TTYReset": "no",
"TTYVHangup": "no",
"TTYVTDisallocate": "no",
"TasksAccounting": "yes",
"TasksCurrent": "[not set]",
"TasksMax": "11202",
"TimeoutStartUSec": "1min 30s",
"TimeoutStopUSec": "1min 30s",
"TimerSlackNSec": "50000",
"Type": "notify",
"UID": "[not set]",
"UMask": "0022",
"WatchdogTimestampMonotonic": "0",
"WatchdogUSec": "0"
}
}
添加防火墙规则
[student@server ansible]$ ansible node1 -m firewalld -a 'rich_rule="rule family=ipv4 source address=192.168.91.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.91.0/24 service name=http accept to enabled"
}
访问网页
[student@server ansible]$ curl http://node1
my name is kangyimei