##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'

k8s记录-安装ansible的更多相关文章

  1. CentOS 7离线安装Ansible

    前言 我一直都想成为自动化运维界最亮的仔,奈何自己实力不允许.不过,我一直都在奋斗的路上:这不,最近就在学习自动化运维界的神器--Ansible. 要系统的学习一下Ansible,那就是要先搭建学习环 ...

  2. Kubernetes(k8s)完整安装教程

    Kubernetes(k8s)完整安装教程  2019-08-27 2.3k 记录 发表评论 目录 1 安装 1.1 安装 Docker 1.2 安装 VirtualBox 1.3 安装 kubect ...

  3. k8s Docker 安装

    k8s Docker 安装 一.运行环境 Centos 7.7 虚拟机内核为 3.10 基础组件版本: k8s.gcr.io/kube-apiserver:v1.16.0 k8s.gcr.io/kub ...

  4. win10的pycharm中安装ansible模块过程

    前面的安装报错信息 ansible模块安装报错:Could not install packages due to an OSError: [Errno 2] No such file or dire ...

  5. 一键源码安装Ansible

    #!/bin/bash # @Name:install_ansible.sh # @Author:Eivllom # @Create -- # @Modify -- app_soft="/a ...

  6. 源码安装Ansible

    一.Ansible介绍 ansible是一款的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置.批 ...

  7. CeontOS7安装ansible

    安装方法一. 第一步:安装epel rpm -ivh http://mirror.pnl.gov/epel/7/x86_64/e/epel-release-7-5.noarch.rpm 第二步:安装a ...

  8. NSIS:在注册表中记录安装路径以便重装或升级时读取

    原文 NSIS:在注册表中记录安装路径以便重装或升级时读取 在NSIS中,这个功能是非常有用的,可以避免用户把程序安装到多个位置的尴尬. 第1步:在“安装目录选择页面”前面加入以下代码: 1 !def ...

  9. centos6.5 安装ansible,管理多台服务器

    安装python(最低2.6v) (1).python2.7安装 wget https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz # tar ...

随机推荐

  1. redis windows下载地址

    https://github.com/MicrosoftArchive/redis/tags

  2. Linux不管上一条命令成功还是失败都执行下一个命令的方法

    转载请注明来源https://www.cnblogs.com/sogeisetsu/p/11407830.html Linux不管上一条命令成功还是失败都执行下一个命令的方法 Linux不管上一条命令 ...

  3. php解释器模式( interpreter pattern)

    ... <?php /* The interpreter pattern specifies how to evaluate language grammar or expressions. W ...

  4. python正则表达式(6)--split、sub、escape方法

    1.re.split 语法: re.split(pattern, string[, maxsplit=0, flags=0]) 参数: pattern    匹配的正则表达式 string      ...

  5. table的各种用法

    使用 colgroup 和 col 实现响应式表格(table的各种用法):http://coderlt.coding.me/2017/11/20/table-colgroup/

  6. linux 查看硬盘使用情况

    在windows系统中,我们可以很容易的查看磁盘的使用情况,在linux系统中,我们可以使用命令来查看磁盘使用情况. 1.df命令 作用:用来查看硬盘的挂载点,以及对应的硬盘容量信息.包括硬盘的总大小 ...

  7. ImageMagick 的安装及使用

    一.什么是Imagemagick? ImageMagick是一款免费开源的图片编辑软件.既可以通过命令行使用,也可以通过C/C++.Perl.Java.PHP.Python或Ruby调用库编程来完成. ...

  8. 18-Flutter移动电商实战-首页_火爆专区商品接口制作

    1.获取接口的方法 在service/service_method.dart里制作方法.我们先不接收参数,先把接口调通. Future getHomePageBeloConten() async{   ...

  9. nave node 的虚拟环境管理工具

    nave 是类似python venv 的node 虚拟环境管理工具 安装 npm install -g nave 简单使用 帮助命令 Usage: nave <cmd> Commands ...

  10. Android Studio中每次打开工程Gradle sync龟速解决办法

    问题描述 自己使用android studio后,发现每次一打开工程,软件就在Grandle sync.sync就算了,而且这个步骤还必须过TZ,并且时间超级长,可能睡完觉起来还没有下载好.下面是正在 ...