用自己电脑做网站服务器,唐县做网站,广东阳江发布最新消息,营销案例100例小故事及感悟目录 实战演练六#xff1a;tags 模块
实战演练七#xff1a;Templates 模块 实战演练六#xff1a;tags 模块 可以在一个playbook中为某个或某些任务定义“标签”#xff0c;在执行此playbook时通过ansible-playbook命令使用--tags选项能实现仅运行指定的tasks。 playboo…目录 实战演练六tags 模块
实战演练七Templates 模块 实战演练六tags 模块 可以在一个playbook中为某个或某些任务定义“标签”在执行此playbook时通过ansible-playbook命令使用--tags选项能实现仅运行指定的tasks。 playbook还提供了一个特殊的tags为always。作用就是当使用always作为tags的task时无论执行哪一个tags时定义有always的tags都会执行。 vim play5.yaml
- name: five playremote_user: roothosts: dbserversgather_facts: truetasks:- name: copy filecopy: src/etc/hosts dest/opt/tags:- test- name: touch filefile: path/opt/myhosts statetouchtags:- onlyansible-playbook play5.yaml --tagstest #指定标签只运行该标签下的任务
被控制节点查看opt目录下是否有hosts文件
vim play5.yaml
- name: five playremote_user: roothosts: dbserversgather_facts: truetasks:- name: copy filecopy: src/etc/hosts dest/opt/tags:- test- name: touch filefile: path/opt/myhosts statetouchtags:- only- alwaysansible-playbook play5.yaml --tagstest #当使用always作为tags的task时无论执行哪一个tags时定义有always的tags都会执行。
被控制节点查看
实战演练七Templates 模块 Jinja是基于Python的模板引擎。Template类是Jinja的一个重要组件可以看作是一个编译过的模板文件用来产生目标文本传递Python的变量给模板去替换模板中的标记。 cp /opt/httpd.conf ./httpd.conf.j2 先准备一个以 .j2 为后缀的 template 模板文件设置引用的变量vim httpd.conf.j2
Listen {{server_ip}}:{{http_port}} #42行
ServerName {{host_name}}:{{http_port}} #95行vim /etc/ansible/hosts
[webservers]
192.168.9.113 server_ip192.168.9.113 http_port8080 host_namewww.xy102.com
192.168.9.111 server_ip192.168.9.111 http_port8081 host_namewww.xy103.comvim play1.yaml
- name: first playgather_facts: falsehosts: webserversremote_user: roottasks:- name: disable firewalldservice: namefirewalld statestopped enabledno- name: disable selinuxcommand: setenforce 0ignore_errors: true- name: mount cdrommount: src/dev/sr0 path/mnt fstypeiso9660 statemounted- name: install httpdyum: namehttpd statelatest- name: copy config template filetemplate: srchttpd.conf.j2 dest/etc/httpd/conf/httpd.confnotify: reload httpd- name: start httpdservice: namehttpd statestarted enabledyeshandlers:- name: reload httpdservice: namehttpd statereloadedansible-playbook play1.yaml两个被控制节点查看42、95行是否修改成功