zoukankan      html  css  js  c++  java
  • ansible----playbook

    playbook:

    playbook相关的命令:

    [root@centos7 ansible]# ansible-playbook -C xxx.yml               #检查playbook,但不会真正的去执行
    [root@centos7 ansible]# ansible-playbook --list-hosts xxx.yml     #列出所指定的playbook应用的所有主机
    [root@centos7 ansible]# ansible-playbook --list-tags hello.yml    #列出所指定的playbook中的所有标签
    [root@centos7 ansible]# ansible-playbook --list-tasks hello.yml   #列出所指定的playbook中的所有任务
    [root@centos7 ansible]# ansible-playbook --limit 192.168.38.6 hello.yml   #针对所指定的playbook,限制某个主机执行
    

    playbook格式要求:

    ---                                                                            #用于区分yaml语言格式的标准标识
    #install http and configure                                                    #注释,说明当前playbook的用途
    - hosts: websrvs                                                               #应用的主机清单
      remote_user: root                                                            #以谁的身份连接远程主机 
    
      tasks:                                                                       #任务集,必须添加
        - name: install httpd package                                              #指定任务名称,每个任务都要有一个对应的名称
          yum: name=httpd                                                          #指定模块和参数
        - name: config file
          copy: src=./httpd.conf dest=/etc/httpd/conf/ backup=yes
        - name: start service
          service: name=httpd state=started enabled=yes
    
    playbook有严格的缩进要求和空格要求
    

    handlers触发器

    ansible有幂等性,上次执行结果和这次执行结果相同,就不会再执行了
    ---
    #install http and configure
    - hosts: websrvs:!192.168.38.8             #把192.168.38.8从websrvs分组中排除
      remote_user: root
    
      tasks:
        - name: install httpd package
          yum: name=httpd
        - name: config file
          copy: src=./httpd.conf dest=/etc/httpd/conf/ backup=yes
          notify: restart httpd service                #当执行copy的时候,会执行指定名称的触发器;注意:ansible有幂等性,上次和这次执行的结果一样,上次已经执行完了,这次就不会再执行,所以触发器也不会触发
        - name: start service
          service: name=httpd state=started enabled=yes
     
      handlers:                                        #指定触发器
        - name: restart httpd service                  #给触发器设置个名称
          service: name=httpd state=restarted          #指定触发器执行的动作 
    

    tags标签

    ---
    #install http and configure
    - hosts: websrvs
      remote_user: root
    
      tasks:
        - name: install httpd package
          yum: name=httpd
          tags: install                                                   #指定标签名字
        - name: config file
          copy: src=./httpd.conf dest=/etc/httpd/conf/ backup=yes
          tags: config                                                    #指定标签名字
        - name: start service
          service: name=httpd state=started enabled=yes
    
    [root@centos7 ansible]# ansible-playbook -t install,config install_http.yml             #执行playbook时,可以选择指定标签进行执行
    
    一个任务有标签并且有触发器,选择执行标签时,触发器也会跟着执行
    

    playbook变量

    setup模块
    可以查看指定的所有主机的所有状态信息
    filter: 过滤出指定的信息
    [root@centos7 ansible]# ansible all -m setup -a 'filter="ansible_nodename"'
    [root@centos7 ansible]# ansible websrvs -m setup -a 'filter="*mem*"'                       #filter后面支持通配符
    
    ansible系统自带常用的变量:
    ansible_nodename: 查看指定远程主机的主机名
    ansible_memtotal_mb: 查看指定远程主机的内存
    ansible_distribution_major_version: 查看指定远程主机的主版本号
    ansible_processor_vcpus: 查看指定远程主机的cpu个数
    

    自定义变量

    hostname模块
    设置指定远程主机的主机名
    
    主机清单中自定义变量
    [root@centos7 ansible]# vim /etc/ansible/hosts
    [appsrvs]
    192.168.38.37 http_port=817 name=www      #一个主机可以自定义多个变量
    192.168.38.47 http_port=827 name=web
    [appsrvs:vars]                            #设置appsrvs组中通用变量
    mark="-"    
    [root@centos7 ansible]# ansible appsrvs -m hostname -a 'name={{name}}{{mark}}{{http_port}}.com'                  #使用"{{变量名 }}",在命令行中引用自定义的变量
    
    主机清单中主机的自定义变量优先级高于主机清单中的通用变量
    
    命令行中自定义变量
    [root@centos7 ansible]# ansible appsrvs -e mark="server" -m hostname -a 'name={{name}}{{mark}}{{http_port}}'
    # -e 后面可以自定义变量;命令行自定义变量优先级高于主机清单中的自定义变量
    
    playbook中自定义变量
    ---
    #install http and configure
    - hosts: websrvs
      remote_user: root
      vars:                            #设置变量
        - pack: tftp-server            #变量赋值
        - service: tftp.socket         #变量赋值
    
      tasks:
        - name: install httpd package
          yum: name={{pack}}                #使用"{{变量名}}"引用变量
        - name: start service
          service: name={{service}} state=started enabled=yes
    
    把自定义变量存放在yml文件中,通过playbook引用
    [root@centos7 ansible]# vim vars.yml       #编辑存放变量的yml文件
    pack: vsftpd
    service: vsftpd
    [root@centos7 ansible]# vim install_vsftpd.yml        #编辑playbook
    ---
    #install http and configure
    - hosts: websrvs
      remote_user: root
      vars_files:              #引用变量文件
        - vars.yml             #指定存放变量的文件名
    
      tasks:
        - name: install httpd package
          yum: name={{pack}}
        - name: start service
          service: name={{service}} state=started enabled=yes
    

    playbook中的when判断语句

    [root@centos7 ansible]# vim when.yml             #编辑一个playbook
    ---
    #test when
    - hosts: appsrvs
      remote_user: root
    
      tasks:
        - name: centos6
          file: path=/data/centos6.log state=touch
          when: ansible_distribution_major_version == "6"         #判断:ansible_distribution_major_version变量为系统自带变量(查看主机的主版本号),如果主机的主版本号为6,就执行上面的file模块
        - name: centos7
          file: path=/data/centos7.log state=touch
          when: ansible_distribution_major_version == "7"         #判断:如果主机的主版本号为7,就执行上面的file模块
    

    playbook-----with_items迭代

    迭代:当有需要重复性执行的任务时,可以使用迭代机制
    ---
    #test with_items
    - hosts: appsrvs
      remote_user: root
    
      tasks:
        - name: create user
          user: name={{item}}           #item变量为系统自带变量,item的值会从下面的with_items中读取;with_items有几个列表,重复执行几次操作
          with_items:
            - test1
            - test2
            - test3
    
    with_items嵌套:
    ---
    #test with_items
    - hosts: appsrvs
      remote_user: root
    
      tasks:
        - name: create group
          group: name={{item}}              #先执行一个循环,创建group1、2、3,3个组
          with_items:
            - group1
            - group2
            - group3
        - name: create user
          user: name={{item.user}} group={{item.group}}           #要想把test1用户对应的主组设置为group1,把两个没有关联的组合起来,需要设置两个键值对儿,并且把两个键值对儿组合成一个字典
          with_items:
            - {user: test1, group: group1 }                       #一个字典里面设置两个键值对儿
            - {user: test2, group: group2 }
            - {user: test3, group: group3 }
    

    强化playbook及各种模块的应用:

    playbook二进制安装mysql数据库

    ---
    #install mysql
    - hosts: websrvs
      remote_user: root
    
      tasks:
        - name: create user
          user: name=mysql uid=27 system=yes shell=/sbin/nologin create_home=no
        - name: create datadir
          file: path=/data/mysql owner=mysql group=mysql state=directory
        - name: unarchive package
          unarchive: src=./files/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz dest=/usr/local/ owner=root group=root
        - name: create link
          file: src=/usr/local/mysql-5.6.46-linux-glibc2.12-x86_64 path=/usr/local/mysql state=link
        - name: install lager
          yum: name=autoconf,libaio.so.1,libaio-devel.x86_64
        - name: install database
          shell: chdir=/usr/local/mysql ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
        - name: config file
          copy: src=./files/my.cnf dest=/etc/ backup=yes
        - name: service script
          shell: cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
        - name: start service
          service: name=mysqld state=started enabled=yes
        - name: PATH variable
          copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
    
    如遇到缺少文件,可以使用 rpm -qf 加具体路径文件 或者 yum provides 加文件;进行查询
    

    playbook----删除mysql数据库

    [root@centos7 ansible]# vim remove_mysql.yml
    ---
    #remove mysql
    - hosts: appsrvs
      remote_user: root
    
      tasks:
        - name: stop service              #删除之前先停服务
          service: name=mysqld state=stopped
        - name: delete user               #删除用户和家目录
          user: name=mysql state=absent remove=yes
        - name: delete file               #删除程序、数据库、PATH文件、配置文件、二进制安装和软链接;都是使用file模块,可以选择用with_items循环删除
          file: path={{item}} state=absent
          with_items:
            - /data/mysql
            - /usr/local/mysql
            - /usr/local/mysql-5.6.46-linux-glibc2.12-x86_64
            - /etc/profile.d/mysql.sh
            - /etc/init.d/mysqld
            - /etc/my.cnf
  • 相关阅读:
    js等弱类型语言的"鸭子类型"
    js中的局部函数与局部变量
    js中的break和continue
    for in 循环
    js中的异常
    js中的运算符
    js中的符合类型
    js中的数据类型
    js强大的数据类型转换
    shell67批量创建用户(来自文件)
  • 原文地址:https://www.cnblogs.com/dongzhanyi123/p/11914493.html
Copyright © 2011-2022 走看看