返回顶部
扶摇直上九万里,展翅高飞岂可待。

1. ansible学习总结: 基础模块

copy模块
#传输文件到目标机
ansible -i /kingdee/ansible/host all -m copy -a 'src=/tmp/aaaa.tgz dest=/tmp/aaaa.tgz'

cron模块:
#创建任务
ansible -i /kingdee/ansible/host all -m cron -a 'hour=2 minute=20 weekday=1-5 name="backup_mysql" job=/root/mysql_backup'
ansible -i /kingdee/ansible/hosts all -m cron -a "hour=2 minute=*/5 weekday=1-5 name="Sycntime" job='/usr/bin/ntpdate 88.888.88.88 &>/dev/null' "

#禁用计划
ansible -i /kingdee/ansible/hosts all -m cron -a " job='/usr/bin/ntpdate 88.888.88.88 &>/dev/null' disabled=yes"

#启用计划任务
ansible -i /kingdee/ansible/hosts all -m cron -a " job='/usr/bin/ntpdate 88.888.88.88 &>/dev/null' disabled=no"

#删除计划任务
ansible -i /kingdee/ansible/hosts all -m cron -a " job='/usr/bin/ntpdate 88.888.88.88 &>/dev/null' name="Sycntime" state=absent"


yum模块,只支持centos:
#安装
ansible -i /kingdee/ansible/hosts all -m yum -a "name=net-tools state=latest" -become
ansible -i /kingdee/ansible/hosts all -m yum -a "name=httpd" -become
#卸载
ansible -i /kingdee/ansible/hosts all -m yum -a "name=net-tools state=absent" -become
ansible -i /kingdee/ansible/hosts all -m yum -a "name=httpd state=absent" -become
#指定下载源,安装多个组件,包括版本
ansible -i /kingdee/ansible/hosts all -m yum -a "name=net-tools,nginx,mysql,git-1.8.3.1-23.el7_8.x86_64 state=latest enablerepo=local" -become


service模块:
#启动httpd
ansible -i /kingdee/ansible/hosts all -m service -a "name=httpd state=started" -become
#停止httpd
ansible -i /kingdee/ansible/hosts all -m service -a "name=httpd state=stopped" -become
#开机启动httpd
ansible -i /kingdee/ansible/hosts all -m service -a "name=httpd enabled=yes" -become
#开机启动取消httpd
ansible -i /kingdee/ansible/hosts all -m service -a "name=httpd enabled=no" -become
#重新加载配置文件
ansible -i /kingdee/ansible/hosts all -m service -a "name=httpd daemon_reload=yes " -become

group模块:
#创建组:
ansible -i /kingdee/ansible/hosts all -m group -a "name=httpd gid=1001 system=yes" -become
#删除组
ansible -i /kingdee/ansible/hosts all -m group -a "name=httpd gid=1001 state=absent" -become


user模块:
#创建用户create_home是否创建home目录,home路径
ansible -i /kingdee/ansible/hosts all -m user -a "name=testadmin comment='test_uer' uid=1001 groups=1001 shell=/sbin/nologin system=yes create_home=yes home=/home/test_user" -become

#删除用户,remove=yes删除用户home目录
ansible -i /kingdee/ansible/hosts all -m user -a "name=testadmin state=absent remove=yes" -become

 

lineinfile模块:操作都是行,注意不是特定字段
#创建testfile.txt文件行
ansible -i /kingdee/ansible/hosts all -m shell -a "echo 'aaaaaa123asdf' > /kingdee/testfile.txt" -become
#替换testfile.txt中的内容aaaaaa
ansible -i /kingdee/ansible/hosts all -m lineinfile -a "path=/kingdee/testfile.txt regexp='aaaaaaa' line='bbbbbb'" -become
#删除已#号开头的行
ansible -i /kingdee/ansible/hosts all -m lineinfile -a "path=/kingdee/testfile.txt regexp='^#' state=absent'" -become


replace模块:替换文件夹中的内容
ansible -i /kingdee/ansible/hosts all -m replace -a "path=/kingdee/testfile.txt regexp='bbbbbb' replace='asdf123123134523452'" -become

 

setup模块:手机远程主机信息

ansible_all_ipv4_addresses:仅显示ipv4的信息。
ansible_devices:仅显示磁盘设备信息。
ansible_distribution:显示是什么系统,例:centos,suse等。
ansible_distribution_major_version:显示是系统主版本。
ansible_distribution_version:仅显示系统版本。
ansible_machine:显示系统类型,例:32位,还是64位。
ansible_eth0:仅显示eth0的信息。
ansible_hostname:仅显示主机名。
ansible_kernel:仅显示内核版本。
ansible_lvm:显示lvm相关信息。
ansible_memtotal_mb:显示系统总内存。
ansible_memfree_mb:显示可用系统内存。
ansible_memory_mb:详细显示内存情况。
ansible_swaptotal_mb:显示总的swap内存。
ansible_swapfree_mb:显示swap内存的可用内存。
ansible_mounts:显示系统磁盘挂载情况。
ansible_processor:显示cpu个数(具体显示每个cpu的型号)。
ansible_processor_vcpus:显示cpu个数(只显示总的个数)。


#查找IP地址,使用filter进行过滤,可以采用通配符查找
ansible -i /kingdee/ansible/hosts all -m setup -a "filter=*ipv4_addresses*"
#查找版本
ansible -i /kingdee/ansible/hosts all -m setup -a "filter=ansible_distribution_major_version"
#为减少每次执行ansile都需要收集setup,效率比较低,可以增加gather_facts:no来禁止手机facts

 

posted @ 2022-12-17 18:34  鲤鱼洲畔  阅读(39)  评论(0编辑  收藏  举报