💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
# :-: ansible 命令使用示例 [TOC] ##### shell 模块 1. 每次创建10个子进程去执行主机任务(默认值是5) ``` ansible all -m shell -a "reboot" -f 10 ``` 2. 指定远程执行命令的用户 ``` ansible all -m shell -a "reboot" -u 用户名 ``` 3. 使用 sudo 去执行命令 ``` ansible all -m shell -a "reboot" -u 用户名 --sudo [--ask-sudo-pass] ``` 4. 使用 sudo 切换到其他用户执行命令 ``` ansible all -m shell -a "reboot" -u 用户名 -U 其他用户名 ``` 5. 使用变量 ansible 中的 command 模块和 shell 模块用着差不多,但 command 模块不支持只用变量或者管道符之类的语法 ``` ansible all -m shell -a ’echo $VAR‘ ``` > 使用环境的时候,特别需要注意双引号,如果使用的是双引号,那么就会被替换成当前系统的变量值,但是我们实际希望的是把这个环境变量传到所对应的主机中,所以应当使用单引号。 ##### copy 模块 ``` ansible all -m copy -a "src=文件源地址 dest=目标主机的目标地址" ``` ##### file 模块 1. 修改文件属性(这里copy模块也完成) ``` ansible all -m file -a "dest=目标文件 mode=755 owner=root group=root" ``` 2. 创建目录 ``` ansible all -m file -a "dest=目标地址 mode=755 owner=root group=root state=directory" ``` 3. 删除目录和文件 ``` ansible all -m file -a "dest=目标地址 state=absent" ``` ##### apt、yum 模块 > apt 和 yum 都是支持的,下面用 apt 进行示例 1. 安装一个软件,使用参数`present`或者`installed` ``` ansible all -m apt -a "name=ssh state=present" ansible all -m apt -a "name=ssh-1.2 state=installed" //安装指定版本 ``` 2. 卸载一个软件,使用参数`removed`或者`absent` ``` ansible all -m apt -a "name=ssh state=absent" ``` 3. 更新一个软件,使用参数`latest` ``` ansible all -m apt -a "name=ssh state=latest" ``` ##### user 模块 1. 创建用户,并设置密码 ``` ansible all -m user -a "name=用户名 password=密码" ``` 2. 删除用户 ``` ansible all -m user -a "name=用户名 state=absent" ``` ##### service 模块 1. 启动一个服务 ``` ansible all -m service -a "name=ssh state=started" ``` 2. 重启一个服务 ``` ansible all -m service -a "name=ssh state=restarted" ``` 3. 停止一个服务 ``` ansible all -m service -a "name=ssh state=stoped" ```