templates 模板
[root@ansible ~]
[root@ansible opt]
[root@ansible opt]
Enter passphrase for /root/.ssh/id_rsa:
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
[root@ansible opt]
'//将webserver端的apache配置文件远程复制至本地'
httpd.conf 100% 11KB 10.1MB/s 00:00
[root@ansible opt]
'//修改配置文件,添加模板(变量)'
43 Listen {{http_port}} '//监听'
98 ServerName {{server_name}} '//域名'
99 MaxClients {{access_num}} '//访问量'
[root@ansible opt]
'//修改完配置文件,复制一份,j2格式表示为模板'
[root@ansible opt]
'//后续需要对参数进行赋值'
25 [webserver] '//设定变量'
26 192.168.126.12 http_port=192.168.126.12:80 server_name="www.xcf.com:80" access_num=300
[root@ansible opt]
- hosts: webserver '//指定webserver'
remote_user: root '//指定远程主机使用root用户'
vars: '//变量可以定义在vars中,也可以在hosts主机清单中'
- package: httpd
- server: httpd
tasks:
- name: check latest '//检查最新版本'
yum: name={{package}} state=latest '//安装最新版本服务'
- name: configure apache '//配置apache'
template: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
'//源文件在ansible端,生成至对方服务器端'
notify: '//修改配置文件后需重启服务'
- restart httpd
- name: statrt httpd '//最后一步需要开启服务项'
service: name={{server}} enabled=true state=started '//开启自启'
handlers: '//notify通知handlers执行'
- name: restart httpd '//重启服务使配置生效'
service: name={{server}} state=restarted
[root@ansible opt]
'//检查语法,正确'
playbook: apache.yml
[root@ansible opt]
'//执行'
PLAY [webserver] ****************************************************************
TASK [Gathering Facts] **********************************************************
ok: [192.168.126.12]
TASK [check latest] *************************************************************
ok: [192.168.126.12]
TASK [configure apache] *********************************************************
changed: [192.168.126.12]
TASK [statrt httpd] *************************************************************
ok: [192.168.126.12]
RUNNING HANDLER [restart httpd] *************************************************
changed: [192.168.126.12]
PLAY RECAP **********************************************************************
192.168.126.12 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
'//切换至webserver主机,查看配置文件是否更改'
[root@webserver ~]
[root@webserver ~]
[root@webserver ~]
'//可以看到配置好的模板已自动生效'
'//所有服务器皆可按照模板来,非常强大及便捷!'
tags 模块
1.在一个剧本中,一般会定义多个task,而tags可以选择执行哪一个
[root@ansible opt]
- hosts: webserver
remote_user: root
tasks:
- name: Copy hosts file '//复制操作'
copy: src=/etc/hosts dest=/opt/hosts
tags: '//标记,打标签,以上为一个整体'
- only
- name: touch file '//另一个操作'
file: path=/opt/hosts01 state=touch
'//以上操作表示为只执行完第一个就结束而不执行第二个'
'//若不设置标签则会执行到底
'
[root@ansible opt]
playbook: file.yml
[root@ansible opt]
PLAY [webserver] ****************************************************************
TASK [Gathering Facts] **********************************************************
ok: [192.168.126.12]
TASK [Copy hosts file] **********************************************************
changed: [192.168.126.12]
PLAY RECAP **********************************************************************
192.168.126.12 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansible opt]
192.168.126.12 | CHANGED | rc=0 >>
hosts '//结果反馈,只有hosts,没有hosts01,执行成功'
rh
--always--
[root@ansible opt]
- hosts: webserver
remote_user: root
tasks:
- name: Copy hosts file
copy: src=/etc/hosts dest=/opt/hosts
tags:
- only
- name: touch file
file: path=/opt/hosts01 state=touch
tags: '//添加此项,always表示在任何条件前提下始终执行'
- always
[root@ansible opt]
[WARNING]: Consider using the file module with state=absent rather than running
'rm'. If you need to use command because file 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.
192.168.126.12 | CHANGED | rc=0 >>
'//删除旧文件,方便后续测试'
[root@ansible opt]
PLAY [webserver] ****************************************************************
TASK [Gathering Facts] **********************************************************
ok: [192.168.126.12]
TASK [Copy hosts file] **********************************************************
changed: [192.168.126.12]
TASK [touch file] ***************************************************************
changed: [192.168.126.12]
PLAY RECAP **********************************************************************
192.168.126.12 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansible opt]
192.168.126.12 | CHANGED | rc=0 >>
总用量 4
-rw-r--r-- 1 root root 158 4月 8 10:00 hosts
-rw-r--r-- 1 root root 0 4月 8 10:00 hosts01 '//有了,always生效'
drwxr-xr-x. 2 root root 6 3月 26 2015 rh