ansible-playbook [options] playbook.yml [playbook2 ...] -C, --check # 检查,白跑,干跑 -f FORKS, --forks=FORKS #用来做并发 --list-hosts # 列出主机列表 --syntax-check # 语法检查
- 简单用法
- hosts: web tasks: - name: creategroup group: name=jamlee1 - name: cretaeuser user: name=jam1
- 传参
- hosts: web tasks: - name: create{{ user }} user: name={{ user}}
- 方法一
ansible-playbook -e 'user=jamlee1' p1.yml
- 方法二 (host文件)
vi /etc/ansible/hosts [db] 192.168.33.131 user=jam1 192.168.107.132 user=jam2
- 方法三 (host文件)
[db:vars] #表示组的参数 user=jam2
- 方法四
- hosts: db vars: - user: jam3 tasks: - name: create{{ user }} user: name={{ user}}
- 方法五
- hosts: db tasks: - name: sum shell: echo 7+8|bc register: user - name: createuser user: name={{user.stdout}}
传参方式的优先级
-e > playbook vars > hosts文件
- setup 模块用于收集远程主机的一些基本信息。
ansible cache -m setup

ansible_all_ipv4_addresses # ipv4的所有地址 ansible_all_ipv6_addresses # ipv6的所有地址 ansible_date_time # 获取到控制节点时间 ansible_default_ipv4 # 默认的ipv4地址 ansible_distribution # 系统 ansible_distribution_major_version # 系统的大版本 ansible_distribution_version # 系统的版本号 ansible_domain #系统所在的域 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 cache -m setup -a 'filter=*processor*' # 用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息。 用到了正则
# 正则小结 * 匹配数量,表示0或者多次 ? 匹配数量,表示0或者1次 . 除换行符以外的所有字符 + 至少一次 [123abc] 匹配内容,or () 分组 {m} 次数,出现m次 {m,} 至少m次 {m,n}出现m-n次
- 条件判断
- when
- 不同的系统,版本,环境,用户
- hosts: db remote_user: root tasks: - name: createfile copy: content="抬头望明月" dest=/tmp/a.txt when: a=="3" - name: cratefile copy: content="低头思故乡" dest=/tmp/a.txt when: a=="4"
ansible-playbook -e 'a="3"' p6.yml
Ubuntu 安装包的方式是apt-get
-
- hosts: web tasks: - name: installnginx yum: name=nginx - name: copyfile copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf tags: copyfile - name: start service: name=nginx state=started
ansible-playbook -t copyfile p7.yml
-
- hosts: web tasks: - name: crateuser user: name={{item}} with_items: - jam11 - jam22 - jam33
- 嵌套循环
- hosts: web tasks: - name: crategroup group: name={{item}} with_items: - canglaoshi30 - canglaoshi31 - canglaoshi32 - name: createuser user: name={{item.name}} group={{item.group}} with_items: - {'name':jam40,'group':canglaoshi30} - {'name':jam41,'group':canglaoshi31} - {'name':jam42,'group':canglaoshi32}
- template:
- 根据模块文件动态生成对应的配置文件
- hosts: web tasks: - name: installredis yum: name=redis - name: copyfile template: src=/etc/redis.conf dest=/etc/redis.conf - name: start service: name=redis state=started vi /etc/redis.conf 配置文件: bind {{ ansible_default_ipv4.address }}
copy和tamplate的区别 - copy模块不替代参数 - template模块替代参数
- 相对路径: 在当前目录下新建一个templates目录,然后把文件放在templates目录里面
- hosts: web tasks: - name: installredis yum: name=redis - name: copyfile template: src=redis.conf dest=/etc/redis.conf - name: start service: name=redis state=started
- handlers
- 修改配置文件
- hosts: web tasks: - name: installredis yum: name=redis - name: copyfile template: src=redis.conf dest=/etc/redis.conf tags: copyfile notify: restart - name: start service: name=redis state=started handlers: - name: restart service: name=redis state=restarted
templates文件必须存放于templates目录下,且命名为 .j2 结尾 yaml/yml 文件需和templates目录平级,目录结构如下: ./ ├── temnginx.yml └── templates └── nginx.conf.j2