18,Anisble剧本

1,ansible剧本
playbook 翻译过来就是“剧本”, 那 playbook 组成如下
play: 定义的是主机的角色
task: 定义的是具体执行的任务
playbook: 由一个或多个 play 组成,一个 play 可以包含多个 task 任务
简单理解为: 使用不同的模块完成一件事情

2,ansible剧本的优势
1,功能比ansible命令更强大
2,能控制先后执行顺序和依赖关系
3,语法更加直观

3,ansible使用yaml语法
1)以缩进代表不同层级之间的关系
2)对缩进有严格要求
3)-横杠,横杠后面有空格代表列表
4):冒号,冒号后有空格,表示赋值

4,ansible剧本小实例模板

ansible nfs -m group -a "name=www gid=666 state=present"                                        
ansible nfs -m user -a "name=www uid=666 group=666 shell=/sbin/nologin create_home=no"                                      
ansible nfs -m yum -a "name=nfs-utils"                                      
ansible nfs -m file -a "path=/data state=directory"                                     
ansible nfs -m copy -a "src=exports dest=/etc/exports backup=yes"                                       
ansible nfs -m service -a "name=rpcbind state=started enabled=yes"                                      
ansible nfs -m service -a "name=nfs state=started enabled=yes"                                      
ansible nfs -m shell -a "showmount -e"                                      

小试牛刀:


小试牛刀:                   
- hosts: nfs                主机组 
  tasks:                     任务 
  - name: create group              取名字任意,要方便记  
    group:                                      引用的模块   
      name: www                         参数1 
      gid: 666                                 参数2  
      state: present                        参数3 
  - name: create user                   
    user:                   
      name: www                 
      uid: 666                  
      group: www                    
      shell: /sbin/nologin                  
      create_home: no                   
  - name: install nfs soft                  
    yum:                    
      name: nfs-utils                   
  - name: mkdir directory                   
    file:                   
      path: /data                   
      state: directory                  
  - name: copy file                 
    copy:                   
      src: /root/exports                    
      dest: /etc/exports                    
      backup: yes                   
  - name: start rpcbind                 
    service:                    
      name: rpcbind                 
      state: started                    
      enbaled: yes                  
  - name: start nfs                 
    service:                    
      name: nfs                 
      state: started                    
      enbaled: yes                  
  - name: show mount                    
    shell: showmount -e

5,ansible执行方式
1)ansible-playbook --syntax-check xxx.yaml 语法检查
2)ansible-playbook -C xxx.yaml 模拟执行
3)ansible-playbook xxx.yaml 执行剧本
6,anisble剧本高级特性-loop
使用场景:在写ansible剧本中我们经常会写到重复性命令,比如创建多个用户,多个组,多个目录,安装多个软件
一个个写就太麻烦了,也体现不出ansible剧本的优越性。所以我们就要用到它的一些高级特性

- hosts: nfs                    
  tasks:                    
  - name: create directory                  
    file:                   
      path: "{{ item }}"                    
      state: present                    
    loop:                   
    - /data                 
    - /dat2                 

  - name: add group                 
   group:                   
      name: "{{ item.name }}"                   
      gid: "{{ item.gid }}"                 
      state: present                    
   loop:                    
   - { name: group1, gid: 1111 }                    
   - { name: group2, gid: 2222 }                    

  -name: add user                   
    user:                   
      name: "{{ item.name }}"                   
      uid: "{{ item.uid }}"                 
      group: "{{ item.group }}"                 
      shell: /sbin/nologin                  
      create_home: no                   
    loop:                   
    - { name: user1, uid: 7777 group: group1 }                  
    - { name: user2, uid: 8888 group: group2 }  

六,ansible剧本高级特性-变量
使用情景:
1.自定义某个名称,在任务中会多次引用
2.从主机收集的系统信息中提取某个变量并引用,例如网卡信息

- hosts: nfs        
  var:      
    path: /opt/data     
  tasks:        
  - name: create directory      
    file:       
      path:"{{ path }}"     
      state: present        

也可以写在/etc/ansible/hosts

18,Anisble剧本

- hsots: nfs                                        
  tasks:                                        
  - name: show ip                                       
    shell: "echo {{ ansible_facts.eth1.ipv4.address }} > /root/ip_eth1.txt"                         
    shell: "echo {{ ansible_facts.eth0.ipv4.address }} > /root/ip_eth0.txt"

(ansible内置变量提取ip地址的变量)

六,ansible剧本高级特性-注册变量
使用情景:将配置文件的状态注册成一个变量,方便其他任务引用
例:

