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. 手机web站点和手机app 技术选型的困惑于思考

    今年一直在关注移动端技术的发展,自己也用博客园的rss接口玩了半年,关于技术选型的困惑和大家说说 一 趋势 随着手机硬件不断的升级,外加4g牌照的发放,不出2年时间移动端web站点和手机app一定会进 ...

  2. 【COGS】894. 追查坏牛奶

    http://cojs.tk/cogs/problem/problem.php?pid=894 题意:n个点m条边的加权网络,求最少边数的按编号字典序最小的最小割.(n<=32, m<=1 ...

  3. 纪念逝去的岁月——C/C++二分查找

    代码 #include <stdio.h> int binarySearch(int iList[], int iNum, int iX, int * pPos) { if(NULL == ...

  4. org.openqa.selenium.StaleElementReferenceException

    org.openqa.selenium.StaleElementReferenceException如何解啊.什么原因造成的,貌似有时会出现,有时不会出现

  5. Oracle中"行转列"的实现方式

    在报表的开发当中,难免会遇到行转列的问题. 以Oracle中scott的emp为例,统计各职位的人员在各部门的人数分布情况,就可以用"行转列": scott的emp的原始数据为: ...

  6. 李洪强iOS经典面试题126

    1.#import和#include的区别,@class代表什么? @class一般用于头文件中需要声明该类的某个实例变量的时候用到,在m文件中还是需要使用#import 而#import比起#inc ...

  7. 关于Android2.X系统自定义图片圆角BUG的解决

    今天在做项目的时候遇到的一个问题. 预期的效果是这样的:

  8. Git初使用

    今天开始初次使用Git,Git作为一个使用广泛的分布式版本控制系统,我们有必要熟悉掌握. 这次主要是实现将本地上的“Hello World”的完整的项目文件提交到github上新建的代码库,主要过程如 ...

  9. SQL中的with as

    一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是为了让 ...

  10. for循环练习

    1.输入一个整数,计算从1加到这个数的结果int sum = 0;Console.WriteLine("请输入一个正整数");int a = int.Parse(Console.R ...