简介:临时的,在ansible中是指需要快速执行的单条命令,并且不需要保存的命令,对于复杂的命令则为playbook。
1.shell模块: ansible-doc shell 帮助中可以查找你需要的用的选项
例如:
ansible webserver -m shell -a 'hostname' -o 获取主机名
ansible host1 -m shell -a 'uptime' -o 查询系统负载
ansible host2 -m shell -a 'yum -y install httpd' -o 部署apache程序
2.copy模块: ansible-doc copy 帮助中可以查找你需要的选项
例:
ansible host1 -m copy -a 'src=/etc/hosts(文件来源) dest=/tmp/1.txt(目的地) owner=root group=bin mode=777' -o 再次拷贝后会覆盖之前文件
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777 backup=yes' (不会覆盖之前文件另保存一份)
3.user模块: ansible-doc user
例如:
创建用户:ansible host1 -m user -a 'name=nihao state(状态)=present(存在) ' -o
修改密码:ansible host1 -m user -a 'name=nihao password='你要改的免密' ' -o
创建完成后记得看一下是否安装完成
小知识点:echo ‘5121314’ | openssl passwd -1 -stdin 可以生成加密密码值
可以更改用户的信息shell:ansible host1 -m user -a 'name=nihao shell=/sbin/nologin append=yes' -o
删除用户:nsible webserver -m user -a 'name=qianfeng state=absent'
4.软件包管理:ansible-doc yum
例如:
升级所有包:ansible host1 -m yum -a 'name="*" state=latest'
安装apache:ansible host1 -m yum -a 'name=httpd state=latest' -o
5.服务模块; ansible-doc service
启动服务:ansible host1 -m service -a 'name=httpd state=started' -o
开机启动 : ansible host1 -m service -a 'name=httpd state=start enabled=yes' -o
停止: ansible host1 -m service -a 'name=httpd state=stopped' -o
重启: ansible host1 -m service -a 'name=httpd state=restarted' -o
开机禁止启动: ansible host1 -m service -a 'name=httpd state=started enabled=no' -o
6.文件模块:ansible-doc file
创建文件:ansible host1 -m file 'path=/tmp/77.txt mode=777 state=touch'
创建文件夹; ansible host1 -m file 'path=/tmp/99.txt mode=777 state=directory'
7.收集模块:ansible-doc setup
查询所有信息:ansible host1 -m setup
ansible host1 -m setup -a 'filter(guolv)=ansible_all_ipv4_addresses'
8.fetch:从远程某主机获取文件到本地
ansible host1 -m yum -a 'name="*" state=latest'
9.unarchive:解压缩
ansible host1 -m unarchive -a 'src=/root/1.tar dest=/tmp/'