在web组里tmp目录下创建多个文件
[root@ansible roles]# cat file1.yml - hosts: web remote_user: tasks: - name: create file file: name=/tmp/{{ item }} state=touch with_items: - file1 - file2 - file3 # for i in list done; item 就是for循环的 i 变量,with_items里的file1....就是for循环的list里的东西
迭代嵌套子变量
迭代创建用户user1...user3,并家到组g1...g3组里
[root@ansible roles]# cat user.yml - hosts: web remote_user: tasks: - name: create gourp group: name={{ item }} with_items: - g1 - g2 - g3 - name: create user user: name={{item.name}} group={{item.group}} with_items: # | - { name: 'user1', group: 'g1' } #name和group 要和上面的对应,和命令无关 user1 是用户名 ,'g1'就是组名 - { name: user2, group: g2 } - { name: user3, group: g3 }
下载多个文件,并解压
- name: load pag get_url: url={{ item }} dest=/code/ with_items:
- https://cn.wordpress.org/wordpress-5.0.2-zh_CN.tar.gz - http://ahdx.down.chinaz.com/201605/WeCenter_v3.1.9.zip - http://download.edusoho.com/edusoho-8.2.17.tar.gz - name: tar 1 unarchive: src=files/{{ item }} dest=/code/ owner={{ user }} group={{ group }} copy=yes with_items: - wordpress-5.0.2-zh_CN.tar.gz - edusoho-8.2.17.tar.gz - WeCenter_v3.1.9.zip
创建目录
[root@ansible roles]# cat dir.yml - hosts: web remote_user: root vars: - dir1: /tmp/dir1 - dir2: /mnt/dir2 tasks: - name: create dir file: name={{ item }} state=directory with_items: - "{{ dir1 }}" # 引用变量是要加双引号 - "{{ dir2 }}"
执行结果
[root@ansible roles]# ansible-playbook dir.yml PLAY [web] ***************************************************************************************************************************************************************** TASK [Gathering Facts] ***************************************************************************************************************************************************** ok: [192.168.2.7] TASK [create dir] ********************************************************************************************************************************************************** changed: [192.168.2.7] => (item=/tmp/dir1) changed: [192.168.2.7] => (item=/mnt/dir2) PLAY RECAP ***************************************************************************************************************************************************************** 192.168.2.7 : ok=2 changed=1 unreachable=0 failed=0
playbook批量挂载nfs
{{ item.path }} 定义要挂载的目标,
src={{ nfs_ip }} 就是nfs的ip地址 {{ item.src }} 就是本地的目录,比如blog需要挂载的目录
fstype=nfs 挂载的类型
state=mounted 即写入/etc/fstab.不用重启,直接挂载
umounted 卸载
present 只写入/etc/fstab,不挂载
absent 删除/etc/fstab 中的文件
1 - name: moutn 2 #mount: path={{ item.path }} src={{ nfs_ip }}:{{ item.src }} fstype=nfs state=present 3 mount: path={{ item.path }} src={{ nfs_ip }}{{ item.src }} fstype=nfs state=present 4 with_items: 5 - { path: "{{ zh_dir }}", src: "{{ nfszh_dir }}" } 6 - { path: "{{ blog_dir }}", src: "{{ nfsblog_dir }}" } 7 - { path: "{{ edu_dir }}", src: "{{ nfsedu_dir }}" } 8 tags: mount
vars 变量设置
有特殊符号的要加引号
1 [root@nfs roles]# cat nfs/vars/main.yml 2 nfsblog_dir: /data/blog 3 nfszh_dir: /data/zh 4 nfsedu_dir: /data/edu 5 nfs_ip: "192.168.2.31:" 6 zh_dir: /code/zh/uploads 7 blog_dir: /code/wordpress/wp-content/uploads 8 edu_dir: /code/edu/app/data/udisk
template 中的for循环
用for循环实现这种效果
server { listen 80 } server { listen 81 } server { listen 82 } 1
剧本
[root@ansible roles]# cat for1.yml - hosts: web remote_user: root vars: ports: #定义的变量 - 81 - 82 - 83 tasks: - name: cp conf template: src=for1.conf.j2 dest=/tmp/for1.conf
模板文件
[root@ansible roles]# cat templates/for1.conf.j2 {% for port in ports %} #port(是变量相当于for循环里的变量i) in ports(是上面剧本里定义的列表里的变量81,82...) server { listen {{ port }} #相当于打印 for循环的变量 i } {% endfor %}
列表用字典的方式写
字典应用1
模板文件
[root@ansible roles]# cat templates/for2.conf.j2 {% for port in ports %} server { listen {{ port.listen_port }} } {% endfor %}
剧本
[root@ansible roles]# cat for2.yml - hosts: web remote_user: root vars: ports: - listen_port: 81 - listen_port: 82 - listen_port: 83 tasks: - name: cp conf template: src=for2.conf.j2 dest=/tmp/forfor1.conf
字典应用2
[root@ansible roles]# cat for3.yml - hosts: web remote_user: root vars: ports: - web1: port: 81 name: web1.a.com rootdir: /code - web2: port: 82 name: web2.b.com rootdir: /code tasks: - name: cp conf template: src=for3.conf.j2 dest=/tmp/for3.conf
模板文件
[root@ansible roles]# cat templates/for3.conf.j2 {% for p in ports %} server { listen {{ p.port }} servername {{ p.name }} docdir {{ p.rootdir }} } {% endfor %}
执行结果
[root@web02 tmp]# cat for3.conf server { listen 81 servername web1.a.com docdir /code } server { listen 82 servername web2.b.com docdir /code }
if 判断
[root@ansible roles]# cat if1.yml - hosts: web remote_user: root vars: ports: - web1: port: 81 #name: web1.a.com #注释掉此行 rootdir: /code - web2: port: 82 name: web2.b.com rootdir: /code tasks: - name: cp conf template: src=if1.conf.j2 dest=/tmp/if1.conf
模板文件
[root@ansible roles]# cat templates/if1.conf.j2 {% for p in ports %} server { listen {{ p.port }} {% if p.name is defined %} #if判断p的name有没有值,有值就生成 下面的p.name行,也就是sevrver_name servername {{ p.name }} {% endif %} docdir {{ p.rootdir }} } {% endfor %}
执行结果
[root@web02 tmp]# cat if1.conf server { listen 81 #此处经过if判断没用servername行 docdir /code } server { listen 82 servername web2.b.com docdir /code }