ansible模板

您所在的位置:网站首页 jinjia2模板语法 ansible模板

ansible模板

2023-03-30 08:46| 来源: 网络整理| 查看: 265

ansible模板 1. jinja模板的使用及管理大项目1.1使用循环1.2条件判断1.3 条件语句1.4管理大项目1.5 配置并行 2 滚动更新

1. jinja模板的使用及管理大项目

Ansible将jinja2模板系统用于模板文件。Ansible还使用jinja2语法来引用playbook中的变量

示例:

使用template模块可以调用模板里的事实将事实的内容进行输出. [[email protected] ansible]# cat test.yml --- - hosts: node2 tasks: - name: test template: src: roles/file.j2 dest: /etc/hosts [[email protected] ~]# cat /etc/hosts 192.168.182.138 node2 [[email protected] ansible]# ansible-playbook test.yml PLAY [node2] ******************************************************************* TASK [Gathering Facts] ********************************************************* ok: [node2] TASK [test] ******************************************************************** changed: [node2] PLAY RECAP ********************************************************************* node2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 1.1使用循环 在test.yml这个playbook这个文件里面引用一个变量文件,然定义一个jinja文件这个jinja文件里面也使用哪个变量文件里面的变量,用template模块引用这个文件里面的变量。 [[email protected] ansible]# cat test.yml --- - hosts: node2 vars_files: - var/test.yml tasks: - name: test template: src: roles/test.j2 dest: /opt/all [[email protected] ansible]# cat var/test.yml info: - hello linux - hello python [[email protected] ansible]# cat roles/test.j2 {% for msg in info %} {{msg}} {% endfor %} 1.2条件判断 [[email protected] ansible]# cat roles/test.j2 {% for msg in info if msg == "hello python" %} {{msg}} {% endfor %} 若msg等于引用的一个值就将他打印到指定的地方,若不等于的话就除了不等于的内容外其他的都打印出来,不等于的表示方式有两种:!=;if not [[email protected] ansible]# cat roles/test.j2 {% for msg in info %} {% if msg == "hello python" %} count {{ loop.index }} of info is {{ msg }}. //意思是第几次时info循环到的变量是msg这个变量值 loop.index这个表示的是循环次数 {% endif %} {% endfor %} [[email protected] opt]# cat all count 2 of info is hello python. 不需要使用{{}}引用变量的场景: debug模块里面的var参数 playbook中用when判断条件时 模块文件中定义的时候{% 定义的时候 %} 1.3 条件语句 [[email protected] ansible]# cat roles/test.j2 {% if "" %} //当if后面什么也没有的时候就会默认为false就不会输出任何内容 条件语句 {% endif %} [[email protected] opt]# cat all [[email protected] ansible]# cat roles/test.j2 {% if " " %} //即使是if后面的引号里面是空格也默认有东西,就会输出内容 条件语句 {% endif %} [[email protected] opt]# cat all 条件语句 // 将YAML格式转换为json格式 [[email protected] ansible]# cat roles/test.j2 {% if " " %} {{ info | to_nice_json}} {% endif %} [[email protected] ansible]# cat var/test.yml info: - hello linux - hello python - hello CentOS [[email protected] opt]# cat all [ "hello linux", "hello python", "hello CentOS" ] 1.4管理大项目

列表

主机名或主机组用逗号隔开 - hosts: 'lab,data*,192.168.2.2' - hosts: lab,datacenter - hosts: labhost1.example.com,test2.example.com,192.168.2.2

管理多个清单

-i 可以指定清单文件里面的主机,便于项目的管理。 [[email protected] ansible]# ansible all -i inventory/test1 -m ping node2 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false, "ping": "pong" } [[email protected] ansible]# cat inventory/test1 node2 1.5 配置并行

Ansible所进行的最大同时连接数由Ansible配置文件中的forks参数控制,默认的forks为5,但是为了提高效率可以下ansible的配置文件中进行修改,vim /etc/ansible/ansible.cfg 搜索forks修改它的默认连接数

2 滚动更新

当Ansible运行play时,它会确保所有受管主机在启动任何主机进行下一个任务之前已完成每个任务。在所有受管主机完成所有任务后,将运行任何通知的处理程序,若我们更新服务时需要停止服务再启动则会导致服务全部停止,那我们就需要serial ,serial就是让play先到serial指定的那几台服务器上运行。这样即使play任务失败,也能及时发现并作出修改,保证服务的正常运行。 serial也可以指定百分比。百分比就是用于play中主机总数



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3