学习linux的第七十四天

会有一股神秘感。 提交于 2020-01-27 02:17:38

playbook的条件判断

查看到setup收集到的所有的facter信息:
ansible testhost -m setup
编辑条件:
[root@KXLZQ ~]# vim /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 == “192.168.70.129”
注:
when: ansible_ens33是一个数组存储着网卡相关信息,ipv4属于该数组的子元素,但是ipv4也是一个数组,而address则是ipv4数组的子元素。我们需要使用address 来作为判断条件。所以要访问address就需要使用这样的格式:
when: ansible_ens33.ipv4.address,address表示的是键,而"192.168.70.129"则是值,when为判断语句相当于if,所以其判断条件为:该键的值为"192.168.70.129"时就执行shell模块里定义的语句。
执行:
[root@KXLZQ ~]# ansible-playbook /etc/ansible/when.yml
PLAY [testhost]


TASK [Gathering Facts]


ok: [192.168.70.129]
TASK [use when]


[WARNING]: Consider using file module with state=touch rather than running touch
changed: [192.168.70.129]
PLAY RECAP



192.168.70.129 : ok=2 changed=1 unreachable=0 failed=0
查看:
[root@XXQ ~]# ll /tmp/when.txt
-rw-r–r--. 1 root root 0 12月 30 11:21 /tmp/when.txt

playbook中的handlers

使用环境:当执行了tasks里面的内容之后,服务器发生了变化,这时可能需要执行一些相关的操作;相当于编程中的回调函数;例如:修改了某个服务的配置文件后,则需要重启一下服务,当任务执行成功,handlers执行完成重启;否则不执行;类似于shell脚本中的&&符号;
创建:
[root@KXLZQ ~]# vim /etc/ansible/handlers.yml
name: handlers test
hosts: testhost
user: root
tasks:
- name: copy file
copy: src=/etc/passwd dest=/tmp/test_passwd.txt
notify: test handlers
handlers:
- name: test handlers
shell: echo “1234567890” >> /tmp/test_passwd.txt
注:
只有copy模块执行成功后,才会去调用下面的handlers里定义的内容。也就是说如果/etc/passw和/tmp/test_passwd.txt内容是一样的话,就不会去执行handlers里面的shell相关命令,因为copy没有被执行。 这种比较适合配置文件发生更改后,重启服务的操作。
notify用于指定handlers的name参数的值,因为handlers可以定义多个,所以需要使用notify来进行指定调用哪一个
执行:
[root@KXLZQ ~]# ansible-playbook /etc/ansible/handlers.yml
PLAY [handlers test]


TASK [Gathering Facts]


ok: [192.168.70.129]
TASK [copy file]


changed: [192.168.70.129]
RUNNING HANDLER [test handlers]


changed: [192.168.70.129]
PLAY RECAP



192.168.70.129 : ok=3 changed=2 unreachable=0 failed=0
查看:
[root@XXQ ~]# tail -n1 /tmp/test_passwd.txt
1234567890

playbook安装nginx

