Ansible使用playbook自动化编译安装Nginx

Ansible批量部署编译安装nginx

一、Ansible介绍

这次为大家介绍一款批量部署工具Ansible,主要有以下几点优点:1、充分利用现有设施。使用 Ansible 无需安装服务端和客户端,只要 SSH 即可。这意味着,任何一台装有 Ansible 的机器都可以成为强大的管理端。我觉得,这种去中心化的思路显得更为灵活。可能有人会担心 SSH 的效率,Ansible 的并行执行及加速模式或许可以打消你的顾虑。2、使用简单,快速上手相当容易。Ansible 上手十分快,用 Ad-Hoc 可以应付简单的管理任务,麻烦点的也可以定义 Playbook 文件来搞定。3、采用人类易读的格式。Ansible 的主机定义文件使用 INI 格式,支持分组,能够指定模式;此外也能动态生成,这对管理云主机应当很有用。而 Playbook 则是 YAML 格式。4、能够使用你熟悉的语言来编写模块。虽然 Ansible 是使用 Python 开发的,但它不会将你限制到某种具体的编程语言,Bash、Python、Perl、Ruby 等等都可以,你擅长什么就用什么。

一言以蔽之,Ansible 背后的简单化哲学深得我心。这也比较符合我选择软件的一贯原则。可能还有人会比较关心目前 Ansible 都有谁在用。毕竟,榜样的力量是无穷。Puppet 不正是因为 Google 在用而吸引了不少眼球么?据我所知,当前使用 Ansible 较为知名的用户包括 Fedora、Rackspace、Evernote 等等。

Ansible企业应用:

未分类

二、主要架构功能

Ansible Core.    //核心功能

Modules:

Core Modules    //核心功能

Customed Modules  //自定义模块

Host Inventory        //主机库和主机清单,用来定义要管理的主机

File

CMDB(配置管理数据)

PlayBooks            //剧本,定义没个主机扮演的角色

Hosts.        //主机

roles.        //角色

Connection Plugins.  //连接插件,连接至被管控主机,完成并发连接,默认一次管理5台,但是可以修改。

三、安装配置

安装:

# yum install ansible -y  (epel仓库中)

程序:

ansible

ansible-playbook  //唱剧本

ansible-doc        //获取帮助文档

配置文件

/etc/ansible/ansible.cfg    //核心配置文件

主机清单:

/etc/ansible/hosts

插件目录:

/usr/share/ansible_plugins/

1、设置ansble到各个主机的免密钥通讯:

[root@cml1~]# ssh-keygen
[root@cml1~]# ssh-copy-id 192.168.5.102

2、定义主机组:

[root@cml1~]# cd /etc/ansible/
[root@cml1ansible]# vim hosts 
[webserver]
192.168.5.102
192.168.5.104

3、查看主机组内的主机:

[root@cml1ansible]# ansible webserver --list-hosts 
  hosts (2):
    192.168.5.102
    192.168.5.104

4、定义角色路径

[root@cml1~]# cat /etc/ansible/nginx.yaml 
- hosts: 192.168.5.102
 remote_user: root
  roles:
  -nginx_install    ###roles目录下的nginx_install目录
  -nginx_config    ###roles目录下的nginx_config目录

5、查看目录结构:

[root@cml1 roles]# tree
.
├── nginx_config
│  ├── default
│  ├── files
│  ├── handlers
│  │  └── main.yml
│  ├── meta
│  ├── tasks
│  │  └── main.yml
│  ├── templates
│  │  └── temp_server.conf
│  └── vars
│      └── main.yml
└── nginx_install
    ├──default
    ├──files
  │  └── nginx-1.12.0.tar.gz
    ├──handlers
  │  └── main.yml
    ├──meta
    ├──tasks
  │  └── main.yml
    ├──templates
  │  └── nginx.conf
└── vars
[root@cml1roles]# cd nginx_install/
[root@cml1nginx_install]# ls
default  files handlers  meta  tasks templates  vars

6、task定义开始任务:

[root@cml1nginx_install]# cd tasks/
[root@cml1tasks]# cat main.yml 
- name: copynginx package to remote host  
  copy: src=nginx-1.12.0.tar.gz  dest=/tmp/nginx-1.12.0.tar.gz  ##拉取nginx解压吧
  tags: cppkg
- name: tarnginx
  shell: cd /tmp;tar -xf nginx-1.12.0.tar.gz  ##解压nginx包
- name: installpakger
  yum: name={{ item }} state=latest    ##安装依赖包
  with_items:
    - openssl-devel
    - pcre-devel
    - gcc
- name: installnginx
  shell: cd /tmp/nginx-1.12.0;./configure--user=nginx --group=nginx --prefix=/usr/local/nginx--with-http_stub_status_module --with-http_ssl_module --with-pcre;make&& make install      ####编译安装
- name: copyconf file nginx.conf          
  template: src=nginx.confdest=/usr/local/nginx/conf/nginx.conf  ###复制在template目录下的配置文件
  tags: ngxconf
