playbook剧本

有关yaml的文件介绍

- 字典  key:value
- 列表 [] \-
- 后缀名:yaml、yml

  

playbook 命令格式

Usage: ansible-playbook [options] playbook.yml [playbook2 ...]
-C, --check # 检查但是不会真的执行
-f FORKS, --forks=FORKS # 并发,默认是5个
 --list-hosts #列出匹配的主机
 --syntax-check # 检查语法

  

第一个playbook

- hosts: web
  remote_user: root
  tasks:
  - name: copyfile
    copy: src=/etc/fstab dest=/tmp/fs 

  

注意:顺序执行,第一个任务所有机器都执行完,才会执行第二个任务

多任务

- hosts: web
  remote_user: root
  tasks:
  - name: copyfile
    copy: src=/etc/fstab dest=/tmp/fs 
  - name: createuser
    user: name=alex11

 

幂等性 ,不管执行多少次,等到的结果永远是一样的

ansible-playbook hosts.yml --tags="only"    //去被管理主机上查看文件创建情况 2个tags都会执行

  

五种传参的方式

第一种方式

- hosts: web
  tasks:
  - name: create{{user}}
    user: name={{user}}
ansible-playbook -e user=alex13 p3.yml

  

第二种方式

10.0.0.[132:133] user=alex14
10.0.0.135  user=alex12
ansible-playbook p3.yml

  

第三种方式

[web:vars]
user=alex15
ansible-playbook p3.yml

  

第四种方式

- hosts: web
  vars:
  - user: alex16
  tasks:
  - name: create{{user}}
    user: name={{user}}

  

第五种传参方式

- hosts: web
  tasks:
  - name: yumbc
    yum: name=bc
  - name: sum
    shell: echo 8+9|bc
    register: user
  - name: echo
    shell: echo {{user.stdout}} >/tmp/sum.txt
  - name: createuser{{user.stdout}}
    user: name=alex{{user.stdout}}

  

优先级

-e > playbook的vars > hosts文件

 

setup(获取系统的参数)

ansible_all_ipv4_addresses # 所有的ipv4地址
ansible_all_ipv6_addresses # 所有的ipv6的地址
ansible_bios_version # 主板bios的版本
ansible_architecture # 架构信息
ansible_date_time # 系统的时间
ansible_default_ipv4 # IPv4默认地址
	address #ip地址
	alias #网卡名称
	broadcast #广播地址
	gateway # 网关
	macaddress #mac地址
	netmask #子网掩码
	network #网段
ansible_distribution #系统的版本
ansible_distribution_file_variety# 系统的基于对象
ansible_distribution_major_version# 系统的主版本
ansible_distribution_version #系统的版本
ansible_domain #系统的域
ansible_dns #系统的dns
ansible_env #系统的环境变量
ansible_hostname #系统的主机名
ansible_fqdn #系统的完整主机名
ansible_machine #系统的架构
ansible_memory_mb #系统的内存信息
ansible_os_family #系统的家族
ansible_pkg_mgr #系统的包管理工具
ansible_processor_cores #cpu的核数
ansible_processor_count #每颗cpu上的颗数
ansible_processor_vcpus #cpu的总核数=cpu的颗数*每颗cpu上的核数
ansible_python #系统的python版本
ansible 10.0.0.132 -m setup -a "filter=*processor*" 搜索

  

补充正则

. 匹配任何字符,出了换行

\* 任何次

\+  最少一次

? 最多一次

{m} 指定的m次

{m,n} m次到n次

{m,} 最少m次

{0,n} 最多n次

[0-9] 代表数字

^ 开头

$ 结尾

^ 取反

[^0-9]

  

()分组

grep "^\(.*\):.*\1$" /etc/passwd
grep -E "^(.*):.*\1$" /etc/passwd

 

yum -h 先查看包

在找到search进行安装

再通过search进行查找需要的包在哪个文件里边

yum search 需要安装的包名

  

tags(标签)

- hosts: web
  tasks:
  - name: install
    yum: name=redis
  - name: copyfile
    copy: dest=/etc/redis.conf src=/etc/redis.conf
    tags: copyfile
  - name: start
    service: name=redis state=started 
 ansible-playbook -t copyfile p7.yml 
 文件给拷贝过去了,但是运行的文件还是之前的文件,所以,要进行远程的重新启动redis数据库

 

handlers

- hosts: web
  tasks:
  - name: install
    yum: name=redis
  - name: copyfile
    copy: dest=/etc/redis.conf src=/etc/redis.conf
    tags: copy
    notify: restart
  - name: start
    service: name=redis state=started
  handlers:
  - name: restart
    service: name=redis state=restarted

 

template

- hosts: web
  tasks:
  - name: install
    yum: name=redis
  - name: copyfile
    template: dest=/etc/redis.conf src=/etc/redis.conf
    tags: copy
    notify: restart
  - name: start
    service: name=redis state=started
  handlers:
  - name: restart
    service: name=redis state=restarted

  

- hosts: web
  tasks:
  - name: install
    yum: name=redis
  - name: copyfile
    template: dest=/etc/redis.conf src=redis.conf
    tags: copy
    notify: restart
  - name: start
    service: name=redis state=started
  handlers:
  - name: restart
    service: name=redis state=restarted
需要在本地的目录下创建一个templates目录,就可以用相对路径

  

补充关于vi命令的常用快捷命令操作

u 撤销

p 粘贴

#yy 复制#行

d$ 从当前位置删除

o 当前位置下面增加空白行,并切换到编辑模式

r 替换

  

when

- hosts: web
  tasks:
  - name: file
    copy: content="大弦嘈嘈如急雨" dest=/opt/file
    when: ansible_distribution_major_version=="7"
  - name: file
    copy: content="小弦切切如私语" dest=/opt/file
    when: ansible_distribution_major_version=="6"

  

- hosts: web
  tasks:
  - name: file
    copy: content="大弦嘈嘈如急雨\n" dest=/opt/file
    when: sum=="7"
  - name: file
    copy: content="小弦切切如私语\n" dest=/opt/file
    when: sum=="6"
ansible-playbook -e sum=7 p11.yml

 

循环

- hosts: web
  tasks:
  - name: file
    user: name={{item}}
    with_items:
    - alex20
    - alex21


- hosts: web
  tasks:
  - name: creategroup
    group: name={{item}}
    with_items:
    - wusir20
    - wusir21
  - name: file
    user: name={{item}}
    with_items:
    - alex22
    - alex23

  

嵌套循环

- hosts: web
  tasks:
  - name: creategroup
    group: name={{item}}
    with_items:
    - wusir22
    - wusir23
  - name: file
    user: name={{item.name}} group={{item.group}}
    with_items:
    - {"name":alex24,"group":wusir22}
    - {"name":alex25,"group":wusir23}

  

Ad-hoc 直接在命令行执行

在命令行执行的命令称作Ad-hoc

  

 

 

 

 

 

 

 

 

 

  

 

 

 

 

posted @ 2019-04-13 14:53  7411  阅读(99)  评论(0)    收藏  举报