1.迭代:with_itrems

迭代:当有需要重复执行的任务时,可以使用迭代机制
- 对迭代项的引用,固定变量名为 “itrem”
- 要在task中使用with_itrems给定要迭代的元素列表
- 列表格式:
字符串
字典

YAML文件在线测试

环境

[webserver]
10.0.0.48 http_port=81
10.0.0.49 http_port=82[webserver:vars]
nodename=www
domainname=liming.com
[logserver]
10.0.0.49
10.0.0.54   #新建的虚拟机6已经搭好了

示例1

1.1.2查看表示版本的变量

[root@ansible ansible]# ansible all -m setup | grep version"ansible_bios_version": "15.1.4 (47270)","ansible_distribution_major_version": "6","ansible_distribution_version": "6.9","ansible_kernel_version": "#1 SMP Tue Mar 21 19:29:05 UTC 2017","ansible_product_version": "None","version": {"version_info": ["ansible_python_version": "2.6.6","ansible_bios_version": "15.1.4 (47270)","ansible_distribution_major_version": "7","ansible_distribution_version": "7.7","ansible_kernel_version": "#1 SMP Wed Aug 7 18:08:02 UTC 2019","ansible_product_version": "None","version": {"version_info": ["ansible_python_version": "2.7.5","ansible_bios_version": "15.1.4 (47270)","ansible_distribution_major_version": "7","ansible_distribution_version": "7.7","ansible_kernel_version": "#1 SMP Wed Aug 7 18:08:02 UTC 2019","ansible_product_version": "None","version": {"version_info": ["ansible_python_version": "2.7.5",

1.1.3新建yaml文件

[root@ansible ansible]# cat testiterm.yml
---
- hosts: allremote_user: roottasks:- name: create some filefile: name=/data/{{ item }} state=touchwhen: ansible_distribution_major_version == "7"with_items:- file1- file2- file3- name: install some packagesyum: name={{ item }}with_items:- htop- sl- hping3

1.1.4验证

#因为有条件判断 所以54 是6的机器 没有创建文件
[root@ansible ansible]# ansible all -m shell -a 'ls /data'
10.0.0.54 | FAILED | rc=2 >>  #已经跳过54 没有文件
ls: 无法访问/data: 没有那个文件或目录non-zero return code
10.0.0.49 | CHANGED | rc=0 >>
file1  #安装了三个文件
file2
file3
vsftpd.log
web82.example.com.log
10.0.0.48 | CHANGED | rc=0 >>
file1
file2
file3
vsftpd.log
web81.example.com.log#没有条件判断所以都安装上了
[root@ansible ansible]# ansible all -m shell -a "rpm -q htop sl hping3"
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'.  If you need to use command
because yum, dnf or zypper is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
10.0.0.54 | CHANGED | rc=0 >>
htop-1.0.3-1.el6.x86_64
sl-5.02-1.el6.x86_64
hping3-0.0.20051105-16.el6.x86_64
10.0.0.49 | CHANGED | rc=0 >>
htop-2.2.0-3.el7.x86_64
sl-5.02-1.el7.x86_64
hping3-0.0.20051105-24.el7.x86_64
10.0.0.48 | CHANGED | rc=0 >>
htop-2.2.0-3.el7.x86_64
sl-5.02-1.el7.x86_64
hping3-0.0.20051105-24.el7.x86_64

示例2

1.新建yaml文件

[root@ansible ansible]# cat testiterm2.yml
---
- hosts: allremote_user: roottasks:- name: create some groupsgroup: name={{ item }}when: ansible_distribution_major_version == "7"with_items:- g1- g2- g3

2 .验证组是否被创建成功

ansible all -m shell -a 'getent group'

2.迭代嵌套子变量

  • 将每个用户加入到每个组
[root@ansible ansible]# cat testiterm3.yml
---
- hosts: webserverremote_user: roottasks:- name: create some groupsgroup: name={{ item }}when: ansible_distribution_major_version == "7"with_items:- g1- g2- g3- name: create some usersuser: name={{item.name}} group={{item.group}}with_items:- { name: 'user1', group: 'g1'}- { name: 'user2', group: 'g2'}- { name: 'user3', group: 'g3'}

3.playbook中template for if

1.示例1:列表

1.1新建配置文件