- name: copyshell
  copy: src=/opt/create_users.shdest=/tmp/create_users.sh  ##拉取创建用户的shell脚本
- name: createuser nginx
  shell: /bin/bash /tmp/create_users.sh
  tags: addnginx
  notify: start nginx service

为什么要写这个脚本?因为加入有些主机创建的用户已存在就会报错

[root@cml1tasks]# cat /opt/create_users.sh 
#!/bin/bash
a=`cat/etc/passwd | grep nginx | wc -l`
if [ $a == 0];then
      useradd nginx
fi

6、第二行copy对应file目录:

[root@cml1nginx_install]# cd files/
[root@cml1files]# ls
nginx-1.12.0.tar.gz

7、template这一行对应的是template这个目录和主服务端定义的变量:

[root@cml1nginx_install]# cd templates/
[root@cml1templates]# ls
nginx.conf
[root@cml1templates]# cat nginx.conf 

user  nginx;
worker_processes  {{ ansible_processor_vcpus }};

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  65535;
}


http {


    include      mime.types;
    default_type  application/octet-stream;

    #log_format main  '$remote_addr - $remote_user[$time_local] "$request" '
    #                  '$status $body_bytes_sent"$http_referer" '
    #                  '"$http_user_agent""$http_x_forwarded_for"';
  log_format xiaoluo  '$remote_addr -$remote_user  [$time_local]  '
                            '"$request"  $status$body_bytes_sent '
                            '"$http_referer" "$http_user_agent" ';
    #access_log logs/access.log  main;

    sendfile        on;
    #tcp_nopush    on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip on;

    server {
        listen      {{ ngxport }};  
        server_name  wwwNaNl.com
        access_log logs/wwwNaNl.com  xiaoluo;
        #location / {
        #  proxy_pass http://192.168.5.101;
        #}

        #error_page  404              /404.html;

        # redirect server error pages to thestatic page /50x.html
        #
        error_page  500 502 503 504  /50x.html;
        location = /50x.html {
            root  html;
        }

        # proxy the PHP scripts to Apachelistening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #  proxy_pass  http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGIserver listening on 127.0.0.1:9000
        #
        location ~ .php$ {
            root          /web;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME$document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, ifApache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #  deny  all;
        #}
    }
  include vhosts/*.conf;
}##需要注意的就是模板变量(客户端自动采集)、和在服务端定义的变量{{ngx_port}}

8、在vars定义变量:

[root@cml1nginx_install]# cd vars/
[root@cml1vars]# cat main.yml 
ngxport:"8080"

9、定义触发器:

[root@cml1nginx_install]# cd handlers/
[root@cml1handlers]# cat main.yml 
- name: startnginx service 
  shell: /usr/loal/nginx/sbin/nginx

10、在nginx_config目录加入我们经常要增加nginx站点,直接写好模板推送到vhos目录:

[root@cml1roles]# cd nginx_config/
[root@cml1nginx_config]# ls
default  files handlers  meta  tasks templates  vars
[root@cml1nginx_config]# cd templates/
[root@cml1templates]# ls
temp_server.conf
[root@cml1templates]# cat temp_server.conf 
server 
{ 
listen 80; 
server_name {{server_name }}; 
index index.phpindex.html; 
root {{root_dir }}; 
}

###在var定义变量:
[root@cml1templates]# cd ../vars/
[root@cml1vars]# cat main.yml 
server_name: "www.xiaoluo.com"
root_dir:"/web"

11、写配置nginx的tasks步骤:

[root@cml1nginx_config]# cd tasks/
[root@cml1tasks]# ls
main.yml
[root@cml1tasks]# cat main.yml 
- name: createvhosts
  shell: mkdir -p /usr/local/nginx/conf/vhosts/
  tags: create_dir
- name: copyconf file nginx.conf          # 调用templates模块
  template: src=temp_server.confdest=/usr/local/nginx/conf/vhosts/{{ server_name }}.conf
  tags: ngxconf
  notify: reload nginx service 
###定义重启触发器:
[root@cml1tasks]# cd ../handlers/
You have newmail in /var/spool/mail/root
[root@cml1handlers]# cat main.yml 
- name: reloadnginx service
  shell: /usr/local/nginx/sbin/nginx-t;/usr/local/nginx/sbin/nginx -s reload

测试:

[root@cml1ansible]# ansible-playbook -C nginx.yaml 

PLAY[192.168.5.104] ********************************************************** 

GATHERING FACTS*************************************************************** 
ok:[192.168.5.104]

TASK:[nginx_install | copy nginx package to remote host] ********************* 
changed:[192.168.5.104]

TASK:[nginx_install | tar nginx] ********************************************* 
skipping:[192.168.5.104]
ok:[192.168.5.104]

TASK:[nginx_install | install pakger] **************************************** 
changed: [192.168.5.104]=> (item=openssl-devel,pcre-devel,gcc)

TASK:[nginx_install | install nginx] ***************************************** 
skipping:[192.168.5.104]
ok:[192.168.5.104]

TASK:[nginx_install | copy conf file nginx.conf] ***************************** 
changed:[192.168.5.104]

TASK:[nginx_install | copy shell] ******************************************** 
changed:[192.168.5.104]

TASK:[nginx_install | create user nginx] ************************************* 
skipping:[192.168.5.104]
ok: [192.168.5.104]

TASK:[nginx_config | create vhosts] ****************************************** 
skipping:[192.168.5.104]
ok:[192.168.5.104]

TASK:[nginx_config | copy conf file nginx.conf] ****************************** 
changed:[192.168.5.104]

NOTIFIED:[nginx_config | reload nginx service] ******************************* 
skipping:[192.168.5.104]
ok:[192.168.5.104]

PLAY RECAP******************************************************************** 
192.168.5.104              : ok=6    changed=5  unreachable=0    failed=0[root@cml1 ansible]# ansible-playbook  nginx.yaml 
PLAY [192.168.5.104] ********************************************************** 
GATHERING FACTS *************************************************************** 
ok: [192.168.5.104]
TASK: [nginx_install | copy nginx package to remote host] ********************* 
ok: [192.168.5.104]
TASK: [nginx_install | tar nginx] ********************************************* 
changed: [192.168.5.104]
TASK: [nginx_install | install pakger] **************************************** 
ok: [192.168.5.104] => (item=openssl-devel,pcre-devel,gcc)
TASK: [nginx_install | install nginx] ***************************************** 
changed: [192.168.5.104]
TASK: [nginx_install | copy conf file nginx.conf] ***************************** 
ok: [192.168.5.104]
TASK: [nginx_install | copy shell] ******************************************** 
ok: [192.168.5.104]
TASK: [nginx_install | create user nginx] ************************************* 
changed: [192.168.5.104]
TASK: [nginx_config | create vhosts] ****************************************** 
changed: [192.168.5.104]
TASK: [nginx_config | copy conf file nginx.conf] ****************************** 
ok: [192.168.5.104]
NOTIFIED: [nginx_install | start nginx service] ******************************* 
changed: [192.168.5.104]
PLAY RECAP ******************************************************************** 
192.168.5.104              : ok=11  changed=5    unreachable=0    failed=0[root@cml3 ~]# ifconfig
ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.5.104  netmask 255.255.255.0  broadcast 192.168.5.255


[root@cml3 ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address          Foreign Address        State      PID/Program name              
tcp        0      0 0.0.0.0:80              0.0.0.0:*              LISTEN      29264/nginx: master 
tcp        0      0 0.0.0.0:8080            0.0.0.0:*              LISTEN      29264/nginx: master 

四、定义日志文件

下面是介绍如何定义日志:(在ansible1.9.1版本之后有个bug所以定义不了日志文件只能降版本到1.9.1了)

1、ansible倒回去版本:1.9.1

需要安装gcc、python-devel

[root@cml1 ~]#yum install python-pip
[root@cml1 ~]#pip install ansible==1.9.1

2、callback插件:

[root@cml1tasks]# vim /etc/ansible/ansible.cfg
callback_plugins  = /usr/share/ansible/plugins/callback
bin_ansible_callbacks= True

3、在callback目录下创建日志处理文件:

[root@cml1tasks]# cd /usr/share/ansible/plugins/callback
[root@cml1callback]# ls
log.py  log.pyc
[root@cml1callback]# cat log.py
import os
import time
import json

TIME_FORMAT="%b%d %Y %H:%M:%S"
MSG_FORMAT="%(now)s- %(category)s - %(data)snn"

if notos.path.exists("/var/log/ansible/hosts"):
  os.makedirs("/var/log/ansible/hosts")

def log(host,category, data):
    if type(data) == dict:
        if 'verbose_override' in data:
            # avoid logging extraneous datafrom facts
            data = 'omitted'
        else:
            data = data.copy()
            invocation = data.pop('invocation',None)
            data = json.dumps(data)
            if invocation is not None:
                data = json.dumps(invocation) +" => %s " % data

    path =os.path.join("/var/log/ansible/hosts", host)
    now = time.strftime(TIME_FORMAT,time.localtime())
    fd =open(path, "a")
    fd.write(MSG_FORMAT % dict(now=now,category=category, data=data))
    fd.close()

classCallbackModule(object):
    """
    logs playbook results, per host, in/var/log/ansible/hosts
    """

    def on_any(self, *args, **kwargs):
        pass

    def runner_on_failed(self, host, res,ignore_errors=False):
        log(host, 'FAILED', res)

    def runner_on_ok(self, host, res):
        log(host, 'OK', res)

    def runner_on_skipped(self, host,item=None):
        log(host, 'SKIPPED', '...')

    def runner_on_unreachable(self, host, res):
        log(host, 'UNREACHABLE', res)

    def runner_on_no_hosts(self):
        pass

    def runner_on_async_poll(self, host, res,jid, clock):
        pass

    def runner_on_async_ok(self, host, res,jid):
        pass

    def runner_on_async_failed(self, host, res,jid):
        log(host, 'ASYNC_FAILED', res)

    def playbook_on_start(self):
        pass

    def playbook_on_notify(self, host,handler):
        pass

    def playbook_on_no_hosts_matched(self):
        pass

    def playbook_on_no_hosts_remaining(self):
        pass

    def playbook_on_task_start(self, name,is_conditional):
        pass

    def playbook_on_vars_prompt(self, varname,private=True, prompt=None, encrypt=None, confirm=False, salt_size=None,salt=None, default=None):
        pass

    def playbook_on_setup(self):
        pass

    def playbook_on_import_for_host(self, host,imported_file):
        log(host, 'IMPORTED', imported_file)

    def playbook_on_not_import_for_host(self,host, missing_file):
        log(host, 'NOTIMPORTED', missing_file)

    def playbook_on_play_start(self, name):
        pass

    def playbook_on_stats(self, stats):
        pass

Ansible服务部署与使用 – ansible-playbook剧本

第4章 ansible-playbook 剧本

4.1 ansible基础知识部分补充

4.1.1 ansible软件特点:

  • 可以实现批量管理

  • 可以实现批量部署

  • ad-hoc(批量执行命令)—针对临时性的操作

ansible linuxidc -m command -a “hostname” <- 批量执行命令举例

  • 编写剧本-脚本(playbook)—针对重复性的操作

4.1.2 ansible核心功能:

pyYAML—–用于ansible编写剧本所使用的语言格式(saltstack—python)

rsync-ini语法 sersync-xml语法 ansible-pyYAML语法

paramiko—远程连接与数据传输

Jinja2—–用于编写ansible的模板信息

4.2 ansible剧本编写规则说明

4.2.1 pyYAML语法规则

规则一:缩进

yaml使用一个固定的缩进风格表示数据层结构关系,Saltstack需要每个缩进级别由两个空格组成。一定不能使用tab键

注意:编写yaml文件,就忘记键盘有tab

规则二:冒号

CMD=”echo”

yaml:

mykey:

每个冒号后面一定要有一个空格(以冒号结尾不需要空格,表示文件路径的模版可以不需要空格)

规则三:短横线

想要表示列表项,使用一个短横杠加一个空格。多个项使用同样的缩进级别作为同一个列表的一部分

核心规则:有效的利用空格进行剧本的编写,剧本编写是不支持tab的

4.3 剧本书写格式

### 剧本的开头,可以不写
- hosts: all         <- 处理所有服务器,找到所有服务器;  -(空格)hosts:(空格)all
tasks:             <- 剧本所要干的事情;                (空格)(空格)task:
- command: echo hello linuxidc linux.  
  (空格)(空格)空格)(空格)-(空格)模块名称:(空格)模块中对应的功能
 ansible all -m command -a "echo hello linuxidc linux"     

    剧本编写内容扩展:剧本任务定义名称

- hosts: 172.16.1.7  <- 处理指定服务器                   -(空格)hosts:(空格)all
task:                <- 剧本所要干的事情;                (空格)(空格)task:
- name:
command: echo hello linuxidc linux.                   
(空格)(空格)空格)(空格)-(空格)模块名称:(空格)模块中对应的功能

4.3.1 剧本格式示例

[root@m01 ansible-playbook]# vim rsync_sever.yml
- hosts: 172.16.1.41
  tasks:
    - name: install rsync
      yum: name=rsync state=installed

4.4 剧本编写后检查方法

01:ansible-playbook --syntax-check 01.yml 

        --- 进行剧本配置信息语法检查

02:ansible-playbook -C 01.yml             

           --- 模拟剧本执行(彩排)

4.4.1 语法检查

[root@m01 ansible-playbook]# ansible-playbook --syntax-check 01.yml
playbook: 01.yml

4.4.2 模拟剧本执行

[root@m01 ansible-playbook]# ansible-playbook -C 01.yml
PLAY [all] ****************************************************************

TASK [Gathering Facts] ****************************************************
ok: [172.16.1.41]
ok: [172.16.1.8]
ok: [172.16.1.31]

TASK [cron] ***************************************************************
ok: [172.16.1.8]
ok: [172.16.1.41]
ok: [172.16.1.31]

PLAY RECAP ****************************************************************
172.16.1.31                : ok=2    changed=0    unreachable=0    failed=0
172.16.1.41                : ok=2    changed=0    unreachable=0    failed=0
172.16.1.8                 : ok=2    changed=0    unreachable=0    failed=0

4.5 剧本示例

4.5.1 剧本编写内容扩展:剧本任务编写多个任务

- hosts: all
  tasks:
    - name: restart-network
      cron: name='restart network' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'
    - name: sync time
      cron: name='sync time' minute=*/5 job="/usr/sbin/ntpdate pool.ntp.com >/dev/null 2>&1"

4.5.2 剧本编写内容扩展:剧本任务编写多个主机

- hosts: 172.16.1.7
  tasks:
    - name: restart-network
      cron: name='restart network' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'
    - name: sync time
      cron: name='sync time' minute=*/5 job="/usr/sbin/ntpdate pool.ntp.com >/dev/null 2>&1"
- hosts: 172.16.1.31
  tasks:
    - name: show ip addr to file
      shell: echo $(hostname -i) >> /tmp/ip.txt

4.6 剧本编写方式

01 多主机单任务编写方式

02 多主机多任务编写方式

03 不同主机多任务编写方式


第5章 常见错误

5.1 ansible编写剧本排错思路

  • ansible-playbook编写完,检査语法和模拟测试运行

  • 打开剧本,定位异常问題原因,将剧本中的内容转换命令执行一次

cron: name=linuxidc64 minute=ee hour=03 job='/bin/sh /server/scripts/test.sh &>/dev/null'
ansible linuxidc -m cron -a "name=linuxidc64 minute=00 hour=03 job='/bin/sh /server/scripts/test.sh &>/dev/null
  • 将参数中的脚本文件推送到远程服务器,在远程服务器本地执行脚本 sh -x test.sh

说明:ansible执行时,加1上-vvvv显示ansible详细执行过程,也可以定位异常原因!

5.1.1 排错逻辑

  1. 剧本执行中的错误

  2. 把剧本中的内容转换为ansible命令执行

ansible linuxidc -m yum -a "name=rsync state=installed"
  1. 把ansible服务器上执行的命令放在被管理主机上执行
yum install -y rsync

5.2 ansible 无法正常使用

5.2.1 在被控端上 root@notty 进程一直存在

[root@backup ~]# ps -ef|grep sshd
root      35274      1  0 15:25 ?        00:00:00 /usr/sbin/sshd
root      37004  35274  0 16:23 ?        00:00:00 sshd: root@pts/2 
root      37062  35274  0 16:55 ?        00:00:00 sshd: root@notty 
root      37154  37006  0 16:55 pts/2    00:00:00 grep --color=auto sshd

5.2.2 解决办法

首先,将该进程干掉

kill pid

5.2.3 然后使用ansible的 -vvvv 参数查看执行的错误信息

Loading callback plugin minimal of type stdout, v2.0 from /usr/lib/python2.6/site-packages/ansible/plugins/callback/__init__.pyc
META: ran handlers
Using module file /usr/lib/python2.6/site-packages/ansible/modules/system/ping.py
<172.16.1.8> ESTABLISH SSH CONNECTION FOR USER: None
<172.16.1.8> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/923ebeb605 172.16.1.8 '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
……

找到在哪里出错。

5.2.4 可能的错误

在 /etc/ssh/sshd_config 文件中的第132行为空,导致sftp 无法连接,出错~

133 Subsystem      sftp    /usr/libexec/openssh/sftp-server

5.3 常见问题二

[root@m01 ~]# ansible  -k 172.16.1.51 -m ping
SSH password:
[WARNING]: No hosts matched, nothing to do

**原因分析:88

在ansible的hosts文件中,没有配置相应主机地址信息

5.3.1 常见问题三

# ansible -k 172.16.1.51 -m ping
SSH password:
172.16.1.51|FAILED! => {
"failed": true,
"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."
}

原因分析:

因为没有受控端的指纹信息,在known_hosts文件中

本博文中所使用的系统版本为: CentOS release 6.9 (Final) 内核版本为: 2.6.32-696.10.1.el6.x86_64 望读者注意!

Ansible服务部署与使用-Ansible模块

第3章 Ansible中的模块说明

3.1 ping 模块:测试连通性

[root@m01 ~]# ansible all -m ping 
172.16.1.8 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
172.16.1.41 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
172.16.1.31 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

连接正常返回 pong 通过帮助信息可以获得 ↓

通过 ansible-doc -v ping 可以获得该模块的说明

ansible-doc -s file 参看模块的具体信息

[root@m01 ~]# ansible-doc -v ping
Using /etc/ansible/ansible.cfg as config file
> PING    (/usr/lib/python2.6/site-packages/ansible/modules/system/ping.py)

  A trivial test module, this module always returns `pong' on successful contact. It does not make sense in playbooks, but it is useful from `/usr/bin/ansible' to verify the ability to login and that a usable  python is configured. This is NOT ICMP ping, this is just a trivial test module.

3.2 command 模块 默认模块

3.2.1 command命令常用参数说明

未分类

不指定模块的时候默认使用的模块就是command ↓

[root@m01 ~]# ansible all -a "date"
172.16.1.41 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:15 CST 2017

172.16.1.31 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:15 CST 2017

172.16.1.8 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:15 CST 2017

使用ansible自带模块执行命令 如果要用 > < | & ‘ ‘ 使用shell模块

[root@m01 ~]# ansible all -m command -a "date"
172.16.1.8 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:27 CST 2017

172.16.1.31 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:28 CST 2017

172.16.1.41 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:27 CST 2017

chdir参数的使用:

[root@m01 ~]# ansible linuxidc -m command -a "chdir=/tmp pwd"
172.16.1.31 | SUCCESS | rc=0 >>
/tmp

172.16.1.8 | SUCCESS | rc=0 >>
/tmp

172.16.1.41 | SUCCESS | rc=0 >>
/tmp

creates 文件是否存在,不存在就执行命令

[root@m01 ~]# ansible linuxidc -m command -a "creates=/etc/hosts date"
172.16.1.31 | SUCCESS | rc=0 >>
skipped, since /etc/hosts exists

removes 文件是否存在,不存在就不执行命令,

[root@m01 ~]# ansible linuxidc -m command -a "removes=/etc/hosts date"
172.16.1.31 | SUCCESS | rc=0 >>
Fri Oct 20 13:32:40 CST 2017

3.3 shell模块 万能模块

执行linux命令时可以用

远程节点执行命令

说明: shell 模块在远程执行脚本时,远程主机上一定要有相应的脚本

[root@m01 ~]# ansible linuxidc -m shell -a "/bin/sh /server/scripts/ssh-key.sh"
172.16.1.31 | SUCCESS | rc=0 >>
fenfa 172.16.1.31 [  OK  ]

fenfa 172.16.1.41 [  OK  ]

fenfa 172.16.1.8 [  OK  ]

3.4 script 模块 执行脚本模块

在本地执行脚本时,将脚本中的内容传输到远程节点上运行

[root@m01 ~]# ansible all -m script -a "/server/scripts/free.sh"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 172.16.1.8 closed.rn",
    "stdout": "             total       used       free     shared    buffers     cachedrnMem:          474M       377M        97M       532K        54M       202Mrn-/+ buffers/cache:       120M       354MrnSwap:         767M         0B       767Mrn",
    "stdout_lines": [
        "             total       used       free     shared    buffers     cached",
        "Mem:          474M       377M        97M       532K        54M       202M",
        "-/+ buffers/cache:       120M       354M",
        "Swap:         767M         0B       767M"
    ]
}

说明:

使用scripts模块,不用将脚本传输到远程节点,脚本本身不用进行授权,即可利用script模块执行。直接执行脚本即可,不需要使用sh

3.5 copy模块 把本地文件发送到远端

3.5.1 copy模块常用参数

未分类

说明: src和content不能同时使用

3.5.2 copy常用命令参数测试

使用copy 模块,将/etc/hosts 文件 传输到各个服务器送,权限修改为0600 属主属组为linuxidc

[root@m01 ~]# ansible linuxidc -m copy -a "src=/etc/hosts dest=/tmp/ mode=0600 owner=linuxidc group=oldboy "
172.16.1.8 | SUCCESS => {
    "changed": true,
    "checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",
    "dest": "/tmp/hosts",
    "gid": 500,
    "group": "linuxidc",
    "md5sum": "8c2b120b4742a806dcfdc8cfff6b6308",
    "mode": "0600",
    "owner": "linuxidc",
    "size": 357,
    "src": "/root/.ansible/tmp/ansible-tmp-1508410846.63-224022812989166/source",
    "state": "file",
    "uid": 500
}
……

检查结果

[root@m01 ~]# ansible all -m shell -a "ls -l /tmp/hosts"
172.16.1.31 | SUCCESS | rc=0 >>
-rw------- 1 linuxidc oldboy 357 Oct 19 19:00 /tmp/hosts

172.16.1.41 | SUCCESS | rc=0 >>
-rw------- 1 linuxidc oldboy 357 Oct 11 15:12 /tmp/hosts

172.16.1.8 | SUCCESS | rc=0 >>
-rw------- 1 linuxidc oldboy 357 Oct 19 19:00 /tmp/hosts

移动远程主机上的文件 remote_src=true 参数

[root@m01 ~]# ansible linuxidc -m copy -a " src=/server/scripts/ssh-key.sh  dest=/tmp/ remote_src=true"
172.16.1.41 | SUCCESS => {
    "changed": true,
    "checksum": "d27bd683bd37e15992d2493b50c9410e0f667c9c",
    "dest": "/tmp/ssh-key.sh",
    "gid": 0,
    "group": "root",
    "md5sum": "dc88a3a419e3657bae7d3ef31925cbde",
    "mode": "0644",
    "owner": "root",
    "size": 397,
    "src": "/server/scripts/ssh-key.sh",
    "state": "file",
    "uid": 0
}

定义文件中的内容 content=linuxidcedu.com 默认没有换行

[root@m01 ~]# ansible linuxidc -m copy -a "content=linuxidcedu.com dest=/tmp/linuxidc666.txt"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "checksum": "291694840cd9f9c464263ea9b13421d8e74b7d00",
    "dest": "/tmp/linuxidc666.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "0a6bb40847793839366d0ac014616d69",
    "mode": "0644",
    "owner": "root",
    "size": 13,
    "src": "/root/.ansible/tmp/ansible-tmp-1508466752.1-24733562369639/source",
    "state": "file",
    "uid": 0
}

