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

    配置文件:/etc/ansible/ansible.cfg

    主机列表:/etc/ansible/hosts 

    安装anslibe 

    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
    yum -y install ansible

    配置文件先备份

    修改配置文件hosts添加主机组 (分组)

    如果要远程连接需要配置用户名和密码或密钥(两种方式都可以)

    用户密码:

    [webtest]
    192.168.32.132 ansible_ssh_user=root ansible_ssh_pass=登录密码
    192.168.32.131 ansible_ssh_user=root ansible_ssh_pass=登录密码

    密钥:

    生成私钥和公钥 ssh-keygen -t rsa -P ''

    注意文件权限:

    [root@localhost .ssh]# cp id_rsa.pub authorized_keys
    [root@localhost .ssh]# ll
    total 16
    -rw-r--r-- 1 root root  408 Mar 14 22:32 authorized_keys
    -rw------- 1 root root 1679 Mar 14 22:32 id_rsa
    -rw-r--r-- 1 root root  408 Mar 14 22:32 id_rsa.pub
    -rw-r--r-- 1 root root  352 Mar 14 22:20 known_hosts
    [root@localhost .ssh]# chmod 600 authorized_keys 
    [root@localhost .ssh]# ll authorized_keys 
    -rw------- 1 root root 408 Mar 14 22:32 authorized_keys

    公钥分别发送到被管理的主机:

    scp authorized_keys 192.168.32.132:/root/.ssh/
    scp authorized_keys 192.168.32.131:/root/.ssh/

     测试OK:

    常用模块:

    注意:command和shell模块的核心参数直接为命令本身;而其它模块的参数通常为“key=value”格式

    -m  command   (如下没写-m command 是因为默认模式是 command)

    ansible测试:ping模块测试连通性

    第一个要做的就是时间同步:

     首先使用ansible的yum模块批量安装ntpdate服务

    ansible all -m yum -a "state=present name=ntpdate"

    批量删除两种方式:

    ansible all -m yum -a "state=removed name=ntpdate"
    ansible all -m yum -a "state=absent name=ntpdate"

    指定节点安装及删除:

    安装:ansible 192.168.32.131 -m yum -a "state=present name=ntpdate"

    删除:ansible 192.168.32.131 -m yum -a "state=remove name=ntpdate"

    时间同步:

    ansible all -a 'ntpdate ntp1.aliyun.com'

    获取模块列表:ansible-doc -l

    获取指定模块的使用帮助:ansible-doc -s MOD_NAME

     创建用户:

    copy模块:复制文件到远程主机

    用法:

        (1) 复制文件

            -a "src=原地址 dest=目标地址   mode=644 权限 "

        (2) 给定内容生成文件

            -a "content=  dest=  "

     复制文件:

    src原地址   dest目标地址  mode权限

    注:如果文件存在将覆盖原文件,并没有提示操作需小心。

    ansible all -m copy -a "src=/etc/ansible/hosts.bak dest=/opt/ mode=600"

    file模块:

    用法:

        (1) 创建目录:

            -a "path=  state=directory"

        (2) 创建链接文件:

            -a "path=  src=\'#\'" /p>

        (3) 删除文件:

            -a "path=  state=absent“

    修改文件用户组属性:

    ansible all -m file -a "path=/tmp/hosts.bak mode=644 owner=root group=root"

    创建目录及修改属性:

    创建目录
    ansible webtest -m file -a "path=/opt/work state=directory"
    修改组属性
    ansible webtest -m file -a "path=/opt/work mode=755 owner=root group=root"

    创建软链:

    ansible all -m file -a "src=/opt/hosts.bak path=/tmp/hosts.link state=link"

     删除软链:

    ansible all -m file -a "path=/tmp/hosts.link state=absent"

    fetch模块:从远程主机取文件

    批量取:
    ansible all -m fetch -a "src=/opt/hosts.bak dest=/root"
    指定主机取:
    ansible 192.168.32.131 -m fetch -a "src=/opt/hosts.bak dest=/root"

    cron模块:管理计划任务条目

    用法:    

        -a ""

            minute=

            hour=

            day=

            month=

            weekday=

            job=

            name=

            user=

            state={present|absent}

    创建一个同步时间的计划任务,每5分钟同步一下服务器的时间

    ansible all -m cron -a "minute='*/5' job='/usr/sbin/ntpdate ntp1.aliyun.com &> /dev/null' name='时间同步'"

    删除计划任务:

    ansible all -m cron -a "name='时间同步' state=absent"

    hostname模块:管理主机名

    用法:

        name=

    ansible 192.168.32.131 -m hostname -a "name=CentOS_7"

    yum模块:使用yum命令完成程序包管理

    用法:首先,确定主机的yum源是可用的 yum info samba

        -a ""

            (1) name=  state={present|latest}

            (2) name=  state=absent

    批量安装samba
    ansible all -m yum -a "name=samba" 或 ansible all -m yum -a "name=samba state=present  |   latest"
    查看安装状态
    ansible all -a "yum info samba"
    删除samba
    ansible all -m yum -a "name=samba state=absent"

    service模块:服务管理

    用法:  

    -a ""

        name=

        state=

            started

            stopped

            restarted

        enabled=

        runlevel=

    安装httpd

    ansible all -m yum -a "name=httpd"

    启动httpd

    ansible all -m service -a "name=httpd state=started enabled=true"

    关闭httpd服务

    ansible all -m service -a "name=httpd state=stopped enabled=false"

    group模块:增加或删除组

    用法:

        -a ""

            name=

            state=

            system=

            gid=

    创建组
    ansible all -m group -a "name=ggg system=true"
    删除组
    ansible all -m group -a "name=ggg state=absent"

    user模块:用户管理

    使用格式:

        name= : 创建的用户名

        state= : present新增,absent删除

        force= : 删除用户的时候删除家目录

        system= : 创建系统用户

        uid= : 指定UID

        shell= : 指定shell

        home= : 指定用户家目录

    创建用户
    ansible all -m user -a "name=ggg system=true"
    查看
    ansible all -a "id ggg"
    删除用户
    ansible all -m user -a "name=ggg state=absent"

    setup模块:收集主机里面的各种信息

    信息太多,指定一台收集
    ansible 192.168.32.131 -m setup
  • 相关阅读:
    [LeetCode] 771. Jewels and Stones
    [LeetCode] 129. Sum Root to Leaf Numbers
    java定时器demo
    Spring Boot与监控管理
    springboot与热部署
    springboot中的web项目不能访问templates中的静态资源
    @Component 和 @Bean 的区别
    springcluoud入门
    Dubbo和Zookerper的关系
    Spring boot配置Dubbo三种方式
  • 原文地址:https://www.cnblogs.com/zhink/p/8572175.html
Copyright © 2011-2022 走看看