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()
posted on 2016-06-22 13:48 北京涛子 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/liujitao79/p/ansible.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值