zoukankan      html  css  js  c++  java
  • k8s记录-安装ansible

    ##1.安装
    1) python版本需要2.6以上,不过通过centos7都会默认安装上python2.7.5,查看方法:python -V
    2) 添加yum 源
    a.vim /etc/yum.repos.d/ansible
    [epel]
    name = all source for ansible
    baseurl = https://mirrors.aliyun.com/epel/7/x86_64/
    enabled = 1
    gpgcheck = 0
    [ansible]
    name = all source for ansible
    baseurl = http://mirrors.aliyun.com/centos/7.3.1611/os/x86_64/
    enabled = 1
    gpgcheck = 0
    b.安装
    yum clean all
    yum install ansible -y
    ##2.配置
    1)关闭第一次使用ansible连接客户端时输入命令提示:
    sed -i "s@#host_key_checking = False@host_key_checking = False@g" /etc/ansible/ansible.cfg
    2) 指定日志路径:
    sed -i "s@#log_path = /var/log/ansible.log@log_path = /var/log/ansible.log@g" /etc/ansible/ansible.cfg
    3) 加入主机名
    cat /etc/ansible/hosts
    [app]
    172.16.153.125
    10.107.117.33
    4) 配置无密登录
    ssh-keygen -t rsa 一路回车即可
    cat ~/.ssh/id_rsa.pub >> /home/app/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys
    合并id_rsa.pub内容即可
    或者执行
    ansible all -m authorized_key -a "user=root key='{{ lookup('file', '/root/.ssh/id_rsa.pub') }}' path=/root/.ssh/authorized_keys manage_dir=no" --ask-pass -c paramiko
    5)指定私密文件路径
    sed -i "s@#private_key_file = /path/to/file@private_key_file = /root/.ssh/id_rsa@g" /etc/ansible/ansible.cfg
    ##3.测试
    ansible all -m ping
    ansible app -m ping
    ansible app -m copy -a "src=/etc/hosts dest=/etc/"
    ansible app -m shell -a "yum install wget -y"
    ansible app -m command -a "df -h"
    ansible '*' -m command -a 'uptime'
    ansible app -a 'pwd'
    指定节点上的权限,属主和数组为root
    ansible '*' -m file -a "dest=/tmp/t.sh mode=755 owner=root group=root"
    #指定节点上定义一个计划任务,每隔3分钟到主控端更新一次时间
    ansible '*' -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 172.16.254.139"'
    指定节点上创建一个组名为aaa,gid为2017的组
    ansible all -m group -a 'gid=2017 name=a'
    在节点上创建一个用户aaa,组为aaa
    ansible all -m user -a 'name=aaa groups=aaa state=present'
    #删除用户示例
    ansible all -m user -a 'name=aaa groups=aaa remove=yes'
    在节点上安装httpd
    ansible all -m yum -a "state=present name=httpd"
    在节点上启动服务,并开机自启动
    ansible all -m service -a 'name=httpd state=started enabled=yes'
    执行主控端脚本
    ansible '*' -m script -a '/root/test.sh'
    执行远程主机的脚本
    ansible '*' -m shell -a 'ps aux|grep zabbix'
    类似shell
    ansible '*' -m raw -a "ps aux|grep zabbix|awk '{print $2}'"
    创建软链接
    ansible '*' -m file -a "src=/etc/resolv.conf dest=/tmp/resolv.conf state=link"
    删除软链接
    ansible '*' -m file -a "path=/tmp/resolv.conf state=absent"
    复制文件到远程服务器
    ansible '*' -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644"
    在节点上运行hostname
    ansible all -m raw -a 'hostname|tee'
    将指定url上的文件下载到/tmp下
    ansible all -m get_url -a 'url=http://10.1.1.116/favicon.ico dest=/tmp'
    ######################################################################################
    常用模块:
    1.ping
    ansible all -m ping
    !检测机器是否可登录,ping模块不需要传送参数
    !注:这里的ping模块并非调用了系统的ping命令,而是类似于登录到远程机器再echo出一个信息。
    2.command
    !可以执行任意命令,但不接受管道命令和重定向符号,如果想使用这些,则需要使用Shell模块。
    ansible all -m command -a "ls" #在所有匹配主机上执行ls命令
    playbook:
    - name: test for command
    command: ls
    3.copy
    #复制一个文件到远程服务器
    ansible all -m copy -a "src=/tmp/text.txt dest=/home/tmp/"
    #将本地text.txt文件复制到所有匹配主机的/home/tmp/路径下。
    playbook:
    - name: test for copy
    copy: src=/tmp/text.txt dest=/home/tmp/ force=yes
    4.file
    这个模块这次构建系统中并没有用到,官方的解释是用来设置文件、文件夹或快链的属性,或者删除文件、快链、文件夹。
    创建一个文件夹,如果目标不存在
    ansible webservers -m file -a "path=/tmp/tdir state=directory mode=0755"
    playbook
    - name: create a directory if it doesn't exist
    - file:
    path: /etc/some_directory
    state: directory
    mode: 0755
    5.get_url
    !从服务器上下载一个文件到远程主机指定的目录
    ansible webservers -m get_url -a "url='http://baidu.com dest=/tmp/ba.html"
    playbook
    - name: download foo.conf
    get_url:
    url: http://example.com/path/file.conf
    dest: /etc/foo.conf
    mode: 0440
    6.yum
    特别适用于批量安装一些依赖包的时候
    ansible webservers -m yum -a "name=httpd state=latest"
    ansible webservers -m yum -a "name=httpd state=absent" !删除包
    playbook
    - name: install the latest version of Apache
    yum:
    name: httpd
    state: latest
    7.service
    控制远程服务器上的服务
    ansible webservers -m service -a "name=nginx state=restart"
    playbook
    - name: restart rmote nginx
    service: name=nginx state=restart
    确定服务都是开启的
    #ansible all -m service -a "name=httpd state=started"
    重启服务
    #ansibel all -m service -a "name=httpd state=restarted"
    关闭服务
    #ansible all -m service -a "name=httpd state=stoped"
    8.setup
    收集远程服务器信息
    ansible all -m setup
    在playbook中,这项任务默认是执行的,不需要列出。
    ############################################################
    copy 推送文件模块
    src=
    源 推送数据的全路径
    dest=
    目标 推送数据的目标路径
    owner=
    指定推送文件的所有者信息
    group=
    指定推送文件的用户组信息
    mode=
    指定推送文件的权限信息
    backup=
    对传送过去的数据进行备份
    content=
    批量在服务端文件内添加内容 先清空再增加,与src二选一
    scripts 模块
    先把脚本传输到远端 然后执行
    ansible all -m script -a "/server/scripts/yum.sh"
    yum 安装模块
    name
    指定要安装的软件包名
    state
    要执行的yum动作
    installed & present 安装软件包
    remove & absent 卸载 关闭或者删除
    latest 更新软件包
    ansible all -m yum -a 'name=sl state=present'
    file 文件模块
    具有touch mkdir ln rm 的功能
    不支持通配符
    ansible all -m file -a 'path=/tmp/1/2/3/4 state=directory'
    ansible all -m file -a 'path=/tmp/1/2/3/oldboy.txt state=touch'
    serivce 服务模块
    name=服务名
    started #启动服务
    stopped #停止服务
    restarted #重启服务
    reloaded #平滑重启服务
    enabled
    yes 让服务开机自启
    no 默认disable
    ansible all -m service -a 'name=crond enabled=yes'
    group组模块
    name 指定创建的组名
    gid 指定组的gid
    state
    absent 移除远端主机的组
    present 创建远端主机的组(默认)
    ansible all -m group -a 'name=guoav1 gid=1113 state=present'
    user用户模块
    name 创建的用户名
    uid 指定创建用户的uid
    gid 指定创建用户的gid
    group 指定用户组名称
    groups 指定附加组名称
    password 给用户添加密码
    shell 指定用户登录shell
    create_home 是否创建家目录
    ansible all -m group -a 'name=guoav gid=1111 state=present'
    ansible all -m user -a 'name=guoav uid=1111 group=1111 shell=/sbin/nologin create_home=no'

  • 相关阅读:
    #pragma 预处理指令
    C++类继承中的构造函数和析构函数 调用顺序
    static_cast与dynamic_cast转换 最简单的理解
    std::map的insert和下标[]访问
    C++有没有string转化int的函数,怎样转换
    c++中在一个类中定义另一个只有带参数构造函数的类的对象
    c++二进制文件的读写
    C++ 包含头文件 和 宏的使用 和 条件编译
    C++ 前置声明 和 包含头文件 如何选择
    C语言 gets()和scanf()函数的区别
  • 原文地址:https://www.cnblogs.com/xinfang520/p/11882822.html
Copyright © 2011-2022 走看看