zoukankan      html  css  js  c++  java
  • Ansible常用模块

    Ansible模块

    # 环境
    # ansible: 172.16.1.61
    cat /etc/ansible/hosts
    [web]
    web1:	172.16.1.7
    web2:  172.16.1.8
    web3:  172.16.1.9
    

    1. ssh免密钥配置

    # 免交互创建密钥
    ssh-keygen -t dsa -f /root/.ssh/id_dsa -N ""
    
    # 免交互将密钥推送至远端
    sshpass -p'1289456'  ssh-copy-id -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.108
    
    # scp免交互拷贝远程拷贝
    sshpass -p "1289456" scp root@172.16.1.61:/root/1.txt /tmp
    
    # 通过ansible使用scp免交互远程拷贝
    ansible web -m shell -a " sshpass -p "1289456" scp root@172.16.1.61:/root/1.txt /tmp "
    

    2. Ad-Hoc

    # ad-hoc 表示临时命令,不会保存
    # ansible中有两种模式,分别是ad-hoc模式和playbook模式
    
    #使用场景
    # 在多台机器上,查看某个进程是否启动
    # 在多台机器上,拷贝指定日志文件到本地
    

    3. Ad-Hoc命令格式

    命令 + 主机组名称 + 指定模块 + 模块动作 + 具体命令
    

    4. command命令模块( shell )

    # command 为默认模块,可以不指定;
    ansible web -a "hostname"
    
    # 特殊操作可以使用shell模块,同样是命令模块
    ansible web -m shell -a "ifconfig |grep eth0 " -f 50
    # -f = forks /etc/ansible/ansible.cfg   #结果返回的数量
    

    5. script脚本模块

    ansible web -m script -a "/scripts/check_flog.sh"
    

    6. yum 安装软件模块

    # name			 指定要安装的软件包名称;
    # state		     指定使用yum的方法; (installed,persent #安装) (removed,absent #移除软件包)  (latest #安装最新)
    # exclude		 排除
    # enablerepo	 指定yum仓库
    # disablerepo 	 安装时不使用哪个仓库
    ansible web -m yum -a "name=nginx state=installed"
    
    ansible web -m yum -a "name=nginx state=latest"
    
    ansible web -m yum -a "name=nginx enablerepo=epel state=present"
    
    ansible web -m yum -a "name=http://192.168.16.236/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.0-2.el7.x86_64.rpm state=present "
    
    ansible web -m yum -a "name='*' state=latest exclude="kernel*""
    
    ansible web -m yum -a "name=nginx state=abent"
    

    7. copy 拷贝模块

    `# src 			源文件
    `# dest			目标路径
    `# backup 		对推送过去的文件,进行备份
    `# content		直接批量的在被管理端的文件中添加内容
    `# group		指定属组
    `# owner		指定属主
    `# mode			指定权限
    ansible web -m copy -a 'src=/etc/yum.repos.d/nginx.repo dest=/tmp/owner=root group=root mode=600 backup=yes'
    
    ansible web -m copy -a "src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf owner=root group=root mode=0644 backup=yes"
    
    # 直接向远端文件内写入数据信息,并且会覆盖远端文件内原有数据信息
    ansible web -m copy -a "content='test' dest=/tmp/dtest.txt"
    

    8.systemd 启动服务模块

    `# name 	 	服务名称
    `# state   		操作	( started  stopped  restarted  reloaded )
    `# enabled  	是否开机自启动 yes|no
    ansible web -m systemd -a "name=nginx state=started"
    
    ansible web -m systemd -a "name=nginx state=started enabled=yes"
    

    9.file 文件配置模块

    `# path   	指定远程主机目录或文件信息
    `# recures	递归授权
    `# state	类型或动作 -->
    			`# --> directory	创建目录	
    			`# --> touch			创建文件  
    			`# --> link				创建链接文件 
    			`# --> absent			删除文件或目录 
    			`# --> mode				指定权限  
    			`# --> owner			指定属主
    			`# --> group			指定属组
    ansible web -m file -a "path=/tmp/test state=directory"
    
    ansible web -m file -a "path=/tmp/test state=touch mode=555 owner=root group=root"
    
    ansible web -m file -a "src=/tmp/test path=/tmp/test_link state=link"
    
    

    10. group模块

    `# name 		指定创建的组名
    `# gid			指定组的gid
    `# state		状态 -->
    				`# --> absent  移除远端主机的组
    				`# --> present 创建远端主机的组
    ansible web -m group -a " name=www gid=666 state=present"
    
    ansible web -m group -a " name=www state=absent"
    

    11. user模块

    `# name					指定创建的用户名
    `# uid 					指定用户uid
    `# group 				指定用户组名称
    `# groups				指定附加组名称
    `# password				给用户添加密码
    `# shell				指定用户登录shell
    `# createhome			是否创建家目录
    `# system				指定系统用户
    ansible web -m user -a "name=www uid=666 shell=/sbin/nologin create_home=no"
    
    `# 将明文密码进行hash加密,然后进行用户创建
    ansilbe all -i localhost, -m debug -a "msg={{'password' | password_hash('sha512', 'mysecretsalt' ) }}"  ;
     "msg": "$6$mysecretsalt$ZB9R8AirQYAXhtfhOo2qdJz52FyNI6v3L6Uc3KNRP.arBKIYpcuEyQewT5qBAHoyQFwHkW6Z551Ql.cZ53GeY0"
    ansible web -m user -a 'name=mysql password="$6$mysecretsalt$ZB9R8AirQYAXhtfhOo2qdJz52FyNI6v3L6Uc3KNRP.arBKIYpcuEyQewT5qBAHoyQFwHkW6Z551Ql.cZ53GeY0"'
    

    12. mount 挂载

    `# src 			源设备( 磁盘|光盘|远程共享地址 )
    `# path			挂载点
    `# fstype		设备类型  --> nfs  xfs  ext4   iso9660
    `# opts			挂载选项	defaults
    `# stste		状态 -->
    				`# --> absent  | mounted   永久挂载
                  	`# --> present | unmounted 临时挂载
    
    ansible web -m mount -a "src=172.16.1.31:/data/blog path=/opt fstype=nfs opts=defaults state=mounted"
    
    ansible web -m mount -a "src=172.16.1.31:/data/blog path=/opt fstype=nfs opts=defaults state=unmounted"
    

    13. selinux

    `# 关闭selinux
    ansible web -m selinux -a "state=disabled"
    

    14. firewalld

    `# zone				指定区域 默认public
    `# service			指定服务名称
    `# port				指定端口
    `# state			启用或禁止
    `# masquerade		开机地址伪装是否开启 yes|no
    `# immediate		临时生效 yes | no
    `# permanent  		永久生效
    `# source			来源IP
    `# rich_rule:	rule service name="ftp" audit limit value="1/m" accept
    
    ansible web -m firewalld -a "zone=public  service=http state=enabled immediate=yes permanent=yes"
    
    ansible web -m firewalld -a "zone=public  port=80/tcp state=enabled immediate=yes permanent=yes"
    
    `# 将5555端口转发到 172.16.1.7 22  端口 开启masquerade地址伪装`
    ansible web -m firewalld -a "zone=public rich_rule='rule family=ipv4 forward-port port=5555 protocol=tcp to-port=22 to-addr=172.16.1.7'  state=enabled immediate=yes"
    
    ansible web -m firewalld -a "zone=public masquerade=yes state=enabled immediate=yes"
    
    `# 配置基于来源IP  10.0.0.1主机 放行 22 端口`
    ansible web -m firewalld -a "zone=public rich_rule='rule family=ipv4 source address=10.0.0.100/32 service name=ssh accept'  state=enabled immediate=yes"
    

    15. cron 定时任务模块

    
    
    `# 添加一个定时任务
    ansible web -m cron -a "name='Backup scripts' minute=00 hour=05 user=root job='/bin/bash /scripts/check_data.sh &>/dev/null'"
    
    `# 删除定时任务
    ansible web -m cron -a "name='Backup scripts' minute=00 hour=05 user=root job='/bin/bash /scripts/check_data.sh &>/dev/null' state=absent"
    

    16.yum_repository 源仓库模块

    `# name				仓库名称,文件的名称
    `# description		描述
    `# baseurl			仓库地址
    `# enabled			是否启用该仓库yes
    `# gpgcheck			不对下载的包检查
    
    ansible web -m yum_repository -a 'name=rpmforge description="RPMforge YUM Repo" baseurl="https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/" enabled=yes gpgcheck=no'
    

    17.replace 替换模块

    # path        指定替换文件路径
    # regexp      指定替换对象
    # repaace     指定替换内容
    
    - name: Modify ssh configure
      replace:
        path: /etc/ssh/sshd_config
        regexp: '^#UseDNS yes'
        replace: 'UseDNS no'
    
  • 相关阅读:
    DOM优化
    jQuery绑定以及解除时间方法总结,以及事件触发的方法
    javascript的异步编程方法
    innerHTML,innertext ,textcontent,write()
    关于盒子的那些事
    关于HTTP的几种
    关于php中正则匹配包括换行符在内的任意字符的问题总结
    CSS透明属性详解
    Linux下apache日志分析与状态查看方法
    折半排序 (稳定的排序)
  • 原文地址:https://www.cnblogs.com/IMSCZ/p/12057969.html
Copyright © 2011-2022 走看看