ansible入门
来源:互联网 发布:mac pro机箱 编辑:程序博客网 时间:2024/05/17 23:31
安装
直接apt-get安装版本太低
apt-get install ansible
root@pca-test:~# ansible --versionansible 1.5.4root@pca-test:~#
我们使用源码安装
sudo apt-get install python-pipsudo apt-get install python-dev libffi-dev libssl-dev libyaml-dev sshpass -ysudo pip install paramiko PyYAML Jinja2 httplib2 six --upgradesudo pip install --upgrade setuptoolsgit clone git://github.com/ansible/ansible.git --recursive cd ansiblegit pull --rebase git submodule update --init --recursivesudo make install
报错…
[root@ansible_01 ~]# ansible -vTraceback (most recent call last): File "/usr/bin/ansible", line 36, in <module> from ansible.runner import RunnerImportError: No module named ansible.runner
执行这个~
source hacking/env-setup
修改配置
直接安装完成后没有/etc/ansible目录
mkdir /etc/ansiblecp examples/ansible.cfg /etc/ansible/cp examples/hosts /etc/ansible/
加入几个host
root@pca-test:/etc/ansible# grep -Ev "^$|#" /etc/ansible/hosts [pca]192.168.11.81 ansible_ssh_pass=123456[oat-server]192.168.11.82 ansible_ssh_pass=123456[oat-client]192.168.11.83 ansible_ssh_pass=123456root@pca-test:/etc/ansible#
设置为首次链接不需要key认证
sed -i "s/^#\(host_key_checking\).*/\1 = False/g" /etc/ansible/ansible.cfg
测试
利用ping测试
root@pca-test:~/workspace/ansible# ansible all -m pingSSH password: 192.168.11.81 | SUCCESS => { "changed": false, "ping": "pong"}192.168.11.82 | SUCCESS => { "changed": false, "ping": "pong"}192.168.11.83 | SUCCESS => { "changed": false, "ping": "pong"}root@pca-test:~/workspace/ansible#
将salt-stack转变为ansible
ansible模块可以从这里看到:http://docs.ansible.com/ansible/list_of_files_modules.html
我们来尝试把之前用salt-stack写的代码改成ansible
原saltstack代码
之前用salt-stack写的pre.sls如下:
{# for /etc/hosts #}/etc/hosts: file.managed: - source: salt://files/hosts - user: root - group: root - mode: 644 cmd.run: - name: sed -i 's/^\(127.0.1.1\).*$/\1\t{{host}}/g' /etc/hosts/etc/apt/sources.list: file.managed: - source: salt://files/local_sources.list - user: root - group: root - mode: 644/etc/apt/sources.list.bak: file.managed: - source: salt://files/ubuntu/sources.list - user: root - group: root - mode: 644install_base_pkg: cmd.run: - name: | sed -i "s/MODIFIED/{{conf.admin_host}}/g" /etc/apt/sources.list apt-get update -y - require: - file: /etc/apt/sources.list
ansible代码
目录结构如下:
root@pca-test:~/workspace/test# tree .├── group_vars│ └── bootstrap.json├── roles│ └── pre│ ├── files│ ├── handlers│ │ └── main.yml│ ├── tasks│ │ └── main.yml│ └── templates│ ├── hosts│ └── ubuntu│ ├── sources.list│ └── sources.list.local├── site.retry└── site.yml8 directories, 8 filesroot@pca-test:~/workspace/test#
相关文件内容:
root@pca-test:~/workspace/test# cat site.yml ---# pre for deploying OAT- hosts: all remote_user: root # become: activate privilege escalation become: yes # overrides the default method set in ansible.cfg, set to sudo/su/pbrun/pfexec/doas/dzdo/ksu become_method: sudo # invoke setup module to gather facts before executing tasks gather_facts: true vars_files: - group_vars/bootstrap.json roles: - preroot@pca-test:~/workspace/test# cat roles/pre/handlers/main.yml ---- name: update sources #command: apt-get update -y debug: msg="handlers update sources is triggered.."root@pca-test:~/workspace/test# cat roles/pre/tasks/main.yml---- name: copy files template: src: "{{item.src}}" dest: "{{item.dest}}" owner: root group: root mode: 0644 with_items: - {src: "hosts", dest: "/etc/hosts"} - {src: "ubuntu/sources.list.local", dest: "/etc/apt/sources.list"} - {src: "ubuntu/sources.list", dest: "/etc/apt/sources.list.bak"}- name: debug debug: msg="admin={{base.admin_host}}, myhostname={{ansible_hostname}}"- name: modify parameters for hosts command: sed -i 's/^\(127.0.1.1\).*$/\1\t{{ansible_hostname}}/g' /etc/hosts- name: modify parameters for sources.list command: sed -i "s/MODIFIED/{{base.admin_host}}/g" /etc/apt/sources.list notify: update sourcesroot@pca-test:~/workspace/test#
目前代码只为测试,最后应当使用template来代替sed
执行结果:
root@pca-test:~/workspace/test# ansible-playbook site.yml PLAY [all] ****************************************************************************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************************************************************ok: [192.168.11.81]ok: [192.168.11.82]ok: [192.168.11.83]TASK [pre : copy files] ***************************************************************************************************************************************************************changed: [192.168.11.82] => (item={u'dest': u'/etc/hosts', u'src': u'hosts'})changed: [192.168.11.81] => (item={u'dest': u'/etc/hosts', u'src': u'hosts'})changed: [192.168.11.83] => (item={u'dest': u'/etc/hosts', u'src': u'hosts'})ok: [192.168.11.81] => (item={u'dest': u'/etc/apt/sources.list', u'src': u'ubuntu/sources.list.local'})ok: [192.168.11.82] => (item={u'dest': u'/etc/apt/sources.list', u'src': u'ubuntu/sources.list.local'})ok: [192.168.11.81] => (item={u'dest': u'/etc/apt/sources.list.bak', u'src': u'ubuntu/sources.list'})ok: [192.168.11.82] => (item={u'dest': u'/etc/apt/sources.list.bak', u'src': u'ubuntu/sources.list'})ok: [192.168.11.83] => (item={u'dest': u'/etc/apt/sources.list', u'src': u'ubuntu/sources.list.local'})ok: [192.168.11.83] => (item={u'dest': u'/etc/apt/sources.list.bak', u'src': u'ubuntu/sources.list'})TASK [pre : debug] ********************************************************************************************************************************************************************ok: [192.168.11.81] => { "msg": "admin=pca-test, myhostname=pca-test"}ok: [192.168.11.83] => { "msg": "admin=pca-test, myhostname=vtpm"}ok: [192.168.11.82] => { "msg": "admin=pca-test, myhostname=oat-test"}TASK [pre : modify parameters for hosts] **********************************************************************************************************************************************changed: [192.168.11.81] [WARNING]: Consider using template or lineinfile module rather than running sedchanged: [192.168.11.83]changed: [192.168.11.82]TASK [pre : modify parameters for sources.list] ***************************************************************************************************************************************changed: [192.168.11.81]changed: [192.168.11.83]changed: [192.168.11.82]RUNNING HANDLER [pre : update sources] ************************************************************************************************************************************************ok: [192.168.11.81] => { "msg": "handlers update sources is triggered.."}ok: [192.168.11.83] => { "msg": "handlers update sources is triggered.."}ok: [192.168.11.82] => { "msg": "handlers update sources is triggered.."}PLAY RECAP ****************************************************************************************************************************************************************************192.168.11.81 : ok=6 changed=3 unreachable=0 failed=0 192.168.11.82 : ok=6 changed=3 unreachable=0 failed=0 192.168.11.83 : ok=6 changed=3 unreachable=0 failed=0 root@pca-test:~/workspace/test#
0 0
- ansible入门
- ansible入门
- ansible入门
- ansible入门
- ansible入门
- Ansible入门简介
- ansible的简单入门
- ansible之playbook入门
- Ansible快速入门
- Ansible入门基础
- Ansible工具入门
- ansible基础入门
- Ansible 基础入门-Playbook
- ansible
- ansible
- Ansible
- Ansible
- ansible
- [Qt creator+Linux]安装UUID(libuuid)以及问题undefined reference to uuid generate
- EasyUI学习之DataGird
- Heritrix3.1.1使用教程
- 连接redis报此错误:ERR Client sent AUTH, but no password is set
- 如何选择Python版本2还是3
- ansible入门
- PDO
- linux centos 7安装极点五笔输入法
- 新华书店的末落
- android 不能在子线程中更新ui的讨论和分析
- 【学习笔记之Openlayers3】补充篇之鹰眼控件(OverviewMap)
- 第一章 整洁的代码
- Linux脚本中用户自定义终止符-EOF
- 发布wcf服务元数据