Blog

  • ansible tasks判断一个主机是不是在某group

    hosts文件里是这样写的

    [backup_tar]
    host1
    host2

    [backup_notar]
    host3
    host4

    针对 backup_tar 中的 hosts 只执行某条 task

    - cron: name="backup" minute=0 hour=4
            user="root" job="/bin/sh /root/mysql_backup.sh full >> /dev/null 2>&1" backup=yes
      when: inventory_hostname in groups['backup_tar']
    

    参考:https://groups.google.com/forum/#!topic/ansible-project/lr7nXyGw0UY

  • ansible直接通过ip操作机器

    如果要了解一两个 ip 的一些简单的情况如 ls -l /data0/ | wc -l,以前是要写到文件里的。

    ansible -i hosts all -m shell -a "ls -l /data0/ | wc -l"

    也可以这样,一台机器最后面要有一个逗号

    ansible all -i 192.168.1.1, -m shell -a "ls -l /data0/ | wc -l"

    两台机器用逗号隔开,并且不能有空格

    ansible all -i 192.168.1.1,192.168.1.2 -m shell -a "ls -l /data0/ | wc -l"

    参考: http://stackoverflow.com/questions/17188147/how-to-run-ansible-without-specifying-the-inventory-but-the-host-directly?answertab=votes#tab-top

    题外话:

    除了上面的 shell 模块,还有 command, script 可以在被管理机器上方便执行 shell.

    ansible all -i hosts -l 192.168.1.183 -m script -a uptime.sh
  • centos4机器被ansible管理

    使用 ansible 管理 centos4 的机器会报错, 默认安装的 python 是 2.3.4 ,需要在 centos4 上的机器添加 python2.7 的环境,以便于被 ansible 管理。

    下载安装

    wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tgz --no-check-certificate
    tar xzf Python-2.7.5.tgz 
    cd Python-2.7.5
    ./configure --enable-shared
    make
    make altinstall

    接下来创建一个记录 python lib 目录的配置文件,/etc/ld.so.conf.d/python27.conf 里记录

    /usr/local/lib

    并用 ldconfig 加载到系统

    ldconfig -v -f /etc/ld.so.conf.d/python27.conf

    保证 yum 可用

    cp /usr/bin/python /usr/bin/python2.3.4
    vim /usr/bin/yum 第一行改为
    #!/usr/bin/python2.3.4
    

    系统级别

    ln -nfs /usr/local/bin/python2.7 /usr/bin/python

    参考:
    http://stivesso.blogspot.jp/2014/08/ansible-hosts-install-alternate.html

  • python dict sort

    python 字典(dict)的特点就是无序的,按照键(key)来提取相应值(value),如果我们需要字典按值排序的话,那可以用下面的方法来进行:

    1 下面的是按照value的值从大到小的顺序来排序。

    dic = {'a':31, 'bc':5, 'c':3, 'asd':4, 'aa':74, 'd':0}
    dict= sorted(dic.iteritems(), key=lambda d:d[1], reverse = True)
    print dict

    输出的结果:

    [('aa', 74), ('a', 31), ('bc', 5), ('asd', 4), ('c', 3), ('d', 0)]

    下面我们分解下代码
    print dic.iteritems() 得到[(键,值)]的列表。
    然后用sorted方法,通过key这个参数,指定排序是按照value,也就是第一个元素d[1的值来排序。reverse = True表示是需要翻转的,默认是从小到大,翻转的话,那就是从大到小。

    2 对字典按键(key)排序:

    dic = {'a':31, 'bc':5, 'c':3, 'asd':4, 'aa':74, 'd':0}

    dict= sorted(dic.iteritems(), key=lambda d:d[0]) d[0]表示字典的键
    print dict

    http://www.cnpythoner.com/post/266.html

  • dd清除GPT信息

    方法一

    dd if=/dev/zero of=/dev/sdb bs=512 count=2

    方法二

    parted /dev/sdb mklabel gpt

    参考:
    http://unix.stackexchange.com/questions/167500/using-dd-to-zero-a-gpt-table-from-disc-how-many-bytes-to-write

  • javascript中的object

    “In C++ or C#, when we’re talking about objects, we’re referring to instances of classes or structs. Objects have different properties and methods, depending on which templates (that is, classes) they are instantiated from. That’s not the case with JavaScript objects. In JavaScript, objects are just collections of name/value pairs – think of a JavaScript object as a dictionary with string keys.”

    摘录来自: Manuel Kiessling. “The Node Beginner Book”

    自己在 node 中尝试

    > var handle = {}
    undefined
    > handle["/"] = 'aaa';
    'aaa'
    > handle["/start"] = 'aaa/start';
    'aaa/start'
    > handle["/upload"] = 'aaa/upload';
    'aaa/upload'
    > handle
    { '/': 'aaa',
    '/start': 'aaa/start',
    '/upload': 'aaa/upload' }
    > typeof(handle)
    'object'