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

    一、Ansible facts

    [ansible@hk-elk-redis1 ansible]$ ansible 10.20.11.201 -m setup
    10.20.11.201 | SUCCESS => {
        "ansible_facts": {
            "ansible_all_ipv4_addresses": [
                "10.20.11.201"
            ], 
            "ansible_all_ipv6_addresses": [
                "fe80::250:56ff:feb8:80f4"
            ], 
            "ansible_apparmor": {
                "status": "disabled"
            }, 
            "ansible_architecture": "x86_64", 
            "ansible_bios_date": "04/05/2016", 
            "ansible_bios_version": "6.00", 
            "ansible_cmdline": {
    		-----省略-----
    
    [ansible@hk-elk-redis1 ansible]$ ansible 10.20.11.201 -m setup -a 'filter=ansible_all_ipv4_addresses'	##查看特定的行
    10.20.11.201 | SUCCESS => {
        "ansible_facts": {
            "ansible_all_ipv4_addresses": [
                "10.20.11.201"
            ]
        }, 
        "changed": false
    }
    

     二、Ansible-playbook

    1使用register内的变量

    [ansible@hk-elk-redis1 playbook]$ cat register.yaml 
    ---
      - hosts: all
        gather_facts: False
        tasks:
          - name: register variables
            shell: hostname
            register: info
          - name: display variable
            debug: msg="The variable is {{ info }}"
    		
    [ansible@hk-elk-redis1 playbook]$ ansible-playbook register.yaml -l 10.20.11.201
    
    PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************
    
    TASK [register variables] ****************************************************************************************************************************************************************************************************************************************************
    changed: [10.20.11.201]
    
    TASK [display variable] ******************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201] => {
        "msg": "The variable is {'stderr_lines': [], u'changed': True, u'end': u'2018-12-20 14:35:53.043939', 'failed': False, u'stdout': u'hk-elk-redis2', u'cmd': u'hostname', u'rc': 0, u'start': u'2018-12-20 14:35:53.015943', u'stderr': u'', u'delta': u'0:00:00.027996', 'stdout_lines': [u'hk-elk-redis2']}"
    }
    
    PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
    10.20.11.201               : ok=2    changed=1    unreachable=0    failed=0 
    我们可以看到info的结果是一段python字典数据,里面存储着很多信息包括执行时间状态输出等信息,如果需要下面标准输出stdout的信息时,只需指定stdout这个key即可
    
    [ansible@hk-elk-redis1 playbook]$ vim register.yaml 
    ---
      - hosts: all
        gather_facts: False
        tasks:
          - name: register variables
            shell: hostname
            register: info
          - name: display variable
            debug: msg="The variable is {{ info['stdout'] }}"
    [ansible@hk-elk-redis1 playbook]$ ansible-playbook register.yaml -l 10.20.11.201
    
    PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************
    
    TASK [register variables] ****************************************************************************************************************************************************************************************************************************************************
    changed: [10.20.11.201]
    
    TASK [display variable] ******************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201] => {
        "msg": "The variable is hk-elk-redis2"
    }
    
    PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
    10.20.11.201               : ok=2    changed=1    unreachable=0    failed=0
    

     2、使用vars_prompt传入

    [ansible@hk-elk-redis1 playbook]$ cat vars_prompt.yaml 
    ---
      - hosts: all
        gather_facts: no
        vars_prompt:
          - name: "one"
            prompt: "please input one value"
            private: no
          - name: "two"
            prompt: "please input two value"
            default: "good"
            private: yes
        tasks:
          - name: display one value
            debug: msg="one value is {{ one }}"
          - name: display two value
            debug: msg="two value is {{ two }}"
    例子中通过vars_prompt参数进行交互传入两个变量的值,two定义了一个默认值,one没有。private表示输入是否在屏幕显示
    [ansible@hk-elk-redis1 playbook]$ ansible-playbook vars_prompt.yaml -l 10.20.11.201
    please input one value: hehe
    please input two value [good]: 
    
    PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************
    
    TASK [display one value] *****************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201] => {
        "msg": "one value is hehe"
    }
    
    TASK [display two value] *****************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201] => {
        "msg": "two value is cc"
    }
    
    PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
    10.20.11.201               : ok=2    changed=0    unreachable=0    failed=0
    

     三、ansible-playbook循环

    1、标准Loops  with_items

    [ansible@hk-elk-redis1 playbook]$ cat loops.yaml 
    ---
      - hosts: all
        gather_facts: False
        tasks:
          - name: debug loops
            debug: msg="name ----> {{ item }}"
            with_items:
              -  one
              -  two
    运行结果:
    [ansible@hk-elk-redis1 playbook]$ ansible-playbook loops.yaml -l 10.20.11.201 
    
    PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************
    
    TASK [debug loops] ***********************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201] => (item=one) => {
        "changed": false, 
        "item": "one", 
        "msg": "name ----> one"
    }
    ok: [10.20.11.201] => (item=two) => {
        "changed": false, 
        "item": "two", 
        "msg": "name ----> two"
    }
    
    PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
    10.20.11.201               : ok=1    changed=0    unreachable=0    failed=0
    
    with_items的值是python list结果,可以理解为每个task都会循环读取list里面的值,然后key的值是item,当然list里面也支持python字典,如下
    
    
    [ansible@hk-elk-redis1 playbook]$ cat loops.yaml 
    ---
      - hosts: all
        gather_facts: False
        tasks:
          - name: debug loops
            debug: msg="name ----> {{ item.key }}    value ----->{{ item.value }}"
            with_items:
              -  {key: 'one', value: 'VAULE1'}
              -  {key: 'two', value: 'VAULE2'}
    运行结果:
    [ansible@hk-elk-redis1 playbook]$ ansible-playbook loops.yaml -l 10.20.11.201 
    
    PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************
    
    TASK [debug loops] ***********************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201] => (item={u'key': u'one', u'value': u'VAULE1'}) => {
        "changed": false, 
        "item": {
            "key": "one", 
            "value": "VAULE1"
        }, 
        "msg": "name ----> one    value ----->VAULE1"
    }
    ok: [10.20.11.201] => (item={u'key': u'two', u'value': u'VAULE2'}) => {
        "changed": false, 
        "item": {
            "key": "two", 
            "value": "VAULE2"
        }, 
        "msg": "name ----> two    value ----->VAULE2"
    }
    
    PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
    10.20.11.201               : ok=1    changed=0    unreachable=0    failed=0
    

     2、嵌套Loops with_nested

    [ansible@hk-elk-redis1 loop]$ cat with_nested.yaml 
    ---
    - hosts: all
      gather_facts: True
      tasks:
       - name: debug loops
         debug: msg="name --------> {{ item[0] }}   key-------> {{ item[1] }}"
         with_nested:
           - ['A']
           - ['1','2','3']
    	   
    执行结果
    [ansible@hk-elk-redis1 loop]$ ansible-playbook with_nested.yaml -l 10.20.11.201
    
    PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************
    
    TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201]
    
    TASK [debug loops] ***********************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201] => (item=[u'A', u'1']) => {
        "changed": false, 
        "item": [
            "A", 
            "1"
        ], 
        "msg": "name --------> A   key-------> 1"
    }
    ok: [10.20.11.201] => (item=[u'A', u'2']) => {
        "changed": false, 
        "item": [
            "A", 
            "2"
        ], 
        "msg": "name --------> A   key-------> 2"
    }
    ok: [10.20.11.201] => (item=[u'A', u'3']) => {
        "changed": false, 
        "item": [
            "A", 
            "3"
        ], 
        "msg": "name --------> A   key-------> 3"
    }
    
    PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
    10.20.11.201               : ok=2    changed=0    unreachable=0    failed=0
    

     3、散列loops with_dict

    [ansible@hk-elk-redis1 loop]$ cat with_dict.yaml 
    ---
    - hosts: all
      gather_facts: False
      vars:
        user:
          fengct:
            name: fengct
            shell: bash
          test:
            name: test
            shell: sh
      tasks:
       - name: debug loops
         debug: msg="name --------> {{ item.key }}   keys -------> {{ item.value.name }}   shell -------> {{ item.value.shell }}"
         with_dict: "{{ user }}"
      
    [ansible@hk-elk-redis1 loop]$ ansible-playbook with_dict.yaml -l 10.20.11.201
    
    PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************
    
    TASK [debug loops] ***********************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201] => (item={'value': {u'shell': u'sh', u'name': u'test'}, 'key': u'test'}) => {
        "changed": false, 
        "item": {
            "key": "test", 
            "value": {
                "name": "test", 
                "shell": "sh"
            }
        }, 
        "msg": "name --------> test   keys -------> test   shell -------> sh"
    }
    ok: [10.20.11.201] => (item={'value': {u'shell': u'bash', u'name': u'fengct'}, 'key': u'fengct'}) => {
        "changed": false, 
        "item": {
            "key": "fengct", 
            "value": {
                "name": "fengct", 
                "shell": "bash"
            }
        }, 
        "msg": "name --------> fengct   keys -------> fengct   shell -------> bash"
    }
    
    PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
    10.20.11.201               : ok=1    changed=0    unreachable=0    failed=0 
    

     4、文件匹配loops with_fileglob

    [ansible@hk-elk-redis1 loop]$ vim with_fileglob.yaml
    ---
    - hosts: all
      gather_facts: False
      tasks:
       - name: debug loops
         debug: msg="logs files ---------> {{ item }}"
         with_fileglob:
            - /var/log/*.log
    
    [ansible@hk-elk-redis1 loop]$ ansible-playbook with_fileglob.yaml -l 10.20.11.201
    
    PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************
    
    TASK [debug loops] ***********************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201] => (item=/var/log/boot.log) => {
        "changed": false, 
        "item": "/var/log/boot.log", 
        "msg": "logs files ---------> /var/log/boot.log"
    }
    ok: [10.20.11.201] => (item=/var/log/yum.log) => {
        "changed": false, 
        "item": "/var/log/yum.log", 
        "msg": "logs files ---------> /var/log/yum.log"
    }
    ok: [10.20.11.201] => (item=/var/log/vmware-vmsvc.log) => {
        "changed": false, 
        "item": "/var/log/vmware-vmsvc.log", 
        "msg": "logs files ---------> /var/log/vmware-vmsvc.log"
    }
    
    PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
    10.20.11.201               : ok=1    changed=0    unreachable=0    failed=0		
    

     5、随机选择loops with_random_choice

    [ansible@hk-elk-redis1 loop]$ cat with_random_choice.yaml 
    ---
    - hosts: all
      gather_facts: False
      tasks:
       - name: debug loops
         debug: msg="name --------> {{ item }}"
         with_random_choice:
           - one
           - two
           - three
    [ansible@hk-elk-redis1 loop]$ ansible-playbook with_random_choice.yaml -l 10.20.11.201
    
    PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************
    
    TASK [debug loops] ***********************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201] => (item=one) => {
        "changed": false, 
        "item": "one", 
        "msg": "name --------> one"
    }
    
    PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
    10.20.11.201               : ok=1    changed=0    unreachable=0    failed=0
    

     6、条件判断loops until:

    [ansible@hk-elk-redis1 loop]$ vim until.yaml
    ---
    - hosts: all
      gather_facts: True
      tasks:
       - name: debug loops
         shell: cat /root/ansible
         register: host
         until: host.stdout.startswith("name")
         retries: 5
         delay: 5
    
    5秒执行一次 cat /root/ansible将结果register给host然后判断host.stdout.startswith的内容是否是Master开头,如果条件成立,此task完成运行,如果条件不成立5秒后重试,5次后还不成立,此task运行失败
    	 
    [ansible@hk-elk-redis1 loop]$ ansible-playbook until.yaml -l 10.20.11.201
    
    PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************
    
    TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201]
    
    TASK [debug loops] ***********************************************************************************************************************************************************************************************************************************************************
    FAILED - RETRYING: debug loops (5 retries left).
    FAILED - RETRYING: debug loops (4 retries left).
    FAILED - RETRYING: debug loops (3 retries left).
    FAILED - RETRYING: debug loops (2 retries left).
    FAILED - RETRYING: debug loops (1 retries left).
    fatal: [10.20.11.201]: FAILED! => {"attempts": 5, "changed": true, "cmd": "cat /root/ansible", "delta": "0:00:00.027601", "end": "2018-12-20 15:49:47.499441", "msg": "non-zero return code", "rc": 1, "start": "2018-12-20 15:49:47.471840", "stderr": "cat: /root/ansible: 权限不够", "stderr_lines": ["cat: /root/ansible: 权限不够"], "stdout": "", "stdout_lines": []}
    	to retry, use: --limit @/etc/ansible/playbook/loop/until.retry
    
    PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
    10.20.11.201               : ok=1    changed=0    unreachable=0    failed=1
    

     7、register Loops

    执行多个task并且register给一个变量
    [ansible@hk-elk-redis1 loop]$ vim register.yaml
    ---
    - hosts: all
      gather_facts: True
      tasks:
        - name: debug loops
          shell: "{{ item }}"
          with_items:
            - date
            - hostname
            - uname
          register: num
        - name: SYSTEM INFO LOOPS----------->
          debug: msg="{% for i in num.results %} {{ i.stdout }} {% endfor %}"
    
    [ansible@hk-elk-redis1 loop]$ ansible-playbook register.yaml -l 10.20.11.201
    
    PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************
    
    TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201]
    
    TASK [debug loops] ***********************************************************************************************************************************************************************************************************************************************************
    changed: [10.20.11.201] => (item=date)
    changed: [10.20.11.201] => (item=hostname)
    changed: [10.20.11.201] => (item=uname)
    
    TASK [SYSTEM INFO LOOPS----------->] *****************************************************************************************************************************************************************************************************************************************
    ok: [10.20.11.201] => {
        "msg": " 2018年 12月 20日 星期四 15:58:14 CST  hk-elk-redis2  Linux "
    }
    
    PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
    10.20.11.201               : ok=3    changed=1    unreachable=0    failed=0
    
  • 相关阅读:
    中间键应用
    djano-cbv模式
    form表单使用(博客系统的登陆验证,注册)
    ajax-简介和实现注册登录
    django-settings.py配置
    Models-详细操作
    登陆验证系统实例-三种(cookie,session,auth)
    django-上传文件 fromdata(头像实例)
    学习笔记之CloudCompare
    学习笔记之Python 3
  • 原文地址:https://www.cnblogs.com/jcici/p/10150065.html
Copyright © 2011-2022 走看看