一、 ansibles基础与playbook

1.优先级问题

ANSIBLE_CONFIG
                        ansible.cfg 项目目录
                        .ansible.cfg 当前用户的家目录
                         /etc/ansible/ansible.cfg

2.配置文件说明

      [root@manager ~]# cat /etc/ansible/ansible.cfg
      #inventory = /etc/ansible/hosts #主机列表配置文件
      #library = /usr/share/my_modules/ #库文件存放目录
      #remote_tmp = ~/.ansible/tmp #临时py文件存放在远程主机目录
      #local_tmp = ~/.ansible/tmp #本机的临时执行目录
      #forks = 5 #默认并发数
      #sudo_user = root #默认sudo用户
      #ask_sudo_pass = True #每次执行是否询问sudo的ssh密码
      #ask_pass = True #每次执行是否询问ssh密码
      #remote_port = 22 #远程主机端口
      host_key_checking = False #跳过检查主机指纹
      log_path = /var/log/ansible.log #ansible日志

      ssh-keygen
首先产生公钥,私钥

      yum install
sshpass 安装此工具sshpass

      sshpass -p 'xxxx' ssh-copy-id -o
StrictHostKeyChecking=no root@172.17.0.112 只要在分发的这台服务器上安装即可,其他的机器不用安装

      然后将ip地址进行循环这样可以完成

      for host in $()

      do

      sshpass -p '密码'
ssh-copy-id -o StrictHostKeyChecking=no root@${host}

      done

  3.常用模块

    (1)  yum模块(安装present 卸载absent 升级latest  排除exclude 指定仓库enablerepo)

          #示例一、安装当前最新的Apache软件,如果存在则更新
            # ansible oldboy -m yum -a "name=httpd state=latest" -i hosts

          #示例二、安装当前最新的Apache软件,通过epel仓库安装
            # ansible oldboy -m yum -a "name=httpd state=latest enablerepo=epel" -i hosts

          #示例三、通过公网URL安装rpm软件
            # ansible oldboy -m yum -a "name=https://mirrors.aliyun.com/zabbix/zabbix/4.2/rhel/7/x86_64/zabbix-agent-4.2.3-2.el7.x86_64.rpm state=latest" -i hosts

          #示例五、更新所有的软件包,但排除和kernel相关的
            # ansible oldboy -m yum -a "name=* state=latest exclude=kernel*,foo*" -i hosts

          #示例六、删除Apache软件
            # ansible oldboy -m yum -a "name=httpd state=absent" -i hosts

    (2)copy模块

              #示例一、将本地的httpd.conf文件Listen端口修改为9999,然后推送到远端服务。

            # ansible oldboy -m copy -a "src=./httpd.conf dest=/etc/httpd/conf/httpd.conf owner=root group=root mode=644" -i hosts

          #示例二、将本地的httpd.conf文件Listen端口修改为9090,然后推送到远端,检查远端是否存在上一次的备份文件
            # ansible oldboy -m copy -a "src=./httpd.conf dest=/etc/httpd/conf/httpd.conf owner=root group=root mode=644 backup=yes" -i hosts

          #示例三、往远程的主机文件中写入内容
            # ansible oldboy -m copy -a "content=HttpServer... dest=/var/www/html/index.html" -i host

    (3)git-url和file模块

        -------get_url
        #示例一、下载互联网的软件至本地
                url ==> http https ftp
          # ansible oldboy -m get_url -a "url=http://fj.xuliangwei.com/public/ip.txt dest=/var/www/html/" -i hosts

        #示例二、下载互联网文件并进行md5校验(了解)
          # ansible oldboy -m get_url -a "url=http://fj.xuliangwei.com/public/ip.txt dest=/var/www/html/ checksum=md5:7b86f423757551574a7499f0aae" -i hosts

        -------file 创建目录 授权

        #示例一、创建文件,并设定属主、属组、权限。
          # ansible oldboy -m file -a "path=/var/www/html/tt.html state=touch owner=apache group=apache mode=644" -i hosts

        #示例二、创建目录,并设定属主、属组、权限。
          # ansible oldboy -m file -a "path=/var/www/html/dd state=directory owner=apache group=apache mode=755" -i hosts

        #示例三、递归授权目录的方式。
          # ansible oldboy -m file -a "path=/var/www/html/ owner=apache group=apache mode=755" -i hosts
          # ansible oldboy -m file -a "path=/var/www/html/ owner=apache group=apache recurse=yes" -i hosts

  (4)service启动模块

        #示例一、启动Httpd服务
          [root@ansible ~]# ansible oldboy -m service -a "name=httpd state=started"

        #示例二、重载Httpd服务
          [root@ansible ~]# ansible oldboy -m service -a "name=httpd state=reloaded"

        #示例三、重启Httpd服务
          [root@ansible ~]# ansible oldboy -m service -a "name=httpd state=restarted"

        #示例四、停止Httpd服务
          [root@ansible ~]# ansible oldboy -m service -a "name=httpd state=stopped"

        #示例五、启动Httpd服务,并加入开机自启
          [root@ansible ~]# ansible oldboy -m service -a "name=httpd state=started enabled=yes"

          模块网址:https://www.jianshu.com/p/54b8460f68ea

          ansible
