本文共 4489 字,大约阅读时间需要 14 分钟。
01. 编写剧本的重要功能介绍
a 在剧本中设置变量信息 OK 3种方式 常用方式--剧本中设置 b 在剧本中设置注册信息 OK 执行剧本时,可以显示输出命令结果信息 debug b 在剧本中设置判断信息 OK setup c 在剧本中设置循环信息 OK d 在剧本中设置错误忽略 OK d 在剧本中设置标签信息 OK e 在剧本中设置触发信息 OK f 在剧本中进行剧本整合 详细的剧本扩展: https://docs.ansible.com/ansible/latest/user_guide/playbooks.html f 在剧本中进行剧本整合 02. 在剧本中设置变量信息 方式一:直接在剧本文件编写 vars: test01: data01 test02: data02调用便令: {
{ test01 }} 方式二:在命令行中进行指定 ansible-playbook --extra-vars=test01=data01 短格式 -e方式三:在主机清单文件编写
[test] test01=data01 test02=data02三种变量设置方式都配置了,三种方式的优先级???
最优先: 命令行变量设置 次优先: 剧本中变量设置 最后: 主机清单变量设置如何全局设置变量: roles 剧本整合
03. 在剧本中设置注册信息 - hosts: test tasks: - name: check server port shell: netstat -lntup --- 端口信息 register: get_server_port<--端口信息 - name: display port info debug: msg={ { get_server_port.stdout_lines }} 显示进程信息,表示服务已经正常启动 PS: 设置变量不能有空格信息 04. 在剧本中设置判断信息 如何指定判断条件: (ansible_hostname == "nfs01") (ansible_hostname == "web01") setup模块中显示被管理主机系统的详细信息- hosts: test
remote_user: root tasks: - name: Check File file: path=/tmp/this_is_{ { ansible_hostname }}_file state=touch when: (ansible_hostname == "nfs") or (ansible_hostname == "backup") - name: install httpd yum: name=httpd state=installed when: (系统情况 == "CentOS") - name: install httpd2 yum: name=httpd2 state=installed when: (系统情况 == "ubuntu")获取内置变量方法:
ansible test -m setup -a "filter=ansible_hostname"常见主机信息:
ansible_all_ipv4_addresses: 仅显示ipv4的信息。 ansible_devices: 仅显示磁盘设备信息。 ansible_distribution: 显示是什么系统,例:centos,suse等。 ansible_distribution_major_version: 显示是系统主版本。 ansible_distribution_version: 仅显示系统版本。 ansible_machine: 显示系统类型,例:32位,还是64位。 ansible_eth0: 仅显示eth0的信息。 ansible_hostname: 仅显示主机名。 ansible_kernel: 仅显示内核版本。 ansible_lvm: 显示lvm相关信息。 ansible_memtotal_mb: 显示系统总内存。 ansible_memfree_mb: 显示可用系统内存。 ansible_memory_mb: 详细显示内存情况。 ansible_swaptotal_mb: 显示总的swap内存。 ansible_swapfree_mb: 显示swap内存的可用内存。 ansible_mounts: 显示系统磁盘挂载情况。 ansible_processor: 显示cpu个数(具体显示每个cpu的型号)。 ansible_processor_vcpus: 显示cpu个数(只显示总的个数)。 获取子信息方法: ansible_eth0[ipv4]04. 在剧本中设置循环信息
vim test04.yml - hosts: all remote_user: root tasks: - name: Add Users user: name={ { item.name }} groups={ { item.groups }} state=present with_items: - { name: 'testuser1', groups: 'bin' } - { name: 'testuser2', groups: 'root' } vim test05.yml - hosts: all remote_user: root tasks: - name: Installed Pkg yum: name={ { item }} state=present with_items: - wget - tree - lrzsz剧本执行出现错误排查思路/步骤:
1) 找到剧本中出现问题关键点 2) 将剧本中的操作转换成模块进行操作 3) 将模块的功能操作转换成linux命令 本地管理主机上执行命令测试 远程被管理主机上执行命令测试 - name: 01-install rsync yum: name: ['rsync', 'tree', 'wget'] --- saltstack state: installed - name: xxx yum: name=xxx state=installed --- ansible05. 在剧本中设置忽略错误
默认playbook会检查命令和模块的返回状态,如遇到错误就中断playbook的执行 可以加入ignore_errors: yes忽略错误继续向下执行 vim test06.yml - hosts: all remote_user: root tasks: - name: Ignore False command: /bin/false ignore_errors: yes - name: touch new file file: path=/tmp/test_ignore state=touch06. 在剧本中设置标签功能
- hosts: test ignore_errors: yes remote_user: root tasks: - name: Check File file: path=/tmp/this_is_{ { ansible_hostname }}_file state=touch when: (ansible_hostname == "nfs01") or (ansible_hostname == "backup") tags: t1 - name: bad thing command: echo 123 #ignore_errors: yes tags: t2 - name: install httpd yum: name=httpd state=installed when: (ansible_all_ipv4_addresses == ["172.16.1.7","10.0.0.7"]) tags: t3 - name: install httpd2 yum: name=httpd2 state=installed when: (ansible_distribution == "ubuntu") tags: t4 指定执行哪个标签任务: ansible-playbook --tags=t2 test05.yml 短格式 -t 跳过指定标签任务: ansible-playbook --skip-tags=t2 test05.yml07. 在剧本中设置触发功能
- hosts: backup remote_user: root tasks: - name: 01 Install rsync yum: name=rsync state=present - name: 02 push config file copy: src=./file/{ { item.src }} dest=/etc/{ { item.dest }} mode={ { item.mode }} with_items: - { src: "rsyncd.conf", dest: "rsyncd.conf", mode: "0644" } - { src: "rsync.password", dest: "rsync.password", mode: "0600" } notify: restart rsync serverhandlers:
- name: restart rsync server service: name=rsyncd state=restarted注:handlers:标签下的- name: 名称 必须与notify:后定义的名称一致
如果rsyncd.conf被修改重传之后 触发notify标签,然后触发handlers 重启rsyncd服务
08. 将多个剧本进行整合 方式一:include_tasks: f1.yml - hosts: all remote_user: root tasks: - include_tasks: f1.yml - include_tasks: f2.yml方式二:include: f1.yml
- include:f1.yml - include:f2.yml方式三:- import_playbook:
[root@m01 ansible-playbook]# cat main.yml - import_playbook: base.yml - import_playbook: rsync.yml - import_playbook: nfs.yml - import_playbook: oxxx.yml - import_playbook: rsync.yml - import_playbook: nfs.yml转载地址:http://jhepb.baihongyu.com/