常见
| #定义任务的名称 | #调用模块,具体要做的事情 |
|---|---|
| - name: create new file | file: name=/tmp/playtest.txt state=touch |
| - name: create new dir | file: name=/tmp/back state=directory |
| - name: create new user | user: name=test02 system=yes shell=/sbin/nologin |
| user: name=test001 state=present | |
| - name: Create New Group | group: name=test001 state=present |
| - name: install package | yum: name=httpd |
| - name: start httpd | service: name=httpd state=started |
| - name: copy index.html | copy: src=/var/www/html/index.html dest=/var/www/html/index.html |
| - name: run df -h | shell: name=df -h |
| get ip_addr | shell: echo {{ ansible_default_ipv4[ “address” ] }} >> /root/newfile.txt |
task定义要执行该任务的远程用户
tasks:- name: run df -hremote_user: testshell: name=df -h
task定义使用sudo授权用户执行该任务
tasks:- name: run df -hsudo_user: testsudo: yesshell: name=df -h
task任务列表
每一个task必须有一个名称name,这样在运行playbook时,从其输出的任务执行信息中可以很清楚的辨别是属于哪一个task的,如果没有定义 name,action的值将会用作输出信息中标记特定的task。
Handlers与Notify
很多时候当我们某一个配置发生改变,我们需要重启服务,(比如httpd配置文件文件发生改变了)这时候就可以用到handlers和notify了;
(当发生改动时)notify actions会在playbook的每一个task结束时被触发,而且即使有多个不同task通知改动的发生,notify actions知会被触发一次;比如多个resources指出因为一个配置文件被改动,所以apache需要重启,但是重新启动的操作只会被执行一次。
[root@ansible ~]# cat httpd.yml#用于安装httpd并配置启动---- hosts: 192.168.1.31remote_user: roottasks:- name: install httpdyum: name=httpd state=installed- name: config httpdtemplate: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.confnotify:- restart httpd- name: start httpdservice: name=httpd state=startedhandlers:- name: restart httpdservice: name=httpd state=restarted#这里只要对httpd.conf配置文件作出了修改,修改后需要重启生效,在tasks中定义了restart httpd这个action,然后在handlers中引用上面tasks中定义的notify。
Playbook中变量的使用
环境说明:这里配置了两个组,一个apache组和一个nginx组
[root@ansible PlayBook]# cat /etc/ansible/hosts[apache]192.168.1.36192.168.1.33[nginx]192.168.1.3[1:2]
命令行指定变量
执行playbook时候通过参数-e传入变量,这样传入的变量在整个playbook中都可以被调用,属于全局变量
[root@ansible PlayBook]# cat variables.yml---- hosts: allremote_user: roottasks:- name: install pkgyum: name={{ pkg }}#执行playbook 指定pkg[root@ansible PlayBook]# ansible-playbook -e "pkg=httpd" variables.yml
hosts文件中定义变量
在/etc/ansible/hosts文件中定义变量,可以针对每个主机定义不同的变量,也可以定义一个组的变量,然后直接在playbook中直接调用。注意,组中定义的变量没有单个主机中的优先级高。
# 编辑hosts文件定义变量[root@ansible PlayBook]# vim /etc/ansible/hosts[apache]192.168.1.36 webdir=/opt/test #定义单个主机的变量192.168.1.33[apache:vars] #定义整个组的统一变量webdir=/web/test[nginx]192.168.1.3[1:2][nginx:vars]webdir=/opt/web# 编辑playbook文件[root@ansible PlayBook]# cat variables.yml---- hosts: allremote_user: roottasks:- name: create webdirfile: name={{ webdir }} state=directory #引用变量# 执行playbook[root@ansible PlayBook]# ansible-playbook variables.yml
playbook文件中定义变量#
编写playbook时,直接在里面定义变量,然后直接引用,可以定义多个变量;注意:如果在执行playbook时,又通过-e参数指定变量的值,那么会以-e参数指定的为准。
# 编辑playbook[root@ansible PlayBook]# cat variables.yml---- hosts: allremote_user: rootvars: #定义变量pkg: nginx #变量1dir: /tmp/test1 #变量2tasks:- name: install pkgyum: name={{ pkg }} state=installed #引用变量- name: create new dirfile: name={{ dir }} state=directory #引用变量# 执行playbook[root@ansible PlayBook]# ansible-playbook variables.yml# 如果执行时候又重新指定了变量的值,那么会已重新指定的为准[root@ansible PlayBook]# ansible-playbook -e "dir=/tmp/test2" variables.yml
调用setup模块获取变量#
setup模块默认是获取主机信息的,有时候在playbook中需要用到,所以可以直接调用。常用的参数参考
# 编辑playbook文件[root@ansible PlayBook]# vim variables.yml- hosts: [dbservers]remote_user: ansibleuserbecome: yesbecome_user: roottasks:- name: create new fileshell: echo {{ ansible_fqdn }} > /tmp/newfile.txt# 执行playbook[root@ansible PlayBook]# ansible-playbook variables.yml
[ansibleuser@node1 ~]$ ansible dbservers -m setup >setup.txt[ansibleuser@node1 ~]$ head -50 setup.txt172.18.15.182 | SUCCESS => {"ansible_facts": {"ansible_all_ipv4_addresses": ["172.18.15.182"],"ansible_all_ipv6_addresses": ["fe80::a00:27ff:fe2f:c9bc"],......当然,仔细观察setup模块的输出信息不难发现,所有的内建变量都是键值对的形式,有些多个键值对在一个花括号里面,这样的文本组织形式是基于JSON格式。在变量ansible_default_ipv4下还有多个子变量,我们可以称之为二级变量或者子键,分别对应着不同的值,这些子键的调用需要上级主键的配合使用才能完成变量调用,其格式为{{ main_var[ "sub_var" ] }}。task:- name: get ip_addrshell: echo {{ ansible_default_ipv4[ "address" ] }} >> /root/newfile.txt- name: get mac_addrshell: echo {{ ansible_default_ipv4[ "macaddress" ] }} >> /root/newfile.txt
独立的变量YAML文件中定义#
为了方便管理将所有的变量统一放在一个独立的变量YAML文件中,playbook文件直接引用文件调用变量即可。
# 定义存放变量的文件[root@ansible PlayBook]# cat var.ymlvar1: vsftpdvar2: httpd# 编写playbook[root@ansible PlayBook]# cat variables.yml---- hosts: allremote_user: rootvars_files: #引用变量文件- ./var.yml #指定变量文件的path(这里可以是绝对路径,也可以是相对路径)tasks:- name: install packageyum: name={{ var1 }} #引用变量- name: create filefile: name=/tmp/{{ var2 }}.log state=touch #引用变量# 执行playbook[root@ansible PlayBook]# ansible-playbook variables.yml
Playbook中标签的使用#
一个playbook文件中,执行时如果想执行某一个任务,那么可以给每个任务集进行打标签,这样在执行的时候可以通过-t选择指定标签执行,还可以通过--skip-tags选择除了某个标签外全部执行等。
Playbook中模板的使用#
template模板为我们提供了动态配置服务,使用jinja2语言,里面支持多种条件判断、循环、逻辑运算、比较操作等。其实说白了也就是一个文件,和之前配置文件使用copy一样,只是使用copy,不能根据服务器配置不一样进行不同动态的配置。这样就不利于管理。
说明:
1、多数情况下都将template文件放在和playbook文件同级的templates目录下(手动创建),这样playbook文件中可以直接引用,会自动去找这个文件。如果放在别的地方,也可以通过绝对路径去指定。
2、模板文件后缀名为.j2。
示例:通过template安装httpd
1)playbook文件编写
[root@ansible PlayBook]# cat testtmp.yml#模板示例---- hosts: allremote_user: rootvars:- listen_port: 88 #定义变量tasks:- name: Install Httpdyum: name=httpd state=installed- name: Config Httpdtemplate: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf #使用模板notify: Restart Httpd- name: Start Httpdservice: name=httpd state=startedhandlers:- name: Restart Httpdservice: name=httpd state=restarted
2)模板文件准备,httpd配置文件准备,这里配置文件端口使用了变量
[root@ansible PlayBook]# cat templates/httpd.conf.j2 |grep ^ListenListen {{ listen_port }}
3)查看目录结构
# 目录结构[root@ansible PlayBook]# tree ..├── templates│ └── httpd.conf.j2└── testtmp.yml1 directory, 2 files
template之when#
条件测试:如果需要根据变量、facts或此前任务的执行结果来做为某task执行与否的前提时要用到条件测试,通过when语句执行,在task中使用jinja2的语法格式、
when语句:
在task后添加when子句即可使用条件测试;when语句支持jinja2表达式语法。
类似这样:
tasks:- command: /bin/falseregister: resultignore_errors: True- command: /bin/somethingwhen: result|failed- command: /bin/something_elsewhen: result|success- command: /bin/still/something_elsewhen: result|skipped
示例:通过when语句完善上面的httpd配置
1)准备两个配置文件,一个centos6系统httpd配置文件,一个centos7系统httpd配置文件。
[root@ansible PlayBook]# tree templates/templates/├── httpd6.conf.j2 #6系统2.2.15版本httpd配置文件└── httpd7.conf.j2 #7系统2.4.6版本httpd配置文件0 directories, 2 files
2)修改playbook文件,通过setup模块获取系统版本去判断。setup常用模块
[root@ansible PlayBook]# cat testtmp.yml#when示例---- hosts: allremote_user: rootvars:- listen_port: 88tasks:- name: Install Httpdyum: name=httpd state=installed- name: Config System6 Httpdtemplate: src=httpd6.conf.j2 dest=/etc/httpd/conf/httpd.confwhen: ansible_distribution_major_version == "6" #判断系统版本,为6便执行上面的template配置6的配置文件notify: Restart Httpd- name: Config System7 Httpdtemplate: src=httpd7.conf.j2 dest=/etc/httpd/conf/httpd.confwhen: ansible_distribution_major_version == "7" #判断系统版本,为7便执行上面的template配置7的配置文件notify: Restart Httpd- name: Start Httpdservice: name=httpd state=startedhandlers:- name: Restart Httpdservice: name=httpd state=restarted
template之with_items#
with_items迭代,当有需要重复性执行的任务时,可以使用迭代机制。
对迭代项的引用,固定变量名为“item”,要在task中使用with_items给定要迭代的元素列表。
列表格式:
字符串
字典
示例1:通过with_items安装多个不同软件
编写playbook
[root@ansible PlayBook]# cat testwith.yml# 示例with_items---- hosts: allremote_user: roottasks:- name: Install Packageyum: name={{ item }} state=installed #引用item获取值with_items: #定义with_items- httpd- vsftpd- nginx
上面tasks写法等同于:
---- hosts: allremote_user: roottasks:- name: Install Httpdyum: name=httpd state=installed- name: Install Vsftpdyum: name=vsftpd state=installed- name: Install Nginxyum: name=nginx state=installed
示例2:通过嵌套子变量创建用户并加入不同的组
1)编写playbook
[root@ansible PlayBook]# cat testwith01.yml# 示例with_items嵌套子变量---- hosts: allremote_user: roottasks:- name: Create New Groupgroup: name={{ item }} state=presentwith_items:- group1- group2- group3- name: Create New Useruser: name={{ item.name }} group={{ item.group }} state=presentwith_items:- { name: 'user1', group: 'group1' }- { name: 'user2', group: 'group2' }- { name: 'user3', group: 'group3' }
template之for if#
通过使用for,if可以更加灵活的生成配置文件等需求,还可以在里面根据各种条件进行判断,然后生成不同的配置文件、或者服务器配置相关等。
示例1
1)编写playbook
[root@ansible PlayBook]# cat testfor01.yml# template for 示例---- hosts: allremote_user: rootvars:nginx_vhost_port:- 81- 82- 83tasks:- name: Templage Nginx Configtemplate: src=nginx.conf.j2 dest=/tmp/nginx_test.conf
2)模板文件编写
# 循环playbook文件中定义的变量,依次赋值给port[root@ansible PlayBook]# cat templates/nginx.conf.j2{% for port in nginx_vhost_port %}server{listen: {{ port }};server_name: localhost;}{% endfor %}
{% for port in nginx_vhost_port %}
server{
listen: {{ port }};
server_name: localhost;
}
{% endfor %}
3)执行playbook并查看生成结果
[root@ansible PlayBook]# ansible-playbook testfor01.yml# 去到一个节点看下生成的结果发现自动生成了三个虚拟主机[root@linux ~]# cat /tmp/nginx_test.confserver{listen: 81;server_name: localhost;}server{listen: 82;server_name: localhost;}server{listen: 83;server_name: localhost;}
示例2
1)编写playbook
[root@ansible PlayBook]# cat testfor02.yml# template for 示例---- hosts: allremote_user: rootvars:nginx_vhosts:- web1:listen: 8081server_name: "web1.example.com"root: "/var/www/nginx/web1"- web2:listen: 8082server_name: "web2.example.com"root: "/var/www/nginx/web2"- web3:listen: 8083server_name: "web3.example.com"root: "/var/www/nginx/web3"tasks:- name: Templage Nginx Configtemplate: src=nginx.conf.j2 dest=/tmp/nginx_vhost.conf
2)模板文件编写
[root@ansible PlayBook]# cat templates/nginx.conf.j2{% for vhost in nginx_vhosts %}server{listen: {{ vhost.listen }};server_name: {{ vhost.server_name }};root: {{ vhost.root }};}{% endfor %}
3)执行playbook并查看生成结果
[root@ansible PlayBook]# ansible-playbook testfor02.yml# 去到一个节点看下生成的结果发现自动生成了三个虚拟主机[root@linux ~]# cat /tmp/nginx_vhost.confserver{listen: 8081;server_name: web1.example.com;root: /var/www/nginx/web1;}server{listen: 8082;server_name: web2.example.com;root: /var/www/nginx/web2;}server{listen: 8083;server_name: web3.example.com;root: /var/www/nginx/web3;}
示例3
在for循环中再嵌套if判断,让生成的配置文件更加灵活
1)编写playbook
[root@ansible PlayBook]# cat testfor03.yml# template for 示例---- hosts: allremote_user: rootvars:nginx_vhosts:- web1:listen: 8081root: "/var/www/nginx/web1"- web2:server_name: "web2.example.com"root: "/var/www/nginx/web2"- web3:listen: 8083server_name: "web3.example.com"root: "/var/www/nginx/web3"tasks:- name: Templage Nginx Configtemplate: src=nginx.conf.j2 dest=/tmp/nginx_vhost.conf
2)模板文件编写
# 说明:这里添加了判断,如果listen没有定义的话,默认端口使用8888,如果server_name有定义,那么生成的配置文件中才有这一项。[root@ansible PlayBook]# cat templates/nginx.conf.j2{% for vhost in nginx_vhosts %}server{{% if vhost.listen is defined %}listen: {{ vhost.listen }};{% else %}listen: 8888;{% endif %}{% if vhost.server_name is defined %}server_name: {{ vhost.server_name }};{% endif %}root: {{ vhost.root }};}{% endfor %}
3)执行playbook并查看生成结果
[root@ansible PlayBook]# ansible-playbook testfor03.yml# 去到一个节点看下生成的结果发现自动生成了三个虚拟主机[root@linux ~]# cat /tmp/nginx_vhost.confserver{listen: 8081;root: /var/www/nginx/web1;}server{listen: 8888;server_name: web2.example.com;root: /var/www/nginx/web2;}server{listen: 8083;server_name: web3.example.com;root: /var/www/nginx/web3;}
