Ad-Hoc
常用模块
远程复制备份模块copy
模块参数详情src=:指定源文件路径
dest=:目标地址(拷贝到哪儿)
owner:指定属主
group:指定数组
mode:指定权限
backup:在覆盖之前将源文件备份
- 将控制端的文件拷贝到被控制端
ansible jiajia[0] -m copy -a "src=/root/a.txt dest=/opt owner=root group=root mode=644" -o
用户管理模块user
- 添加用户并设置密码:使用命令需要用双引:
"name= " #如:指定的用户名,要安装的软件
passwd -1 # 加密算法 (MD5加密算法)
ansible -i /etc/ansible/hosts fuye2 -m user -a "name=mianshi password=`echo 1234 | openssl passwd -1 -stdin`" -o
- 删除用户
ansible -i /etc/ansible/hosts fuye2 -m user -a "name=mianshi state=absent"
软件包管理模块 yum
ansible fuye2 -m yum -a "name=httpd state=latest" -o
state= #状态是什么,干什么 state=absent 用于remove安装包
state=latest 表示最新的
state=removed 表示卸载
- 卸载软件
ansible fuye2 -m yum -a "name=httpd state=removed"
服务管理模块 service
- 启动
ansible fuye2 -m service -a "name=httpd state=started"
- 停止
ansible fuye2 -m service -a "name=httpd state=stopped"
- 重启
ansible fuye2 -m service -a "name=httpd state=restarted"
- 开机启动
ansible fuye2 -m service -a "name=httpd state=started enabled=yes"
- 开机关闭
ansible fuye2 -m service -a "name=httpd state=started enabled=no"
state=started 启动
state=restarted 重启
state=stopped 停止
state=start enabled=yes 开机自启
state=start enabled=no 开机关闭
文件模块file
模块参数详解:owner:修改属主
group:修改属组
mode:修改权限
path=:要修改文件的路径
recurse:递归的设置文件的属性,只对目录有效
yes:表示使用递归设置
state:
touch:创建一个新的空文件
directory:创建一个新的目录,当目录存在时不会进行修改
- 创建一个新的目录
ansible fuye2 -m file -a "path=/tmp/99.txt mode=777 state=directory"
- 创建一个新的文件
ansible fuye2 -m file -a "path=/tmp/88.txt mode=777 state=touch"
信息收集模块 setup
- 收集所有信息
ansible fuye3 -m setup
filter:过滤
- 只查询ipv4的地址
ansible fuye3 -m setup -a "filter=ansible_all_ipv4_addresses"
- 将查询文件重定向
ansible fuye3 -m setup -a "filter=ansible_processor_cores" --tree /opt/facts