[root@ansible ansible]# cat testiterm4.yml
---
- hosts: webserverremote_user: rootvars:ports:- 81- 82- 83tasks:- name: cp conftemplate: src=for1.conf.j2 dest=/data/for1.conf

1.2 在templates文件夹里新建模版文件

[root@ansible ansible]# cat templates/for1.conf.j2
{% for port in ports %}
server{listen {{ port }}  #port从yaml文件中来
}
{% endfor %}

1.3 验证

[root@ansible ansible]# ansible webserver -m shell -a 'ls /data'
10.0.0.48 | CHANGED | rc=0 >>
file1
file2
file3
for1.conf
vsftpd.log
web81.example.com.log
10.0.0.49 | CHANGED | rc=0 >>
file1
file2
file3
for1.conf
vsftpd.log
web82.example.com.log
[root@ansible ansible]# ansible webserver -m shell -a 'cat /data/for1.conf'
10.0.0.48 | CHANGED | rc=0 >>
server{listen 81
}
server{listen 82
}
server{listen 83
}
10.0.0.49 | CHANGED | rc=0 >>
server{listen 81
}
server{listen 82
}
server{listen 83
}

2.示例2:键值对

2.1新建配置文件

[root@ansible ansible]# cat for2.yml
---
- hosts: webserverremote_user: rootvars:ports:- listen_port: 81- listen_port: 82- listen_port: 83tasks:- name: cp conftemplate: src=for2.conf.j2 dest=/data/for2.conf

2.2新建配置文件

[root@ansible ansible]# cat templates/for2.conf.j2
{% for port in ports %}
server{listen {{ port.listen_port }}
}
{% endfor %}

2.3验证

[root@ansible ansible]# ansible webserver -m shell -a 'ls /data'
10.0.0.48 | CHANGED | rc=0 >>
file1
file2
file3
for1.conf
for2.conf
vsftpd.log
web81.example.com.log
10.0.0.49 | CHANGED | rc=0 >>
file1
file2
file3
for1.conf
for2.conf
vsftpd.log
web82.example.com.log
[root@ansible ansible]# ansible webserver -m shell -a 'cat /data/for2.conf'
10.0.0.49 | CHANGED | rc=0 >>
server{listen 81
}
server{listen 82
}
server{listen 83
}
10.0.0.48 | CHANGED | rc=0 >>
server{listen 81
}
server{listen 82
}
server{listen 83
}

示例3: 多个键值对

[root@ansible ansible]# cat for3.yml
---
- hosts: webserverremote_user: rootvars:ports:- web1:port: 81name: web1.node.comrootdir: /data/website1- web2:port: 82name: web2.node.comrootdir: /data/website2- web3:port: 83name: web3.node.comrootdir: /data/website3tasks:- name: cp conftemplate: src=for3.conf.j2 dest=/data/for3.conf
[root@ansible ansible]# cat templates/for3.conf.j2
{% for p in ports %}
server{listen {{ p.port }}servername {{ p.name }}documentroot {{ p.rootdir }}
}
{% endfor %}
[root@ansible ansible]# ansible webserver -m shell -a 'ls /data'
10.0.0.49 | CHANGED | rc=0 >>
file1
file2
file3
for1.conf
for2.conf
for3.conf
vsftpd.log
web82.example.com.log
10.0.0.48 | CHANGED | rc=0 >>
file1
file2
file3
for1.conf
for2.conf
for3.conf
vsftpd.log
web81.example.com.log
[root@ansible ansible]# ansible webserver -m shell -a 'cat /data/for3.conf'
10.0.0.49 | CHANGED | rc=0 >>
server{listen 81servername web1.node.comdocumentroot /data/website1
}
server{listen 82servername web2.node.comdocumentroot /data/website2
}
server{listen 83servername web3.node.comdocumentroot /data/website3
}
10.0.0.48 | CHANGED | rc=0 >>
server{listen 81servername web1.node.comdocumentroot /data/website1
}
server{listen 82servername web2.node.comdocumentroot /data/website2
}
server{listen 83servername web3.node.comdocumentroot /data/website3
}

示例4: 加上if条件判断

