Ansible中的剧本 ansible--playbook

playbook格式

Usage: ansible-playbook [options] playbook.yml [playbook2 ...]

-C, --check #白跑,执行但是不会有结果
--list-hosts #列出符合的主机
-f FORKS, --forks=FORKS #做并发
--syntax-check #检查语法
-k, --ask-pass #输入密码

单个playbook

- hosts: web
  remote_user: root
  tasks:
  - name: createuser
    user: name=alex20 home=/opt/alex20 uid=4000

多个playbook

- hosts: web
  remote_user: root
  tasks:
  - name: createuser
    user: name=alex20 home=/opt/alex20 uid=4000 
  - name: copyfile
    copy: src=/etc/fstab dest=/tmp/fs

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

- hosts: web
  remote_user: root
  tasks:
  - name: createuser
    user: name=tom20 home=/opt/tom20 uid=4000
  - name: copyfile
    copy: src=/etc/fstab dest=/tmp/fs
- hosts: db
  tasks:
  - name: copyfile
    copy: src=/etc/fstab dest=/tmp/fs                         

传参

第一种方式

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

第二种方式

[web]
192.168.226.[101:102] user=alex30
192.168.226.104  user=alex100

第三种方式

[web:vars]
user=alex31

第四种方式

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

第五种传参方式

- hosts: web
  tasks:
  - name: yum
    yum: name=bc
  - name: sum
    shell: echo 11+22|bc
    register: user
  - name: echo
    shell: echo {{user.stdout}} > /tmp/echo.txt 
  - name: create{{user.stdout}}
    user: name=tom{{user.stdout}}

优先级

-e > playbook > hosts

setup

ansible_all_ipv4_addresses #所有的ipv4地址
ansible_all_ipv6_addresses #所有的ipv6地址
ansible_architecture #系统的架构
ansible_date_time #系统时间
ansible_default_ipv4 #默认的ipv4地址
    address ip地址
    alias 网卡名称
    broadcast 广播地址
    gateway 网关
    netmask 子网掩码
    network 网段
ansible_default_ipv6 #默认的ipv6地址
ansible_device_links #系统的磁盘信息
ansible_distribution #系统名称
ansible_distribution_file_variety #系统的基于公司
ansible_distribution_major_version #系统的主版本
ansible_distribution_version #系统的全部版本
ansible_dns #系统的dns 默认udp 端口53
ansible_domain #系统的域 ldap
ipv4 #ipv4地址
ansible_env #系统的环境
ansible_fqdn #系统的完整主机名
ansible_hostname #系统的简写主机名
ansible_kernel #系统的内核版本
ansible_machine #系统的架构
ansible_memtotal_mb #系统的内存
ansible_memory_mb #系统的内存使用情况
ansible_mounts #系统的挂载信息
ansible_os_family #系统家族
ansible_pkg_mgr #系统的包管理工具
ansible_processor #系统的cpu
ansible_processor_cores #每颗cpu的核数
ansible_processor_count #cpu的颗数
ansible_processor_vcpus #cpu的个数=cpu的颗数*每颗cpu的核数
ansible_python #系统python信息
ansible_python_version #系统python的版本
ansible_system #系统名字

tags

- hosts: web
  tasks:
  - name: install
    yum: name=redis
  - name: copyfile
    copy: dest=/etc/redis.conf src=/etc/redis.conf
    tags: copy
  - name: start
    service: name=redis state=started
ansible-playbook -t copy p7.yml

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

mv redis.conf{,.j2} = mv redis.conf redis.conf.j2

相对路径

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

yy 复制一行

# yy 复制多行

p 粘贴

dd 删除一行

# dd 删除多行

d$ 从当前位置删除到结尾

when

- hosts: web
  tasks:
  - name: copyfile
    copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt
    when: ansible_distribution_major_version=="7"
  - name: copyfile
    copy: content="小弦切切如私语" dest=/tmp/a.txt
    when: ansible_distribution_major_version=="6"
- hosts: web
  tasks:
  - name: copyfile
    copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt
    when: user=="4"
  - name: copyfile
    copy: content="小弦切切如私语" dest=/tmp/a.txt
    when: user=="3"

with_items

- hosts: web
  tasks:
  - name: createuser
    user: name={{item}}
    with_items:
    - tom50
    - Robert50
    - mike50
- hosts: web
  tasks:
  - name: createuser
    user: name={{item}}
    with_items:
    - alex51
    - wusir51
    - taibai51
  - name: creategroup
    group: name={{item}}
    with_items:
    - alex60
    - wusir60
    - taibai60

嵌套循环

- hosts: web
  tasks:
  - name: crateuser
    user: name={{item.name}}  group={{item.group}}
    with_items:
    - {"name":tom52,"group":tom60}
    - {"name":BOB52,"group":BOB60}
    - {"name":mike52,"group":mike60}

猜你喜欢

转载自www.cnblogs.com/robertx/p/10827768.html