Templates 模块 Jinja模版架构,通过这个模版可以实现,往配置文件(模版文件)传参(python转义)把占位符参数传到配置文件中去 Jinja就是生产一个目标文本文件,然后传递变量需要配置文件(web开发) |
创建文件 123 345 678 将其复制到12主机上创建的test1 test2 test3
- hosts: 192.168.233.12remote_user: roottasks:- name: create dirfile:path: "/opt/{{item}}"state: directorywith_items:- test1- test2- test3when: inventory_hostname == '192.168.233.12'- name: copycopy:src: "/opt/{{item.src}}"dest: "/opt/{{item.dest}}"with_items:- {src: "123", dest: "test1" }- {src: "456", dest: "test2" }- {src: "789", dest: "test3" }when是一个比较常见的应用场景,实现满足条件即执行,不满足条件即跳过的任务
when是满足条件即执行,不满足不执行
templates模块
templates模块
jinja模版架构,通过模版可以实现模版文件传参(python转义)把占位符参数传到配置文件中区
生成一个目标文本文件,传递变量到需要配置文件当中(web开发)[root@docker1 opt]# cd /etc/httpd/conf
[root@docker1 conf]# cp httpd.conf httpd.conf.j2
[root@docker1 opt]# vim jinia.httpd.yaml
[root@docker1 opt]# ansible-playbook jinia.httpd.yaml- hosts: allremote_user: rootvars:- package: httpd- service: httpdtasks:- name: install httpdyum: name={{package}}- name: install configure filetemplate: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
#使用template的模版notify:- restart httpd- name: create root_dirfile:path: /etc/httpd/htdocsstate: directory- name: start httpdservice: name={{service}} enabled=true state=startedhandlers:- name: restart httpdservice: name={{service}} state=restartednginx
[root@docker1 opt]# cd /etc/nginx/
[root@docker1 nginx]# vim nginx.conf.j2server {listen "{{port}}";server_name "{{server_name}}";root "{{root_dir}}";
[root@docker1 nginx]# vim /etc/ansible/hosts
[webservers]
192.168.233.12 port=8080 server_name=www.guoqi.com:80 root_dir=/opt/nginx/html
[root@docker1 opt]# cp /etc/nginx/nginx.conf.j2 /opt/- hosts: allremote_user: rootvars:- package: nginx- service: nginxtasks:- name: install nginxyum: name={{package}}- name: install configure filetemplate: src=/opt/nginx.conf.j2 dest=/etc/nginx/nginx.confnotify:- restart nginx- name: create root_dirfile:path: /etc/nginx/htmlstate: directory- name: start nginxservice: name={{service}} enabled=true state=startedhandlers:- name: restart nginxservice: name={{service}} state=restarted
[root@docker1 opt]# ansible-playbook jinia.nginx.yaml
[root@docker2 opt]# vim /etc/nginx/nginx.conf
[root@docker2 opt]# netstat -antp | grep 8080
tags标签模块
tags模块
标签模块,可以在playbook当中为任务设定标签(tags),在运行playbook时可以通过指定任务标签,来实现只允许设定的标签任务
格式:
- name:tags:debug:
--tags debug
--start-at-task='wdf'
#表示只运行debug其他都不运行任务标签的种类:
always:不管你是否指定了运行标签,任务都会执行
never:即使你运行了指定标签,该任务也不会执行
debug:调试任务
setup:收集主机信息
自定义标签
per_tasks:指定标签之前的任务
post_tasks:运行指定标签之后的任务- hosts: allremote_user: roottasks:- name: tag debugdebug:msg: "this is test1"tags:- debug- name: tag setupsetup:tags:- setup- name: tag alwaysdebug:msg: "run"tags:- always- name: tag neverdebug:msg: "never run"tags:- never[root@docker1 opt]# ansible-playbook tags.yaml --tags="never"
[root@docker1 opt]# ansible-playbook tags.yaml --tags="setup"
#只运行了setup和always[root@docker1 opt]# ansible-playbook tags.yaml --tags="ll"
[root@docker1 opt]# ansible-playbook tags.yaml --tags="yy"
#自定义标签,可以对标签自定义,指定运行标签的内容,需要运行哪一个任务指定任务的标签就行练习
#在目标主机上touch guoqi.txt 标签为always,在目标主机复制文件/opt/wdf.txt,标签自定义,第一次运行playbook 不知道查看文件生成情况,指定标签为never,查看文件生成情况
- hosts: 192.168.233.12remote_user: roottasks:- name: touchfile:path: /opt/guoqi.txtstate: touchtags:- always- name: copy filecopy:src: /opt/wdf.txtdest: /opt/wdf.txttags:- never[root@docker1 opt]# ansible-playbook tags.l.yaml
#never是不运行的
[root@docker1 opt]# ansible-playbook tags.l.yaml --tags="never"
#指定运行never
roles模块
Roles模块
角色
Ansible层次化,结构化的组织playbook使用了rolse(角色)
可以根据层次的结构,自动装载变量文件,tasks以及handlers等等
Rolse:分别把变量,文件,任务,模块,以及处理器,放在单独的目录当中,使用rolse模块来一件调用这些文件
Roles:的结构图
----web----总目录,里面有角色
子目录
Files用来存放copy和script模块调用文件
Templates 存放j2的模版文件
Tasks包含任务的目录
----main.yml 角色运行的任务
Handlers:包含处理器的目录
------main.yml
Vars 存放变量的目录
----- main.yml 只能叫main.yaml/yml
Defaults 包含默认变量的目录
-----main.ymlMeta 包含元信息的目录
----main.yml
Site.yml用来调用所有的配置文件需要三个服务
httpd mysql php
[root@docker1 opt]# cd /etc/ansible/roles/
[root@docker1 roles]# mkdir httpd mysql php
[root@docker1 httpd]# mkdir files templates tasks handlers vars defaults meta
[root@docker1 httpd]# touch {defaults,vars,tasks,meta,handlers}/main.yml
[root@docker1 mysql]# mkdir files templates tasks handlers vars defaults meta
[root@docker1 mysql]# touch {defaults,vars,tasks,meta,handlers}/main.yml
[root@docker1 php]# mkdir files templates tasks handlers vars defaults meta
[root@docker1 php]# touch {defaults,vars,tasks,meta,handlers}/main.yml
[root@docker1 roles]# vim /etc/ansible/roles/httpd/tasks/main.yml- name: install httpdyum: name={{pkg}}
- name: start httpdservice: enabled=true name={{svc}} state=started[root@docker1 roles]# vim /etc/ansible/roles/httpd/vars/main.ymlpkg: httpd
svc: httpd[root@docker1 roles]# vim /etc/ansible/roles/mysql/tasks/main.yml- name: install mysqlyum: name={{pkg}}
- name: start mysqlservice: enabled=true name={{svc}} state=started[root@docker1 roles]# vim /etc/ansible/roles/mysql/vars/main.ymlpkg:- mariadb- mariadb-server
svc: mariadb[root@docker1 roles]# vim /etc/ansible/roles/php/tasks/main.yml- name: install phpyum: name={{pkg}}
- name: start php-fpmservice: enabled=true name={{svc}} state=started[root@docker1 roles]# vim /etc/ansible/roles/php/vars/main.yml
pkg:- php- php-fpm
svc: php-fpm[root@docker1 roles]# cd /etc/ansible/roles/
[root@docker1 roles]# vim site.yml- hosts: 192.168.233.12remote_user: rootroles:- httpd- mysql- php[root@docker1 roles]# ansible-playbook site.yml