[root@ansible ansible]# cat for4.yml
---
- hosts: webserverremote_user: rootvars:ports:- web1:port: 81#name: web1.node.comrootdir: /data/website1- web2:port: 82name: web2.node.comrootdir: /data/website2- web3:port: 83#name: web3.node.comrootdir: /data/website3tasks:- name: cp conftemplate: src=for4.conf.j2 dest=/data/for4.conf
[root@ansible ansible]# cat templates/for4.conf.j2
{% for p in ports %}
server{listen {{ p.port }}
{% if p.name is defined %}servername {{ p.name }}
{% endif %}documentroot {{ p.rootdir }}
}
{% endfor %}
[root@ansible ansible]# ansible webserver -m shell -a 'ls /data'
10.0.0.48 | CHANGED | rc=0 >>
file1
file2
file3
for1.conf
for2.conf
for3.conf
for4.conf
vsftpd.log
web81.example.com.log
10.0.0.49 | CHANGED | rc=0 >>
file1
file2
file3
for1.conf
for2.conf
for3.conf
for4.conf
vsftpd.log
web82.example.com.log
[root@ansible ansible]# ansible webserver -m shell -a 'cat /data/for4.conf'
10.0.0.48 | CHANGED | rc=0 >>
server{listen 81documentroot /data/website1
}
server{listen 82servername web2.node.comdocumentroot /data/website2
}
server{listen 83documentroot /data/website3
}
10.0.0.49 | CHANGED | rc=0 >>
server{listen 81documentroot /data/website1
}
server{listen 82servername web2.node.comdocumentroot /data/website2
}
server{listen 83documentroot /data/website3
}
[root@ansible ansible]#

roles

  • roles
    ansible自1.2版本引入的新特性,用于层次性、结构化地组织playbook。roles能够根据层次性结构自动装载变量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include(即将过期)指令即可。简单来讲,roles就是通过分别将变量、文件、模板以及处理器放置于单独的目录中,并可以便捷地include它们的一种机制。角色一般用于基于主机构建服务的场景中,但也可以使用用于构建守护进程等场景中
  • 复杂场景:建议使用roles,代码复用度高
    - 变更指定主机或主机组
    - 如命名不规范维护和传承成本大
    - 某些功能需多个playbook,通过includes实现

roles使用

  • 角色(roles):角色集合
    roles/
    mysql/
    httpd/
    nginx/
    memcached/
[root@ansible ansible]# cd /etc/ansible/  #官方推荐路径
[root@ansible ansible]# ls
ansible.cfg  hosts  roles  #this

部署nginx服务

1.思路

  1. group: nginx
  2. user: nginx(要加入到nginx组)
  3. yum: nginx
  4. template:nginx.conf.j2
  5. service: nginx

2.查看用户和组是否存在

[root@ansible ansible]# ansible all -m shell -a 'getent group nginx'
[root@ansible ansible]# ansible all -m shell -a 'getent passwd nginx'[root@ansible ansible]# ansible all -m shell -a 'userdel -r nginx'  #删除

3. ansible roles目录编排

4.roles目录结构

  • 每个角色,以特定的层级目录结构进行组织
  • roles目录结构
    playbook.yml
    roles/
    project/
    tasks/
    files/
    vars/ 不常用
    default/ 不常用
    templates/
    handlers/
    meta/ 不常用

5.roles各目录作用

  • /roles/project/: 项目名称,有以下子目录

    • files/ :存放由copy或script模块等调用的文件
    • templates/ : template模块查找所需模板文件的目录
    • tasks/: 定义task.role的基本元素,至少应该包含一个名为main.yml的文件;其它的文件需要在此文件中通过include进行包含
    • handers/ :至少应该包含一个名为main.yml的文件;其它的文件需要在此文件中通过include进行包含
    • vars/: 定义变量,至少应该包含一个名为main.yml的文件;其它的文件需要在此文件中通过include进行包含
    • meta/:定义当前角色的特殊设定及其依赖关系,至少应该包含一个名为main.yml的文件,其它文件需在此文件中通过include进行包含
    • default/: 设定默认变量时使用此目录中的main.yml文件

6.