- hosts: nfs                                    
  tasks:                                    
  - name: show ip                                   
    shell: "echo {{ ansible_facts.eth1.ipv4.address }}"                                 
    register: eth1 (register是固定用法,表示注册一个叫eth1的变量)                                   
  - name: echo eth1                                 
    debug:                                  
      msg: "{{ eth1.stdout }} " (固定用法,加.stdout表示显示带stdout的行因为这行刚好有ip)

六,ansible剧本高级特性-触发机制
使用场景:通常剧本里定义的配置文件一旦修改后,我们都要重启服务,但是ansible定义的service模块只能state: started
所以要有一个触发机制,当配置文件修改,服务自动重启

- hosts: nfs            
  tasks:            
- name: copy export         
    copy:           
      src: /root/exports            
      dest: /etc/exports            
      backup: yes           
    notify: restart_nfs_server          
  handlers:         
  - name: restart_nfs_server            
    service:            
      name: "{{ item }}"            
      state: restarted          
    loop:           
    - rpcbind           
    - nfs

六,ansible剧本高级特性-标签tags
应用场景:给剧本里执行的每个模块打上tags,在你执行的时候你可以灵活指定执行哪个模块或者对于报错的模块单独执行,而不需要再从头执行一遍

- hosts: 172.16.1.41                            
  tasks:                            
  - name: 01-add group                          
    group: name=www gid=666                         
    tags: 01-add-group                          
  - name: 02-add user                           
    user: name=www create_home=no shell=/sbin/nologin group=www uid=666                         
    tags: 02-add-user                           
  - name: 03-install rsync                          
    yum: name=rsync state=installed                         
    tags: 03-install-rsync                          
  - name: 04-copy rsync conf                            
    copy: src=/server/scripts/rsyncd.conf dest=/etc/                            
    tags: 04-copy-conf                          
  - name: 05-create passwd conf                         
    copy: content='rsync_backup:oldboy' dest=/etc/rsync.passwd mode=600                         
    tags: 05-create-passwd                          
  - name: 06-create backup dir                          
    file: path=/backup state=directory owner=www group=www                          
    tags: 06-create-backup                          
  - name: 07-create backup dir                          
    file: path=/data state=directory owner=www group=www                            
    tags: 07-create-data                            
  - name: 08-start rsyncd service                           
    service: name=rsyncd state=started                          
    tags: 08-start-rsyncd                           
  - name: 09-enabled rsyncd service                         
    systemd: name=rsyncd enabled=yes                            
    tags: 09-enable 

1)指定运行某个tags:
ansible-playbook -t 05-create-passwd tags2.yml
2)指定运行多个tags:
ansible-playbook -t 05-create-passwd,06-create-backup tags2.yml
3)指定不运行某个tags:
ansible-playbook --skip-tags=05-create-passwd tags2.yml
4)指定不运行多个tags:
ansible-playbook --skip-tags=05-create-passwd,06-create-backup tags2.yml

原文地址:https://blog.51cto.com/13858002/2433644

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


