ansible应用
Posted 晨东linux
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ansible应用相关的知识,希望对你有一定的参考价值。
ansible-playbook的基本组件——杂记
定义执行主机和执行用户
- hosts: webserver
remote_user: root
sudu: yes
——————————————
tasks列表,Play的主体部分是task列表,task列表中的各任务按次序逐个在hosts中指定的主机上执行,即在所有主机上完成第一个任务后再开始第二个任务。Task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量,
- name: ensure apache is at the latest version
yum: name=httpd state=latest(见ansible模块参数)
执行时shell和command模块时忽略错误信息,防止剧本被打断。
tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand
ignore_errors: True
————————————————
定义变量和模版
- hosts: test
vars:
- package: httpd
vars固定格式,package变量名,httpd变量值
变量名支持字母数字以及下划线,变量名须字母开头。
调用方法
tasks:
- name: ensure apache is at the latest version
yum: name={{ package }} state=latest
在task或template中引用变量都是使用双花括号中间引入变量名即可,如:{{ VAR_NAME }}。
————————————————
handlers
发生改变时执行提前定义好的操作
command执行命令
command: shell命令
————————————————
lineinfile
文件操作
匹配行修改
- name: seline modify enforcing
lineinfile:
dest: /etc/selinux/config
regexp: '^SELINUX='
line: 'SELINUX=enforcing'
匹配上上方插入
- name: httpd.conf modify 8080
lineinfile:
dest: /opt/playbook/test/http.conf
regexp: '^Listen'
insertbefore: '^#Port'
line: 'Listen 8080'
匹配行下方插入
- name: httpd.conf modify 8080
lineinfile:
dest: /opt/playbook/test/http.conf
regexp: '^Listen'
insertafter: '^#Port'
line: 'Listen 8080'
删除某行
- name: delete 192.168.1.1
lineinfile:
dest: /opt/playbook/test/hosts
state: absent
regexp: '^192\.'
末行添加
name: add a line
lineinfile:
dest: /opt/playbook/test/hosts
line: '192.168.1.2 foo.lab.net foo'
Register
将命令执行结果存储到变量中
tasks:
- name: retrieve the list of home directories
command: ls /home
register: home_dirs
- name: add home dirs to the backup spooler
file: path=/mnt/bkspool/{{ item }} src=/home/{{ item }} state=link
with_items: home_dirs.stdout_lines
应用实例:
ansible-playbook的应用实例
mkdir /root/test/
vim /root/test/test1.yaml
添加:
---
- hosts: 39.107.103.225 // 第一个任务
gather_facts: False //不读取对方主机的信息
tags: //主机标签一
- play1
tasks:
- name: create user
user: name={{ item }}
with_items:
- lisi
- zhangsan
- wangwu
- hosts: 39.107.103.225 //第二个任务
tags: //第二个主机标签
- play2
tasks:
- name: create directory
file: path=/root/{{ item.a1 }} owner={{ item.a2 }} group={{ item.a3 }} mode={{ item.a4 }} state=directory
with_items:
- {a1: "benet",a2: "zhangsan",a3: "lisi",a4: "0700"}
- {a1: "accp",a2: "wangwu",a3: "zhangsan",a4: "0755"}
- {a1: "yun",a2: "lisi",a3: "root",a4: "0777"}
- hosts: 39.107.103.225 //第三个任务
tags: //第三个主机标签
- play3
tasks:
- name: web stie
file: path=/root/benet/{{ item[0] }}/{{ item[1] }} state=directory
with_nested:
- ['benetcom','accpcom','yuncom']
- ['xixi','haha','lala']
- hosts: 39.107.103.225 //第四个任务
tags: //第四个主机标签
- play4
tasks:
- name: ipv4
lineinfile:
dest: /root/accp/lpc.conf
line: "{{ item }}"
with_items:
- "net.ipv4.conf.all.send_redirects = 0"
- "net.ipv4.conf.default.send_redirects = 0"
- "net.ipv4.conf.eth0.send_redirects = 0"
- hosts: 39.107.103.225 //第五个任务
tags: //第五个主机标签
- play5
tasks:
- name: copy txt
copy: src={{ item }} dest=/root/accp/
with_fileglob:
- /root/*.txt
- hosts: 39.107.103.225 //第六个任务
tags: //第六个主机标签
- play6
tasks:
- name: redhat vim
yum: name=vim-enhanced state=installed
when: ansible_os_family == "RedHat"
- name: debian
apt: name=vim state=installed
when: ansible_os_family == "Debian"
- hosts: 39.107.103.225 //第七个任务
tags: //第七个主机标签
- play7
tasks:
- name: panduan
command: echo {{ item }}
with_items: [ 0,2,4,6,8,10 ]
when: item < 5
执行这个命令:
ansible-playbook /root/test/test1.yaml
以上是关于ansible应用的主要内容,如果未能解决你的问题,请参考以下文章
使用Ansible Galaxy 下载Ansible应用案例