8.ansible高级进阶-role详解相关推荐

  1. Ansible Role详解

    Ansible Role 详解 Roles介绍 ansible自1.2版本引入的新特性,用于层次性.结构化地组织playbook.roles能够根据层次型结构自动装载变量文件.tasks以及handl ...

  2. 区块链技术进阶-深入详解以太坊智能合约语言 solidity(含源码)-熊丽兵-专题视频课程...

    区块链技术进阶-深入详解以太坊智能合约语言 solidity(含源码)-103人已学习 课程介绍         区块链开发技术进阶-深入详解以太坊智能合约语言 solidity视频培训教程:本课程是 ...

  3. MySQL高级之explain详解

    MySQL高级之explain详解 文章目录 MySQL高级之explain详解 一.expalin命令详解 1.使用方式 2.结果显示 3.主要的字段信息 4.作用 二.id字段 三.select_ ...

  4. Android进阶——Preference详解之Preference系的基本应用(三)

    引言 前面一篇文章Android进阶--Preference详解之Preference系的基本应用和管理(二)介绍了二级Preference的使用和特点,接下来进入系统给我提供的底级Preferenc ...

  5. 搜索引擎高级搜索指令详解

    在当今外贸竞争越来越惨烈的情况下,广大站长没有两把刷子是不行滴.学习外贸SEO就是一个方法. 当初凭借傻瓜式就可以赚取美金等外币的时代已经悄悄远去,当下如果要提高站点排名,提升外贸网站流量,那么必须使 ...

  6. ansible自动化运维详解(三)ansible常用模块续

    文章目录 ansible自动化运维详解(三)ansible常用模块续 四.ansible常用模块(2) 4.10.yum_repository 4.11.dnf 4.12.service 及 fire ...

  7. ansible自动化运维详解(一)ansible的安装部署、参数使用、清单管理、配置文件参数及用户级ansible操作环境构建

    文章目录 ansible自动化运维详解(一)ansible的安装部署.参数使用.清单管理.配置文件参数及用户级ansible操作环境构建 一.ansible的安装部署 1.1.ansible简介 1. ...

  8. C#高级--加密解密详解

    C#高级–加密解密详解 零.文章目录 一.名词介绍 1.加密 是以某种特殊的算法改变原有的信息数据,以另外一种形式呈现,这里有几个名词:加密之前的信息数据可以理解为原数据,原文:加密之后的数据信息可以 ...

  9. Android自定义View进阶-MotionEvent详解

    欢迎Follow我的GitHub, 关注我的CSDN. 其余参考Android目录 我们微信公众号:杨守乐 推荐文章: 如果你喜欢上了一个程序员小伙,献给所有的程序员女友 学习资料(干货汇集)不断更新 ...

最新文章

  1. 过去一个月发生了什么,C++再次真香了吗?
  2. linux lsm 程序加载钩函数,LSM在Linux中的实现方式
  3. 获取${}中的值? 比如说var a=${date },无法取出date中的值
  4. 操作系统——内存管理——分段和分页
  5. linux端口接收中文乱码,linux中显示中文乱码如何解决
  6. 性能测试工具AlldayTest 的测试方法及说明
  7. 学习资料收集:计算机系统基础
  8. 前端实现红包雨功能_微信隐藏的7个实用功能,你都知道吗?真的白玩这么久微信...
  9. DIAMOND: 超快的蛋白序列比对软件
  10. WIFI工具移植之IW工具移植
  11. gentoo linux软件安装,Gentoo Linux 快速安装方法安装
  12. 顶尖领导者的52条法则!
  13. offlineimap读取qq邮箱
  14. 又来了!10分钟实现微信 “炸屎“大作战
  15. .php可以转换为.jpg么,php pdf转换为jpg的方法
  16. 对话黄骁俭:SAP的工程师文化
  17. Jmeter察看结果树中响应数据显示乱码
  18. 移动端事件(touchstart+touchmove+touchend)
  19. 我对锤子ROM 功能的看法——视觉篇
  20. 全球100家杂志网站(转)

热门文章

  1. 11款Windows必装软件,每一款都非常好用
  2. 2019 网易校园招聘---[小易的字典]
  3. 怎么攻击天猫商家 java,天猫店铺被恶意攻击,大家看看是怎么做的。
  4. TypeScript报错解决-//@ts-ignore
  5. 机器学习-无监督学习-聚类:聚类方法(一)--- k-Means(k-均值)算法,k-Means++算法【使用最大期望值算法(EM算法)来求解】
  6. cad 打开硬件加速卡_CAD经常性卡顿?要怎么解决?
  7. 基于区块链技术的信息服务新架构探讨
  8. 服务器导出word文档中有乱码,使用Aspose.word DOC转PDF文件乱码问题-Doc文件
  9. Uni-app中几种常用的提示框
  10. Python-接口自动化流程(pytest)