3.6 file模块 设置文件属性

3.6.1 file模块常用参数

未分类

注意:重命名和创建多级目录不能同时实现

3.6.2 常用参数测试

创建目录

[root@m01 ~]# ansible linuxidc -m file -a "dest=/tmp/linuxidc_dir state=directory"
172.16.1.41 | SUCCESS => {
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/tmp/linuxidc_dir",
    "size": 4096,
    "state": "directory",
    "uid": 0
}

创建文件

[root@m01 ~]# ansible linuxidc -m file -a "dest=/tmp/linuxidc_file state=touch"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "dest": "/tmp/linuxidc_file",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "size": 0,
    "state": "file",
    "uid": 0
}

创建软连接

[root@m01 ~]# ansible linuxidc -m file -a "src=/tmp/linuxidc_file dest=/tmp/linuxidc_file_link state=link"
172.16.1.41 | SUCCESS => {
    "changed": true,
    "dest": "/tmp/linuxidc_file_link",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "size": 16,
    "src": "/tmp/linuxidc_file",
    "state": "link",
    "uid": 0
}

删除目录文件信息

[root@m01 ~]# ansible linuxidc -m file -a "dest=/tmp/linuxidc_dir state=absent"
172.16.1.41 | SUCCESS => {
    "changed": true,
    "path": "/tmp/linuxidc_dir",
    "state": "absent"

[root@m01 ~]# ansible linuxidc -m file -a "dest=/tmp/linuxidc_file state=absent"
172.16.1.31 | SUCCESS => {
    "changed": true,
    "path": "/tmp/linuxidc_file",
    "state": "absent"

创建多级目录

[root@m01 ~]# ansible linuxidc -m copy -a "src=/etc/hosts dest=/tmp/01/0/0/0/0/0/0/0/"
172.16.1.31 | SUCCESS => {
    "changed": true,
    "checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",
    "dest": "/tmp/01/0/0/0/0/0/0/0/hosts",
    "gid": 0,
    "group": "root",
    "md5sum": "8c2b120b4742a806dcfdc8cfff6b6308",
    "mode": "0644",
    "owner": "root",
    "size": 357,
    "src": "/root/.ansible/tmp/ansible-tmp-1508466973.39-99676412390473/source",
    "state": "file",
    "uid": 0
}

注意:重命名和创建多级目录不能同时实现

3.7 fetch 模块 拉取文件

3.7.1 fetch常用参数说明

未分类

3.7.2 常用参数实例

从远程拉取出来文件

[root@m01 cp]# ansible linuxidc -m fetch -a "dest=/tmp/backup src=/etc/hosts"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",
    "dest": "/tmp/backup/172.16.1.8/etc/hosts",
    "md5sum": "8c2b120b4742a806dcfdc8cfff6b6308",
    "remote_checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",
    "remote_md5sum": null
}
[root@m01 cp]# tree /tmp/backup/
/tmp/backup/
├── 172.16.1.31
│   └── etc
│       └── hosts
├── 172.16.1.41
│   └── etc
│       └── hosts
└── 172.16.1.8
    └── etc
        └── hosts

flat 参数,拉去的时候不创建目录(同名文件会覆盖)

[root@m01 tmp]# ansible linuxidc -m fetch -a "dest=/tmp/backup/ src=/etc/hosts flat=yes"
172.16.1.8 | SUCCESS => {
    "changed": false,
    "checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",
    "dest": "/tmp/backup/hosts",
    "file": "/etc/hosts",
    "md5sum": "8c2b120b4742a806dcfdc8cfff6b6308"

3.8 mount模块 配置挂载点模块

3.8.1 mount模块常用参数

未分类

3.8.2 mount参数实例

挂载

[root@m01 tmp]# ansible 172.16.1.8 -m mount -a "fstype=nfs opts=rw path=/mnt/  src=172.16.1.31:/data/ state=mounted"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "dump": "0",
    "fstab": "/etc/fstab",
    "fstype": "nfs",
    "name": "/mnt/",
    "opts": "rw",
 "passno": "0",
  "src": "172.16.1.31:/data/"
}

卸载

[root@m01 tmp]# ansible 172.16.1.8 -m mount -a "fstype=nfs opts=rw path=/mnt/  src=172.16.1.31:/data/ state=unmounted"
172.16.1.8 | SUCCESS => {
   "changed": true,
    "dump": "0",
    "fstab": "/etc/fstab",
    "fstype": "nfs",
    "name": "/mnt/",
    "opts": "rw",
    "passno": "0",
    "src": "172.16.1.31:/data/"
}

3.9 cron模块 定时任务

3.9.1 cron模块常用参数

未分类

3.9.2 cron模块参数实践

添加定时任务

[root@m01 ~]# ansible linuxidc -m cron -a "minute=0 hour=0 job='/bin/sh  /server/scripts/hostname.sh &>/dev/null' name=linuxidc01"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
     "linuxidc01"
    ]
}

