运维自动化 第五章 playbook 模块补充

 13.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 #系统名字

 14.template(可以动态的传递参数,在setup获取)

  绝对路径:

- 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

 15.replace用来做替换,支持正则

yaml规范 yaml链接

字典 key:value
列表 :[] -
注: 如果写列表和字典格式必须为- name: alex
还有如果是同级的情况,首行缩进必须是一致的;
冒号后面必须有空格
横线后面必须有空格
严格保持对齐
等号前面不能有空格

playbook格式

用法:

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=alex20 home=/opt/alex20 uid=4000
 - name:copyfile
   copy:src=/etc/fstab dest=/tmp/fs
-hosts :db
 tasks:
 - name: copyfile
   copy: src=/etc/fstab dest=/tmp/fs

传参:

   -e 
   host文件ip地址后面写
   playbook vars
   register  获取值stdout

传参的五种方式

  第一种:

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

ansible-playbook -e user=wusir20 p3.yml

   第二种:

在web组中写

[web]
192.168.100.[1:3]  user=alex30
192.168.100.6    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=alex{{user.stdout}}

 优先级:

-e > playbook > hosts

 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(被触发的任务,notify获取)

- 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 sate=started
  handlers:
    - name :restart
      service :name =redis state=restarted

 mv redis.conf{,.j2} = mv 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的目录,就可以使用相对路径

when (判断 ,当满足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'

当centos版本为7的时候,执行上面那句话; 否则,执行下面这句话

 with_items(循环)

- hosts : web
  tasks:
  - name : createuser
    user: name={{item}}
    with_items:
    - alex50
    - wusir50
    - taibai50


- 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 : createuser
    user : name={{item.name}}  group={{item.group}}
    with_items:
    - {'name':alex52, 'group': alex60}
    - {'name':wusir52, 'group':wusir52}
    - {'name': taibai52, 'group':taibai52}

  

原文地址:https://www.cnblogs.com/zty1304368100/p/10827490.html