localhost -m debug -a "msg={{ '123' | password_hash('sha512', 'salt')
}}"

          ansible webservers
-m user -a 'name=jsm
password="$6$salt$jkHSO0tOjmLW0S1NFlw5veSIDRAVsiQQMTrkOKy4xdCCLPNIsHhZkIRlzfzIvKyXeGdOfCBoW1wJZPLyQ9Qx/1"
create_home=yes'

  

  (5)user-group模块

      group
        #示例一、创建news基本组,指定uid为9999
          # ansible oldboy -m group -a "name=news gid=9999 state=present" -i hosts

        #示例二、创建http系统组,指定uid为8888
          # ansible oldboy -m group -a "name=http gid=8888 system=yes state=present" -i hosts

        #示例三、删除news基本组
          # ansible oldboy -m group -a "name=news state=absent" -i hosts

      -----------------
      user
        #示例一、创建joh用户,uid是1040,主要的组是adm
          # ansible oldboy -m user -a "name=joh uid=1040 group=adm" -i hosts

        #示例二、创建joh用户,登录shell是/sbin/nologin,追加bin、sys两个组
          # ansible oldboy -m user -a "name=joh shell=/sbin/nologin groups=bin,sys" -i hosts

        #示例三、创建jsm用户,为其添加123作为登录密码,并且创建家目录
          # ansible localhost -m debug -a "msg={{ '123' | password_hash('sha512', 'salt') }}"
            $6$salt$jkHSO0tOjmLW0S1NFlw5veSIDRAVsiQQMTrkOKy4xdCCLPNIsHhZkIRlzfzIvKyXeGdOfCBoW1wJZPLyQ9Qx/1

          # ansible oldboy -m user -a 'name=jsm password=$6$salt$jkHSO0tOjmLW0S1NFlw5veSIDRAVsiQQMTrkOKy4xdCCLPNIsHhZkIRlzfzIvKyXeGdOfCBoW1wJZPLyQ9Qx/1 create_home=yes'

        #示例四、移除joh用户
          # ansible oldboy -m user -a 'name=joh state=absent remove=yes' -i hosts

        #示例五、创建http用户,并为该用户创建2048字节的私钥,存放在~/http/.ssh/id_rsa
          # ansible oldboy -m user -a 'name=http generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa' -i hosts

  (6)cron模块 

      #示例一、添加定时任务。每分钟执行一次ls * * * * * ls >/dev/null
        # ansible oldboy -m cron -a "name=job1 job='ls >/dev/null'" -i hosts

      #示例二、添加定时任务, 每天的凌晨2点和凌晨5点执行一次ls。"0 5,2 * * ls >/dev/null
        # ansible oldboy -m cron -a "name=job2 minute=0 hour=5,2 job='ls >/dev/null'" -i hosts

      #示例三、关闭定时任务,使定时任务失效
        # ansible oldboy -m cron -a "name=job2 minute=0 hour=5,2 job='ls >/dev/null' disabled=yes" -i hosts

  (7)mount模块

    present 将挂载信息写入/etc/fstab         unmounted 卸载临时,不会清理/etc/fstab
    mounted 先挂载,在将挂载信息/etc/fstab         absent 卸载临时,也会清理/etc/fstab

        #环境准备:将172.16.1.61作为nfs服务端,172.16.1.7、172.16.1.8作为nfs客户端挂载
        # ansible localhost -m yum -a 'name=nfs-utils state=present'
        # ansible localhost -m file -a 'path=/ops state=directory'
        # ansible localhost -m copy -a 'content="/ops 172.16.1.0/24(rw,sync)" dest=/etc/exports'
        # ansible localhost -m service -a "name=nfs state=restarted"

      #示例一、挂载nfs存储至本地的/opt目录,并实现开机自动挂载
        # ansible oldboy -m mount -a "src=172.16.1.61:/ops path=/opt fstype=nfs opts=defaults state=mounted"

      #示例三、永久卸载nfs的挂载,会清理/etc/fstab
        # ansible webservers -m mount -a "src=172.16.1.61:/ops path=/opt fstype=nfs opts=defaults state=absent"

  selinux模块
    # ansible oldboy -m selinux -a "state=disabled" -i hosts

  firewalld模块

    # ansible oldboy -m service -a "name=firewalld state=started" -i hosts

    示例一 永久放行https的流量,只有重启才会生效
      # ansible oldboy -m firewalld -a "zone=public service=https permanent=yes state=enabled" -i hosts

    示例一 永久放行8081端口的流量,只有重启才会生效
      # ansible oldboy -m firewalld -a "zone=public port=8080/tcp permanent=yes state=enabled" -i hosts
    示例一 放行8080-8090的所有tcp端口流量,临时和永久都生效.
      # ansible oldboy -m firewalld -a "zone=public port=8080-8090/tcp permanent=yes immediate=yes state=enabled" -i hosts

  

