• ansible 变更内网服务器配置


    https://serversforhackers.com/tag/ansible
    http://docs.ansible.com/ansible/developing_api.html
    https://github.com/linuxdynasty/ld-ansible-modules/blob/master/test/cloud/amazon/test_kinesis_stream.py

    ansible.cfg

    [defaults]
    forks          = 20
    transport      = ssh
    host_key_checking = False
    
    [ssh_connection]
    ssh_args = -F /data/ansible/ssh_config
    pipelining = true
    

    change_link.yaml

    ---
    - hosts: proxy
      remote_user: ansible
      become: yes
      become_user: root
      become_method: sudo
      vars: 
        proxy_host: '58.215.39.12'
      
      tasks: 
       - name: update /etc/hosts
         template: src=templates/hosts.j2 dest=/etc/hosts owner=root group=root mode=0644 
         notify: restart dnsmasq
    
      handlers:
        - name: restart dnsmasq
          service:  name=dnsmasq state=restarted
    

    templates/hosts.j2

    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    
    {{ proxy_host }} proxy.live
    

    ansible-playbook -i inventory change_link.yaml

    PLAY ***************************************************************************
    
    TASK [setup] *******************************************************************
    ok: [10.150.1.51]
    ok: [10.150.1.52]
    
    TASK [update /etc/hosts] *******************************************************
    changed: [10.150.1.51]
    changed: [10.150.1.52]
    
    RUNNING HANDLER [restart dnsmasq] **********************************************
    changed: [10.150.1.51]
    changed: [10.150.1.52]
    
    PLAY RECAP *********************************************************************
    10.150.1.51                : ok=3    changed=2    unreachable=0    failed=0   
    10.150.1.52                : ok=3    changed=2    unreachable=0    failed=0
    

    demo

    from collections import namedtuple
    from ansible.parsing.dataloader import DataLoader
    from ansible.vars import VariableManager
    from ansible.inventory import Inventory
    from ansible.playbook.play import Play
    from ansible.executor.task_queue_manager import TaskQueueManager
    
    Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check'])
    # initialize needed objects
    variable_manager = VariableManager()
    loader = DataLoader()
    options = Options(connection='local', module_path='/path/to/mymodules', forks=100, become=None, become_method=None, become_user=None, check=False)
    passwords = dict(vault_pass='secret')
    
    # create inventory and pass to var manager
    inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='localhost')
    variable_manager.set_inventory(inventory)
    
    # create play with tasks
    play_source =  dict(
            name = "Ansible Play",
            hosts = 'localhost',
            gather_facts = 'no',
            tasks = [
                dict(action=dict(module='shell', args='ls'), register='shell_out'),
                dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))
             ]
        )
    play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
    
    # actually run it
    tqm = None
    try:
        tqm = TaskQueueManager(
                  inventory=inventory,
                  variable_manager=variable_manager,
                  loader=loader,
                  options=options,
                  passwords=passwords,
                  stdout_callback='default',
              )
        result = tqm.run(play)
    finally:
        if tqm is not None:
            tqm.cleanup()
    
  • 相关阅读:
    学习Python的一些Tips
    读书笔记《深入理解计算机系统》(第三版) 第二章 信息的表示
    读书笔记《深入理解计算机系统》(第三版) 第一章 计算机系统漫游
    C 标准库系列之locale.h
    C 标准库系列之limits.h
    C 标准库系列之float.h
    C 标准库系列之errno.h
    C 标准库系列之ctype.h
    C 标准库系列之assert.h
    C 标准库系列之概述
  • 原文地址:https://www.cnblogs.com/liujitao79/p/ansible.html
Copyright © 2020-2023  润新知