一、playbook 就是把全部的配置写到一个配置文件里,直接执行这个配置文件就能够了;html
首先定义个一个配置文件 /etc/ansible/test.ymlnginx
[root@localhost_001 ~]# vim /etc/ansible/test.yml cat /etc/ansible/test.yml --- #注释,表示开头了; - hosts: webserver #- host:这表示内容就开始了; 针对哪一个机器; remote_user: root #针对那些用户; tasks: #任务; - name: test_playbook shell: touch /tmp/fenye.txt #用到的是shell模块;
2:执行: ansible-playbook test.yml (ansible-playbook后跟文件名就能够了)web
[root@localhost_001 ~]# cd /etc/ansible/ [root@localhost_001 ansible]# ansible-playbook test.yml PLAY [webserver] ******************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************** ok: [localhost_03] changed: [localhost_03] TASK [test_playbook] **************************************************************************** PLAY RECAP ************************************************************************************************************************** localhost_03 : ok=2 changed=1 unreachable=0 failed=0
注释:第一行须要有三个杠,hosts参数指定了对哪些主机进行参做,若是是多台机器能够用逗号做为分隔,也可使用主机组,在/etc/ansible/hosts定义;
user参数指定了使用什么用户登陆远程主机操做;
tasks指定了一个任务,其下面的name参数一样是对任务的描述,在执行过程当中会打印出来,shell是ansible模块名字shell
2:playbook的变量:vim
下面在001(129)写一个建立用户的playbook。bash
[root@localhost_001 ansible]# vim create_user.yml --- - name: create_user #name是对playbook作一个描述,后面执行中会打印; hosts: localhost_002 #针对哪一个主机或者组; user: root #以那个用户的身份来执行; gather_facts: false #是是否移动setup模块收集系统信息(IP地址主机信息),false表示不收集; vars: - user: "test" #此处是用vars后引出一个变量,用于在下面引用; tasks: - name: create user user: name="{{ user }}" 调用了user模块,name是其里面的一个参数;user表示上面的变量; 说明:name参数对该playbook实现的功能作一个概述,后面执行过程当中,会打印 name变量的值 ,能够省略;gather_facts参数指定了在如下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到;vars参数,指定了变量,这里指字一个user变量,其值为test ,须要注意的是,变量值必定要用引号引住;user提定了调用user模块,name是user模块里的一个参数,而增长的用户名字调用了上面user变量的值。
在001(129)上执行后,以下内容:服务器
[root@localhost_001 ansible]# ansible-playbook create_user.yml PLAY [create_user] ****************************************************************************************************************** TASK [create user] ****************************************************************************************************************** changed: [localhost_002] PLAY RECAP ************************************************************************************************************************** localhost_002 : ok=1 changed=1 unreachable=0 failed=0 在002(130)上查看该用户; [root@localhost_002 ~]# id test uid=1003(test) gid=1003(test) 组=1003(test)
而若是执行时,若是用户存在的话,则执行以下;不变动: changed=0 表示不变动ide
[root@localhost_001 ansible]# ansible-playbook create_user.yml PLAY [create_user] ****************************************************************************************************************** TASK [create user] ****************************************************************************************************************** ok: [localhost_002] PLAY RECAP ************************************************************************************************************************** localhost_002 : ok=1 changed=0 unreachable=0 failed=0
删除用户;user: name="{{ user }}" state=absent remove=yesui
[root@localhost_001 ansible]# cat del_user.yml --- - name: create_user hosts: localhost_002 user: root vars: - user: "test" gather_facts: false tasks: - name: create_user user: name="{{ user }}" state=absent remove=yes
3:pllaybook循环this
vi /etc/ansible/while.yml //加入以下内容 --- - hosts: testhost user: root tasks: - name: change mode for files file: path=/tmp/{{ item }} mode=600 with_items: - 1.txt - 2.txt - 3.txt 说明: with_items为循环的对象 执行 ansible-playbook while.yml
4:playbook的条件判断
vi /etc/ansible/when.yml //加入以下内容 --- - hosts: testhost user: root gather_facts: True tasks: - name: use when shell: touch /tmp/when.txt when: ansible_ens33.ipv4.address == "172.7.15.114“ 说明:ansible aming-02 -m setup 能够查看到全部的facter信息
5:Ansible playbook中的handlers
执行task以后,服务器发生变化以后要执行的一些操做,好比咱们修改了配置文件后,须要重启一下服务 vi /etc/ansible/handlers.yml//加入以下内容 --- - name: handlers test hosts: aming-02 user: root tasks: - name: copy file copy: src=/etc/passwd dest=/tmp/aaa.txt notify: test handlers handlers: - name: test handlers shell: echo "111111" >> /tmp/aaa.txt 说明,只有copy模块真正执行后,才会去调用下面的handlers相关的操做。也就是说若是1.txt和2.txt内容是同样的,并不会去执行handlers里面的shell相关命令。 这种比较适合配置文件发生更改后,重启服务的操做。
6:安装nginx:使用源码安装,定制选项,使用ansible来作:用来扩容后,把已经存在模板,配置文件拷贝过去;
首先在一台机器上安装好nginx,而后把nginx打包,而后分发到各个机器;
1:在/etc/ansible/目录下再建立一个目录;
[root@localhost_001 ~]# cd /etc/ansible/ [root@localhost_001 ansible]# mkdir nginx_install [root@localhost_001 ansible]# cd nginx_install/ [root@localhost_001 nginx_install]# mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars} [root@localhost_001 nginx_install]# ls roles [root@localhost_001 nginx_install]# ls roles/ common install
注释: mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}这条命令表示在 roles下建立 common和install目录,而后在这两个目录下分别建立handlers files meta tasks templates vars等目录;
roles的目录状况:有common和install两个角色;
common:表示作一些准备操做;
install:表示安装nginx的操做;
这两个角色下的目录状况以下;
handlers:表示当发生改变时要执行的操做,一般用在配置文件发生改变,重启服务;
files:表示安装时用到的一些文件;
meta:为角色信息,说明角色依赖信息;
tasks:核心依赖配置文件;
template:配置文件,启动脚本等文件;能够针对系统版原本作不一样的变量在里面;
vars:表示定义的变量;
(1):首先在一台机器上编译好nginx,并指定nginx的目录, 启动脚本 及配置文件;
[root@localhost_001 install]# ls /usr/local/nginx/ client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp [root@localhost_001 install]# ls /etc/init.d/nginx /etc/init.d/nginx [root@localhost_001 install]# ls /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf
(2):把nginx打包,并放到在/etc/ansible/nginx_install/roles/install/files
[root@localhost_001 local]# tar zcvf nginx.tar.gz --exclude "nginx.conf" --exclude "vhosts" nginx^C [root@localhost_001 local]# mv nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/ [root@localhost_001 local]# ls !$ ls /etc/ansible/nginx_install/roles/install/files/ nginx.tar.gz
(3):把nginx启动脚本和配置文件放到/etc/ansible/nginx_install/roles/install/template
[root@localhost_001 local]# cp nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/ [root@localhost_001 local]# cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/
(4):定义common下的tasks,nginx须要依赖的一些包;
[root@localhost_001 local]# cd /etc/ansible/nginx_install/roles [root@localhost_001 roles]# vim common/tasks/main.yml - name: Install initializtion require software yum: name="pcre-devel,zlib-devel" state=installed
(5):定义common下的vars变量;能够自定义变量,也能够指定某台机器用某个用户;
[root@localhost_001 roles]# vim common/vars/main.yml nginx_user: www nginx_port: 80 nginx_basedir: /usr/local/nginx
(7):把配置文件拷贝到目标机器上;
[root@localhost_001 roles]# vim install/tasks/copy.yml - name: Copy Nginx Software copy: src=nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root - name: Uncompression Nginx Software shell: tar zxf /tmp/nginx.tar.gz -C /usr/local/ - name: Copy Nginx Start Script template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755 - name: Copy Nginx Config template: src=nginx.conf dest={{ nginx_basedir }}/conf/ owner=root group=root mode=0644
(8):接下来会创建启动服务,删除配置文件;
[root@localhost_001 roles]# vim install/tasks/install.yml cat install/tasks/install.yml - name: Create Nginx User user: name={{ nginx_user }} state=present createhome=no shell=/sbin/nologin - name: Start Nginx Service shell: /etc/init.d/nginx start - name: Add Boot Start Nginx Service shell: chkconfig --level 345 nginx on - name: Delete Nginx compression files shell: rm -rf /tmp/nginx.tar.gz
(9):在install目录下建立main.yml,把copy和install调用;
[root@localhost_001 roles]# vim install/tasks/main.yml cat install/tasks/main.yml - include: copy.yml - include: install.yml
至此两个common和install定义完成了;
(10):接下来定义一个接口配置文件;
[root@localhost_001 nginx_install]# vim install.yml cat install.yml - hosts: testhost remote_user: root gather_facts: True roles: - common - install
而后执行:ansible-playbook /etc/ansible/nginx_install/install.yml
[root@localhost_001 ansible]# ansible-playbook /etc/ansible/nginx_install/install.yml PLAY [testhost] ********************************************************************************************************************* TASK [Gathering Facts] ************************************************************************************************************** ok: [localhost_002] ok: [127.0.0.1] TASK [common : Install initializtion require software] ****************************************************************************** ok: [127.0.0.1] ok: [localhost_002] TASK [install : Copy Nginx Software] ************************************************************************************************ changed: [localhost_002] changed: [127.0.0.1] TASK [install : Uncompression Nginx Software] *************************************************************************************** [WARNING]: Consider using the unarchive module rather than running tar. If you need to use command because unarchive 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. changed: [127.0.0.1] changed: [localhost_002] TASK [install : Copy Nginx Start Script] ******************************************************************************************** changed: [localhost_002] ok: [127.0.0.1] TASK [install : Copy Nginx Config] ************************************************************************************************** ok: [127.0.0.1] changed: [localhost_002] TASK [install : Create Nginx User] ************************************************************************************************** changed: [localhost_002] changed: [127.0.0.1] TASK [install : Start Nginx Service] ************************************************************************************************ changed: [127.0.0.1] changed: [localhost_002] TASK [install : Add Boot Start Nginx Service] *************************************************************************************** changed: [localhost_002] changed: [127.0.0.1] TASK [install : Delete Nginx compression files] ************************************************************************************* [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. changed: [127.0.0.1] changed: [localhost_002] PLAY RECAP ************************************************************************************************************************** 127.0.0.1 : ok=10 changed=6 unreachable=0 failed=0 localhost_002 : ok=10 changed=8 unreachable=0 failed=0
此时在002这台机器查询nginx已经启动;
[root@localhost_002 ~]# ls /usr/local/nginx/ client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp [root@localhost_002 ~]# ps aux |grep nginx root 2640 0.0 0.1 46392 1264 ? Ss 16:06 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 2641 0.0 0.3 48132 3876 ? S 16:06 0:00 nginx: worker process nobody 2642 0.0 0.3 48132 3876 ? S 16:06 0:00 nginx: worker process root 2772 0.0 0.0 112720 972 pts/0 R+ 16:09 0:00 grep --color=auto nginx