最新文章

  1. ArcGIS Server SOE开发之奇怪异常:
  2. Usaco*Brownie Slicing
  3. 公司内部的一篇关于dom方法的分享
  4. 40个让你的网站屌到爆的jQuery插件
  5. uploadify使用教程
  6. IIS处理并发请求时出现的问题及解决
  7. Node对象属性
  8. .NET破解之100%营销QQ辅助软件【更新】
  9. Git_Commands
  10. InheritableThreadLocal原理
  11. HDU-2888 Check Corners 二维RMQ
  12. spring利用javamail,quartz定时发送邮件 <转>
  13. OpenJudge/Poj 1753 Flip Game
  14. PureLayout(轻量级自动布局)
  15. MVVM探索:从ViewModel关闭Window的最佳实践
  16. Vue中,父组件向子组件传值
  17. 一篇文章有若干行,以空行作为输入结束的条件。统计一篇文章中单词the(不管大小写,单词the是由空格隔开的)的个数。
  18. php里获取第一个中文首字母并排序
  19. POJ 1151Atlantis 矩形面积并[线段树 离散化 扫描线]
  20. HDU1211 RSA

热门文章

  1. 利用canvas+js完成滑块验证码中canvas部分思路
  2. P3804 【模板】后缀自动机 (SAM) && P6139 【模板】广义后缀自动机(广义 SAM)
  3. Python接口自动化之pymysql数据库操作
  4. Android ViewModel,LiveData 简要分析
  5. Bouncy Castle密码算法库
  6. 银河麒麟V10在线安装Postgresql步骤
  7. 一台电脑连接多个不同IP段
  8. No.2.3
  9. 【redis】配置优化及从库优先级
  10. OSPF邻居状态