删除定时任务

[root@m01 ~]# ansible linuxidc -m cron -a "minute=00 hour=00 job='/bin/sh  /server/scripts/hostname.sh &>/dev/null' name=linuxidc01 state=absent"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": []
}

只用名字就可以删除

[root@m01 ~]# ansible linuxidc -m cron -a "name=linuxidc01  state=absent"
172.16.1.31 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": []
}

注释定时任务

注意: 注释定时任务的时候必须有job的参数

[root@m01 ~]# ansible linuxidc -m cron -a "name=linuxidc01 job='/bin/sh  /server/scripts/hostname.sh &>/dev/null'  disabled=yes"
172.16.1.31 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
    "linuxidc01"
    ]
}

取消注释

[root@m01 ~]# ansible linuxidc -m cron -a "name=linuxidc01 job='/bin/sh  /server/scripts/hostname.sh &>/dev/null'  disabled=no"
172.16.1.41 | SUCCESS => {
    "changed": true,
    "envs": [],
   "jobs": [
       "linuxidc01"
    ]
}

3.10 yum 模块

3.10.1 yum 模块常用参数

未分类

3.10.2 yum模块参数实践

[root@m01 ~]# ansible linuxidc -m yum -a "name=nmap state=installed  "
172.16.1.31 | SUCCESS => {
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Loaded plugins: fastestmirror, securitynSetting up Install ProcessnLoading mirror speeds from cached hostfilen * base: mirrors.aliyun.comn * epel: mirrors.aliyun.comn * extras: mirrors.aliyun.comn * updates: mirrors.aliyun.comnResolving Dependenciesn--> Running transaction checkn---> Package nmap.x86_64 2:5.51-6.el6 will be installedn--> Finished Dependency ResolutionnnDependencies Resolvednn================================================================================n Package        Arch             Version                   Repository      Sizen================================================================================nInstalling:n nmap           x86_64           2:5.51-6.el6              base           2.8 MnnTransaction Summaryn================================================================================nInstall       1 Package(s)nnTotal download size: 2.8 MnInstalled size: 9.7 MnDownloading Packages:nRunning rpm_check_debugnRunning Transaction TestnTransaction Test SucceedednRunning Transactionnr  Installing : 2:nmap-5.51-6.el6.x86_64                                     1/1 nr  Verifying  : 2:nmap-5.51-6.el6.x86_64                                     1/1 nnInstalled:n  nmap.x86_64 2:5.51-6.el6                                                      nnComplete!n"
    ]
}

