"owner": "root",
"path": "/scripts",
"secontext": "unconfined_u:object_r:default_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}
//在受控主机上scripts目录中创建test文件
[root@ansible ~]# ansible 192.168.118.130 -m file -a 'path=/scripts/test state=touch'192.168.118.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/scripts/test",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:default_t:s0",
"size": 0,
"state": "file",
"uid": 0
}
//在受控主机上查看
[root@localhost ~]# ll /scripts/
total 0
-rw-r--r--. 1 root root 0 Oct 23 18:57 test
ansible实现lnmp架构
环境介绍
|
主机名 |
ip |
服务 |
系统 |
控制主机 |
ansible |
192.168.118.129 |
ansible(已安装) |
centos8 |
受控主机 |
nginx |
192.168.118.130 |
nginx |
centos8 |
受控主机 |
mysql |
192.168.118.131 |
mysql |
centos8 |
受控主机 |
php |
192.168.118.132 |
php |
centos8 |
配置清单
//修改配置文件设置清单位置
[root@ansible ~]# cd /etc/ansible/
[root@ansible ~]# vim ansible.cfg
inventory = /etc/ansible/inventory
//配置清单
[root@ansible ~]# vim inventory
[nginx]
192.168.118.130
[mysql]
192.168.118.131
[php]
192.168.118.132
受管主机安装python3
[root@ansible ~]# ansible all -m yum -a 'name=python3 state=present'
受管主机关闭防火墙和selinux
[root@ansible ~]# ansible all -a 'setenforce 0'
[root@ansible ~]# ansible all -m selinux -a 'state=disabled'
[root@ansible ~]# ansible all -m service -a 'name=firewalld state=stopped enabled=no'
受控主机nginx安装配置nginx
//创建系统用户nginx
[root@ansible ~]# ansible nginx -m user -a 'name=nginx system=yes shell=/sbin/nologin state=present'
//安装依赖包
[root@ansible ~]# ansible nginx -m yum -a 'name=pcre-devel,openssl,openssl-devel,gd-devel,gcc,gcc-c++,make,wget,vim state=present'
//下载nginx并解压
[root@ansible ~]# ansible nginx -a 'wget http://nginx.org/download/nginx-1.20.2.tar.gz'
[root@ansible ~]# ansible nginx -a 'tar xf nginx-1.20.2.tar.gz'
//编写编译脚本,然后进行编译安装
[root@ansible ~]# mkdir /scripts
[root@ansible ~]# vim /scripts/nginx.sh
#!/bin/bash
cd nginx-1.20.2
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
[root@ansible ~]# ansible nginx -m script -a '/scripts/nginx.sh'
[root@ansible ~]# ansible nginx -m shell -a 'cd nginx-1.20.2 && make && make install'
//配置环境变量
[root@ansible ~]# ansible nginx -m shell -a 'echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh'
[root@ansible ~]# ansible nginx -m shell -a 'source /etc/profile.d/nginx.sh'
//编写service文件
[root@ansible ~]# vim /scripts/nginxservice.sh
#!/bin/bash
cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
Exec