思路:先在一台机器上编译安装好nginx、打包,然后再用ansible 分发到远程机器上。
在ansible配置目录下,创建一个nginx_install目录;
[root@KXLZQ ~]# cd /etc/ansible
[root@KXLZQ ansible]# mkdir nginx_install
[root@KXLZQ ansible]# cd nginx_install
[root@KXLZQ nginx_install]# mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}
[root@KXLZQ nginx_install]# ls roles/
common install
[root@KXLZQ nginx_install]# ls roles/install/
files handlers meta tasks templates vars
[root@KXLZQ nginx_install]# ls roles/common/
files handlers meta tasks templates vars
说明:
roles目录下有两个角色,common为一些准备操作,install为安装nginx的操作。每个角色下面又有几个目录,handlers下面是当发生改变时要执行的操作,通常用在配置文件发生改变,重启服务。files为安装时用到的一些文件,meta为说明信息,说明角色依赖等信息,tasks里面是核心的配置文件,templates通常存一些配置文件,启动脚本等模板文件,vars下为定义的变量;
准备安装需要用到的文件:
在一台机器上事先编译安装好nginx,配置好启动脚本,配置好配置文件
安装好后,我们需要把nginx目录打包,并放到/etc/ansible/nginx_install/roles/install/files/下面,名字为:nginx.tar.gz
启动脚本、配置文件都要放到/etc/ansible/nginx_install/roles/install/templates下面
编译安装:
[root@KXLZQ ~]# ls /usr/local/
bin etc games include lib lib64 libexec nginx sbin share src
[root@KXLZQ ~]# ls /usr/local/nginx/
XXQ_body_temp conf fastcgi_temp html logs nginx.conf proxy_temp sbin scgi_temp uwsgi_temp
把nginx目录打包,并放到files下面,以及把启动脚本、配置文件放到templates下面:
[root@KXLZQ ~]# cd /usr/local/
[root@KXLZQ local]# tar -czvf nginx.tar.gz --exclude “nginx.conf” --exclude “vhost” nginx/
[root@KXLZQ local]# mv nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/
[root@KXLZQ local]# cp nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/
[root@KXLZQ local]# cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/
定义common的tasks,因为nginx是需要一些依赖包的:
[root@KXLZQ local]# cd /etc/ansible/nginx_install/roles
[root@KXLZQ roles]# vim ./common/tasks/main.yml

  • name: Install initializtion require software
    yum: name=“zlib-devel,pcre-devel” state=installed
    定义变量:
    [root@KXLZQ roles]# vim install/vars/main.yml
    nginx_user: www
    nginx_port: 80
    nginx_basedir: /usr/local/nginx
    然后要把所有用到的文档拷贝到目标机器:
    [root@KXLZQ roles]# vim install/tasks/copy.yml
  • name: Copy Nginx Software # 拷贝nginx包
    copy: src=nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root
  • name: Uncompression Nginx Software # 解压nginx包
    shell: tar zxf /tmp/nginx.tar.gz -C /usr/local/
  • name: Copy Nginx Start Script # 拷贝nginx的启动脚本
    template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755
  • name: Copy Nginx Config # 拷贝nginx的配置文件
    template: src=nginx.conf dest={{ nginx_basedir }}/conf/ owner=root group=root mode=0644
    注:这里src参数指定的是相对路径,这个相对路径相对的是模块,例如copy模块里的src参数的值为nginx.tar.gz,那么就会去files目录下找nginx.tar.gz,而template模块则是会去templates目录下找。
    建立用户,启动服务,删除压缩包:
    [root@KXLZQ roles]# vim install/tasks/install.yml
    name: Create Nginx User # 创建nginx用户
    user: name={{ nginx_user }} state=present createhome=no shell=/sbin/nologin
    name: Start Nginx Service # 启动nginx服务
    shell: /etc/init.d/nginx start
    name: Add Boot Start Nginx Service # 将nginx服务添加到开机启动列表
    shell: chkconfig --level 345 nginx on
    name: Delete Nginx compression files # 删除nginx的压缩包
    shell: rm -rf /tmp/nginx.tar.gz
    注:nginx_user变量是之前我们在vars目录下的main.yml文件中定义的,所以在这里可以直接引用。
    再创建一个总体的main.yml,在这个文件里对copy.yml以及install.yml进行一个调用
    [root@KXLZQ roles]# vim install/tasks/main.yml
  • include: copy.yml
  • include: install.yml
    到此两个roles:common和install就定义完成了,接下来要定义一个作为总入口的配置文件:
    [root@KXLZQ nginx_install]# vim install.yml

  • hosts: testhost
    remote_user: root
    gather_facts: True
    roles:
    • common
    • install
      注意:接着就是需要执行这个总入口文件了,但是如果你的目标机器之前安装过nginx需要先清理掉,不然可能会有冲突:
      编译安装的清理办法:(find / -name “nginx*” -exec rm -rf {} 😉
      yum安装的清理办法:rpm -qa nginx yum remove -y nginx
      执行这个总入口文件:
      [root@KXLZQ ~]# ansible-playbook /etc/ansible/nginx_install/install.yml
      [DEPRECATION WARNING]: The use of ‘include’ for tasks has been deprecated. Use ‘import_tasks’ for static inclusions or
      ‘include_tasks’ for dynamic inclusions. This feature will be removed in a future release. Deprecation warnings can be
      disabled by setting deprecation_warnings=False in ansible.cfg.
      [DEPRECATION WARNING]: include is kept for backwards compatibility but usage is discouraged. The module documentation
      details page may explain more about this rationale… This feature will be removed in a future release. Deprecation
      warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
      PLAY [testhost] ********************************************************************************************************
      TASK [Gathering Facts] *************************************************************************************************
      ok: [192.168.70.129]
      TASK [common : Install initializtion require software] *****************************************************************
      changed: [192.168.70.129]
      TASK [install : Copy Nginx Software] ***********************************************************************************
      changed: [192.168.70.129]
      TASK [install : Uncompression Nginx Software] **************************************************************************
      [WARNING]: Consider using unarchive module rather than running tar
      changed: [192.168.70.129]
      TASK [install : Copy Nginx Start Script] *******************************************************************************
      changed: [192.168.70.129]
      TASK [install : Copy Nginx Config] *************************************************************************************
      changed: [192.168.70.129]
      TASK [install : Create Nginx User] *************************************************************************************
      changed: [192.168.70.129]
      TASK [install : Start Nginx Service] ***********************************************************************************
      changed: [192.168.70.129]
      TASK [install : Add Boot Start Nginx Service] **************************************************************************
      changed: [192.168.70.129]
      TASK [install : Delete Nginx compression files] ************************************************************************
      [WARNING]: Consider using file module with state=absent rather than running rm
      changed: [192.168.70.129]
      PLAY RECAP *************************************************************************************************************
      192.168.70.129 : ok=10 changed=9 unreachable=0 failed=0
      查看进程及监听端口:
      [root@XXQ ~]# ps aux |grep nginx
      root 3847 0.0 0.0 20492 612 ? Ss 16:06 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
      nobody 3848 0.0 0.1 20932 1332 ? S 16:06 0:00 nginx: worker process
      root 3964 0.0 0.0 112676 980 pts/0 S+ 16:08 0:00 grep --color=auto nginx
      [root@XXQ ~]# netstat -lntp |grep nginx
      tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3847/nginx: master
      安装完成。

playbook管理配置文件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

生产环境中大多时候是需要管理配置文件的,例如修改配置文件然后进行重启服务,修改配置文件时可能会出现误修改的情况,所以我们还需要准备一个回滚的操作。至于安装软件包只是在初始化环境的时候用一下。下面我们来写个管理nginx配置文件的playbook。
创建相应的目录:
[root@KXLZQ ~]# mkdir -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}
[root@KXLZQ ~]# cd /etc/ansible/nginx_config/
[root@KXLZQ nginx_config]# ls
roles
[root@KXLZQ nginx_config]# ls roles/
new old
[root@KXLZQ nginx_config]# ls roles/new/
files handlers tasks vars
[root@KXLZQ nginx_config]# ls roles/old/
files handlers tasks vars
其中new为更新时用到的,old为回滚时用到的,files下面为nginx.conf和vhosts目录,handlers为重启nginx服务的命令
注:
关于回滚,需要在执行playbook之前先备份一下旧的配置,所以对于老配置文件的管理一定要严格,千万不能随便去修改线上机器的配置,并且要保证new/files下面的配置和线上的配置一致
把nginx.conf和vhost目录放到files目录下面:
[root@KXLZQ nginx_config]# cd /usr/local/nginx/conf/
[root@KXLZQ conf]# ls
fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params
fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default
fastcgi_params koi-win nginx.conf scgi_params.default win-utf
[root@KXLZQ conf]# cp -r nginx.conf /etc/ansible/nginx_config/roles/new/files/
编辑定义变量的文件:
[root@KXLZQ ~]# vim /etc/ansible/nginx_config/roles/new/vars/main.yml
nginx_basedir: /usr/local/nginx
编辑用于定义重新加载nginx服务的文件:
[root@KXLZQ ~]# vim /etc/ansible/nginx_config/roles/new/handlers/main.yml

  • name: restart nginx
    shell: /etc/init.d/nginx reload
    编辑用于执行核心任务的文件:
    [root@KXLZQ ~]# vim /etc/ansible/nginx_config/roles/new/tasks/main.yml
  • name: copy conf file
    copy: src={{ item.src }} dest={{ nginx_basedir }}/{{ item.dest }} backup=yes owner=root group=root mode=0644
    with_items:
    • { src: nginx.conf, dest: conf/nginx.conf }
    • { src: vhost, dest: conf/ }
      notify: restart nginx
      最后是定义总入口配置文件:
      [root@KXLZQ ~]# vim /etc/ansible/nginx_config/update.yml

  • hosts: testhost
    user: root
    roles:
    • new
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!