----name:setpublickeyonremotehosts&setreomtehostssudoersfilehosts:all#此脚本要求所有的主机root用户密码相同become:noremote_user:rootvars:-remoteuser:user1:rhcetasks:-name:setsecondarygroupforrhce
环境准备#cat/etcedhat-releaseCentOSLinuxrelease7.9.2009(Core)#pythonPython3.7.11(default,Jul312022,16:12:35)[GCC4.8.520150623(RedHat4.8.5-44)]onlinuxType"help","copyright","credits"or"li
准备好环境,在安装之前请先了解openshift提供的ansible有大量的安装选项文档地址:https://docs.okd.io/latest/install/configuring_inventory_file.html1、配置/etc/ansible/hosts 
Ansible:运维工作:系统安装(物理机、虚拟机)-->程序包安装、配置、服务启动-->批量操作-->程序发布-->监控OSProvisioning:物理机:PXE、Cobbler虚拟机:ImageTemplatesConfigration:puppet(ruby)saltstack(python)chefcfengineCommandand
ansible与salt对比相同都是为了同时在多台机器上执行相同的命令都是python开发不同agent(saltstack需要安装、ansible不需要)配置(salt配置麻烦,ansible基本不用配置)学习路线(salt比较陡峭,ansible比较平缓)第三方工具(salt比较少)开源社区的对接(salt比较少)现有用户(salt还是an
[root@node1playbook]#catnginx.yml-hosts:test\\主机组,要和nginx.yml在同一个目录下remote_user:root\\远端执行任务的用户tasks:\\任务-name:installhttpd\\任务描述command:yum-yinstallhttpd\\调用ansible的command模块安装httpd-na
一直不知道这个模块到底在哪,并且也挺想搞清楚官方那些模块到底在哪个目录下。1.使用grep-rl"copy.py"/usr/lib/python2.7/site-packages/ansible(这个目录是专门放ansible源码目录的)然后找到是/usr/lib/python2.7/site-packages/ansible/modules/files/copy.py这个文件,这里需要
ansile作为去除安装agent的自动化工具,通过ssh协议的简单功能强大的自动化工作。在ansile使用的过程中,有三种用途1、ansible自行一次性任务,即执行命令如:ansible10.59.87.11-mping*ansible"dev-hdp"-mcopy-a"src=oot/HfHadoopHiveUdf.jardest=/data1/opt/cloudera/par
ansible-playbook(1) Ansible组成部分InventoryModulesAdHocCommandsplaybooksplaybooks:Tasks:任务,即调用的模块完成的某操作variables:变量Templates:模版Roles:角色 基本结构:-host:webserverremote_user:tasks:
报错:[root@jenkins~]#ansiblego_activity-mcron-a"name='log_clear'minute=0hour=2job=find/home/golanger/log/-typef-name'log$(date+\%d-d-1day)'-delete"ERROR!thistask'cron'hasextraparams,wh
一、测试环境说明1、系统:rhel6.92、ip地址:20.20.20.24/2420.20.20.41/2420.20.20.42/243、以下操作使用root身份进行,也可以使用具有sudo权限的用户进行相关操作二、环境准备1、关闭iptables防火墙、selinux#/etc/init.d/iptablesstop#
ansible常用模块安装:依赖于epel源yuminstallansible-y配置文件:/etc/ansible/ansible.cfgInvertoory:/etc/ansible/hosts 如何查看模块帮助:ansible-doc-lansible-doc-sMODULE_NAME` ansible命令应用基础:语法:ansible<host-pattern>[options]-fforks
copycopy模块是将ansible管理主机上的文件拷贝上远程主机中,与fetch相反,如果目标路径不存在,则自动创建,如果src的目录带“/”则复制该目录下的所有东西,如果src的目录不带“/”则连同该目录一起复制到目标路径;常用模块src参数:用于指定需要copy的文件或目录
9.YAML9.1简介(1)YAML是一个可读性高的用来表达资料序列的格式。(2)YAML参考了其它多种语言。包括:XML、C语言、python、perl以及电子邮件格式的RFC2822等。ClarkEvans在2001年首次发表了这种语言。(3)YAML不是XML,在开发这种语言时,YAML的意思其实是:yetanothermarkuplanguage,9.2特性(1)YA
了解ansibleansible批量在远程主机上执行命令ansible主要是为了进行操作多个主机而进行的#!/bin/envpython文件中直接指向python文件#!/bin/base 指行脚本一.ansible第一步.下载epel源wget-O/etc/yum.repos.d/epel.repohttp://mirrors.aliyun.comepo/epel-7.repo
背景:在私有云环境下,遇到要开通某个项目,则需要快速的响应创建虚拟机,并且做一些基础的配置。为了提高效率以及减少手工犯错的概率,一般会采取ansible批量部署,但是使用ansible的前提是预先配置好免密。在密码一致的场景中,可以使用expect优化做免密的过程解决方案:1.
简单例子1:vars定义变量-hosts:allremote_user:rootvars:-package:nginx-service:nginx tasks:-name:installnginxpackage yum:name={{package}}state=latest-name:installconfigurationfileforhttpd copy:src=/etcginxginx
 ansible自动化运维工具的介绍      ansible结构特性:            模块化,调用特定的模块,完成特定的任务;        基于Python语言实现,由Paramiko、PyYAML和Jinja2三个关键模块;        部署简单,agentless        主从模
---恢复内容开始---Templates:模版 cat/etc/ansible/hosts  cattemplatesginx.conf.j2 -hosts:testremote_user:rootvars:-package:httpd-service:httpdtasks:-name:installnginxpackage yum:name={{package}}state=la
1、配置资源清单inventory文件[root@test1~]#cat>/etc/ansible/hosts<<EOF[k8s]192.168.0.92ansible_ssh_port=22ansible_ssh_user=k8sansible_ssh_pass='123'ansible_become_pass='123456'EOF解释:ansible_ssh_pass='123'