3.11 service模块 服务管理

3.11.1 service模块常用参数说明

未分类

说明 :service 管理的服务必须存在在/etc/init.d/下有的服务脚本

3.11.2 service 模块参数实践

重启定时任务

[root@m01 ~]# ansible linuxidc -m service -a "name=crond state=restarted"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "name": "crond",
    "state": "started"
}

3.12 ansible中的常用模块

未分类

3.13 其他模块补充

3.13.1 hostname 修改主机名模块

[root@m01 ~]# ansible 172.16.1.8 -m hostname -a "name=web01"
172.16.1.8 | SUCCESS => {
    "ansible_facts": {
        "ansible_domain": "etiantian.org",
        "ansible_fqdn": "www.etiantian.org",
       "ansible_hostname": "web01",
        "ansible_nodename": "web01"
    },
    "changed": false,
    "name": "web01"
}

3.13.2 selinux 管理模块

[root@m01 ~]# ansible 172.16.1.8 -m selinux -a "state=disabled"
172.16.1.8 | SUCCESS => {
    "changed": false,
    "configfile": "/etc/selinux/config",
    "msg": "",
    "policy": "targeted",
    "state": "disabled"
}

3.13.3 get_url 模块 == 【wget】

[root@m01 ~]# ansible 172.16.1.8 -m get_url -a "url=http://lan.linuxidc.com/RDPWrap-v1.6.1.zip dest=/tmp/"
172.16.1.8 | SUCCESS => {
    "changed": true,
    "checksum_dest": null,
    "checksum_src": "ad402705624d06a6ff4b5a6a98c55fc2453b3a70",
    "dest": "/tmp/RDPWrap-v1.6.1.zip",
    "gid": 0,
    "group": "root",
    "md5sum": "b04dde546293ade71287071d187ed92d",
    "mode": "0644",
    "msg": "OK (1567232 bytes)",
    "owner": "root",
    "size": 1567232,
    "src": "/tmp/tmp4X4Von",
    "state": "file",
    "status_code": 200,
    "uid": 0,
    "url": "http://lan.linuxidc.com/RDPWrap-v1.6.1.zip"
}

url= 下载文件的地址 dest 下载到哪里

timeout 超时时间

url_password 密码

url_username 用户名