ansible作业: Playbook 编写指南 1、使用debug模块显示当前受管主机的dns服务器的ip地址。[devopsmaster chap03]$ vim show_dns.yml --- - name: show dns ip hosts: master gather_facts: yes tasks: - name: show dns by debug debug: var: ansible_facts.dns.nameservers[devopsmaster chap03]$ ansible-playbook show_dns.yml PLAY [show dns ip] ******************************************************************************* TASK [Gathering Facts] *************************************************************************** ok: [master] TASK [show dns by debug] ************************************************************************* ok: [master] { ansible_facts.dns.nameservers: [ 114.114.114.114 ] } PLAY RECAP *************************************************************************************** master : ok2 changed0 unreachable0 failed0 skipped0 rescue0 ignored0Ansible 会自动收集受管主机的 facts 其中 ansible_facts.dns.nameservers 包含 /etc/resolv.conf 中配置的所有 DNS 服务器地址。debug 模块的 var 参数直接输出变量内容执行后即可看到114.114.114.114 。2、将example.conf文件复制到/etc/httpd/conf.d/目录example.conf文件内容如下virtualhost *:80 servername 0.0.0.0 documentroot /var/www/html /virtualhost directory /var/www/html allowoverride none require all granted /directory如果/etc/httpd/conf.d/目录下的文件更新则重启httpd服务。配置/var/www/html/index.html文件内 容如下zuoye[devopsmaster chap03]$ vim http_setup.yml - hosts: all become: yes tasks: - file: path: /var/www/html state: directory - file: path: /etc/httpd/conf.d state: directory - copy: src: ./index1.html dest: /var/www/html/index.html - copy: src: ./example.conf dest: /etc/httpd/conf.d/example.conf notify: restart httpd handlers: - name: restart httpd service: name: httpd state: restarted[devopsmaster chap03]$ vim example.conf virtualhost *:80 servername 0.0.0.0 documentroot /var/www/html /virtualhost directory /var/www/html allowoverride none require all granted /directory ~[devopsmaster chap03]$ vim index1.html zuoye[devopsmaster chap03]$ ansible-playbook http_setup.yml PLAY [all] *************************************************************************************** TASK [Gathering Facts] *************************************************************************** ok: [node02] ok: [master] ok: [node01] TASK [file] ************************************************************************************** ok: [node02] ok: [master] ok: [node01] TASK [file] ************************************************************************************** ok: [node02] ok: [master] ok: [node01] TASK [copy] ************************************************************************************** ok: [node02] ok: [master] ok: [node01] TASK [copy] ************************************************************************************** ok: [node02] ok: [master] ok: [node01] PLAY RECAP *************************************************************************************** master : ok5 changed0 unreachable0 failed0 skipped0 rescued0 ignored0 node01 : ok5 changed0 unreachable0 failed0 skipped0 rescued0 ignored0 node02 : ok5 changed0 unreachable0 failed0 skipped0 rescued0 ignored0使用 notify 和 handlers 实现 “文件变更则重启” 的幂等逻辑只有 copy 任务实际修改了目标文件时才会触发 restart httpd 处理器。file 模块确保 /var/www/html 目录存在避免 node02 上因目录缺失而失败。3、向受管主机的/home/file文件里面写入内容如下hostname当前主机的名字 memory当前主机的内存大小 BIOS version当前主机的bios的版本 distribution当前linux主机的发行版本信息 Size of disk device is 当前主机的磁盘大小[devopsmaster chap03]$ vim content.yml --- - name: select content hosts: master tasks: - file: path: /home/file state: directory - copy: src: ./content.conf dest: /home/file[devopsmaster chap03]$ vim content.conf hostname{{ ansible_hostname }} memory{{ ansible_memtotal_mb }} MB BIOS version{{ ansible_bios_version }} distribution{{ ansible_distribution }} {{ ansible_distribution_version }} Size of disk device is {{ ansible_devices.sda.size }}[devopsmaster chap03]$ ansible-playbook content.yml PLAY [select content] **************************************************************************** TASK [Gathering Facts] *************************************************************************** ok: [master] TASK [file] ************************************************************************************** changed: [master] TASK [copy] ************************************************************************************** changed: [master] PLAY RECAP *************************************************************************************** master : ok3 changed2 unreachable0 failed0 skipped0 rescued0 ignored04.使用file.j2模板文件向受管主机的/home/file文件里面写入内容如下hostname当前主机的名字 memory当前主机的内存大小 BIOS version当前主机的bios的版本 distribution当前linux主机的发行版本信息 Size of disk device is 当前主机的磁盘的大小[devopsmaster chap03]$ vim file2.yml --- - name: select content hosts: master tasks: - file: path: /home/file state: directory - template: src: file2.j2 dest: /home/file[devopsmaster chap03]$ vim file2.j2 hostname{{ ansible_hostname }} memory{{ ansible_memtotal_mb }} MB BIOS version{{ ansible_bios_version }} distribution{{ ansible_distribution }} {{ ansible_distribution_version }} Size of disk device is {{ ansible_devices.sda.size }}[devopsmaster chap03]$ ansible-playbook file2.yml PLAY [select content] **************************************************************************** TASK [Gathering Facts] *************************************************************************** ok: [master] TASK [file] ************************************************************************************** ok: [master] TASK [template] ********************************************************************************** changed: [master] PLAY RECAP *************************************************************************************** master : ok3 changed1 unreachable0 failed0 skipped0 rescued0 ignored0template 模块会对 file.j2 进行 Jinja2 渲染将所有 {{ }} 变量替换为受管主机的实际值这是与 copy 模块的本质区别。5、如果当前受管主机的根分区容量大于1G则安装httpd和mariadb-server软件包如果httpd和mariadb服务未运行则运行该服务。[devopsmaster chap03]$ vim partition.yml --- - name: partition hosts: master become: yes tasks: - name: Install httpd and mariadb-server yum: name: {{ item }} state: present loop: - httpd - mariadb-server when: (ansible_mounts | selectattr(mount, equalto, /) | map(attributesize_total) | first | int) 1073741824 - name: Start httpd and mariadb service: name: {{ item }} state: started loop: - httpd - mariadb when: (ansible_mounts | selectattr(mount, equalto, /) | map(attributesize_total) | first | int) 1073741824[devopsmaster chap03]$ ansible-playbook partition.yml PLAY [partition] ********************************************************************************* TASK [Gathering Facts] *************************************************************************** ok: [master] TASK [Install httpd and mariadb-server] ********************************************************** ok: [master] (itemhttpd) ok: [master] (itemmariadb-server) TASK [Start httpd and mariadb] ******************************************************************* ok: [master] (itemhttpd) ok: [master] (itemmariadb) PLAY RECAP *************************************************************************************** master : ok3 changed0 unreachable0 failed0 skipped0 rescued0 ignored06、创建一个playbook要求如下:该playbook运行在所有受控节点 该playbook覆盖/etc/message文件的内容 在dev主机组的主机上内容是:Development 在test主机组的主机上内容是:Test[devopsmaster chap03]$ vim content1.yml --- - name: select content hosts: master become: yes tasks: - name: Set /etc/message for dev group copy: content: Development dest: /etc/message when: dev in group_names - name: Set /etc/message for test group copy: content: Test dest: /etc/message when: test in group_names[devopsmaster chap03]$ ansible-playbook content1.yml PLAY [select content] **************************************************************************** TASK [Gathering Facts] *************************************************************************** ok: [master] TASK [Set /etc/message for dev group] ************************************************************ skipping: [master] TASK [Set /etc/message for test group] *********************************************************** skipping: [master] PLAY RECAP *************************************************************************************** master : ok1 changed0 unreachable0 failed0 skipped2 rescued0 ignored0