ansible模块

ansible

只有在/etc/ansible/hosts文件当中声明的主机,ansible的服务端才可以进行远程模块

模块7 hostname模块

管理远程主机的主机名

ansible 20.0.0.14 -m hostname -a 'name=localhost‘

模块8 copy

用于复制指定主机文件到远程主机的模块

常用参数

dest:指出复制文件的目标和位置,要复制的文件在哪,必须使用绝对路径,如果是源目录指目标也得是目录,如果目标内容存在,会覆盖原有的内容

src:复制文件的源,最好使用绝对路径,源目标是目录,指目标也得是目录

owner:指出文件的所有者

group:指定文件的所在组

content:从主机复制指定的内容到目标主机,用了content就不能使用src

mode:指定复制之后的文件的权限

模块9 file 文件模块根目录无关

owner

group

mode

link创建连接文件

state=touch 创建文件

state=absent 删除文件

state=link

模块10 yum

远程主机上安装与写在软件包

ansible 20.0.0.14 -m yum -a 'name=nginx' 安装

ansible 20.0.0.14 -m yum -a 'name=tree state=absent' 卸载

在目标主机上的操作都是后台运行,不影响其他用户的使用。

模块11 service/systemd

name,设定管理服务的名称

state=stated|stoppde|restarted 管理动作

enabled=true 表示是否开机自启,如果不加,默认就是开机不自动启动

runlevel:融合enabled 开机自启可以设置运行级别,在命令行可以设置但是不生效,必须要playbook当中使用

目标主机安装nginx 设置为开机自启动 设置访问页面访问 this is nginx

ansible 20.0.0.14 -a 'yum -y install epel-release'

ansible 20.0.0.14 -a 'yum -y install nginx'

ansible 20.0.0.14 -m service -a 'name=httpd state=stopped'

ansible 20.0.0.14 -m service -a 'name=nginx state=started'

ansible 20.0.0.14 -m service -a 'enabled=true name=nginx state=restarted'

ansible 20.0.0.14 -m shell -a 'echo "this is nginx" > /usr/share/nginx/html/index.html'

模块12 script模块

运行本地脚本。然后把结果输出到目标主机

模块13 setup

setup查看日志目标主机的环境系统(facts)目标节点

ansible模块_第1张图片

主机清单

ansible

20.0.0.5[1:9]

ansible_host 连接时的ip地址

ansible_port 声明对方的连接端口 默认是ssh的22端口

ansible_user 指定连接时使用对方主机的用户名,不指定主机执行ansible的用户即为使用目标主机的用户名

ansible_password 指定连接ssh的密码 (目标主机的用户密码)

ansible_become 用户提升权限

become root

ansible_become_password 指定切换用户的密码 (免密登录才行)

ansible模块_第2张图片

你可能感兴趣的:(linux,网络,服务器)