inventory主机清单管理与playbook剧本

1 inventory主机清单管理

Inventory支持对主机进行分组,每个组内可以定义多个主机,每个主机都可以定义在任何一个或多个主机组内。

如果是名称类似的主机,可以使用列表的方式标识各个主机。
vim /etc/ansible/hosts
[webservers]
192.168.80.11:2222        #冒号后定义远程连接端口,默认是 ssh 的 22 端口
192.168.80.1[2:5]

[dbservers]
db-[a:f].example.org    #支持匹配 a~f


inventory 中的变量

Inventory变量名                  含义
ansible_host                  ansible连接节点时的IP地址
ansible_port                  连接对方的端口号,ssh连接时默认为22
ansible_user                  连接对方主机时使用的用户名。不指定时,将使用执行ansible或ansible-playbook命令的用户
ansible_password              连接时的用户的ssh密码,仅在未使用密钥对验证的情况下有效
ansible_ssh_private_key_file  指定密钥认证ssh连接时的私钥文件
ansible_ssh_common_args       提供给ssh、sftp、scp命令的额外参数
ansible_become                允许进行权限提升
ansible_become_method         指定提升权限的方式,例如可使用sudo/su/runas等方式
ansible_become_user           提升为哪个用户的权限,默认提升为root
ansible_become_password       提升为指定用户权限时的密码

(1)主机变量
[webservers]
192.168.80.11 ansible_port=22 ansible_user=root ansible_password=abc1234

(2)组变量
[webservers:vars]            #表示为 webservers 组内所有主机定义变量
ansible_user=root
ansible_password=abc1234

[all:vars]                    #表示为所有组内的所有主机定义变量
ansible_port=22

(3)组嵌套
[nginx]
192.168.80.20
192.168.80.21
192.168.80.22

[apache]
192.168.80.3[0:3]

[webs:children]        #表示为 webs 主机组中包含了 nginx 组和 apache 组内的所有主机
nginx
apache


2 playbooks 剧本组成

(1)Tasks:任务,即通过 task 调用 ansible 的模块将多个操作组织在一个 playbook 中运行
(2)Variables:变量
(3)Templates:模板
(4)Handlers:处理器,当changed状态条件满足时,(notify)触发执行的操作
(5)Roles:角色
 

3 playbook剧本演练

1 下载httpd

创建xxx.yaml格式文件

cd /etc/yum.repos.d/     #制作本地yum源
cd /etc/ansible/playbook/    #将修改后的httpd.conf文件复制到当前目录中

vim /etc/ansible/playbook/play1.yaml 

- name: the first play for install apache
  gather_facts: false
  hosts: dbservers
  remote_user: root
  tasks:
  - name: disable firewalld
    service: name=firewalld state=stopped enabled=no
  - name: disable selinux
    command: '/usr/sbin/setenforce 0'
    ignore_errors: true
  - name: disable selinux forever
    replace: path=/etc/selinux/config  regexp="enforcing"  replace="disabled"
  - name: mount cdrom
    mount: src=/dev/sr0 path=/mnt fstype=iso9660 state=mounted
  - name: copy local yum configuration file
    copy: src=/etc/yum.repos.d/repo.bak/local.repo  dest=/etc/yum.repos.d/local.repo
  - name: install apache
    yum: name=httpd state=latest
  - name: prepare httpd configuration file
    copy: src=/etc/ansible/playbook/httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify: "reload httpd"
  - name: start apache
    service: name=httpd state=started enabled=yes

  handlers:
  - name: reload httpd
    service: name=httpd state=reloaded

//运行playbook
ansible-playbook test1.yaml
//补充参数:
-k(–ask-pass):用来交互输入ssh密码
-K(-ask-become-pass):用来交互输入sudo密码
-u:指定用户
ansible-playbook test1.yaml --syntax-check    #检查yaml文件的语法是否正确
ansible-playbook test1.yaml --list-task       #检查tasks任务
ansible-playbook test1.yaml --list-hosts      #检查生效的主机
ansible-playbook test1.yaml --start-at-task='install httpd'     #指定从某个task开始运行

定义引用变量

- name: second play
  hosts: dbservers
  remote_user: root
  vars:                 #定义变量
   - groupname: mysql   #格式为 key: value
   - username: nginx
  tasks:
   - name: create group
     group: name={{groupname}} system=yes gid=306    #使用 {{key}} 引用变量的值
   - name: create user
     user: name={{username}} uid=306 group={{groupname}} 
   - name: copy file
     copy: content="{{ansible_default_ipv4}}" dest=/opt/vars.txt    #在setup模块中可以获取facts变量信息


ansible-playbook test1.yaml -e "username=nginx"     #在命令行里定义变量

2 使用sudo命令将远程主机的普通用户提升为root用户

vim /etc/ansible/playbook/play2.yaml 

- name: second play
  hosts: webservers
  remote_user: clr
  become: yes
  become_user: root
  vars:
  - username: zhu
  - groupname: feng
  - filename: /opt/123.txt
  gather_facts: true
  tasks:
  - name: create group
    group: name={{groupname}}  gid=2800
  - name: create user join group
    user: name={{username}} uid={{uid}} groups={{groupname}}
  - name: copy file
    copy: content="{{ansible_default_ipv4.address}}" dest={{filename}}
  - name: modify username and groupname of file
    file: path={{filename}} owner={{username}}  group={{groupname}}

3 when条件判断指定ip地址

vim /etc/ansible/playbook/play3.yaml 

- name: third play
  hosts: wenservers
  remote_user: root
  tasks:
  - name: touch file
    file: path=/opt/web.txt state=touch
    #when: ansible_default_ipv4.address != "192.168.80.105"
    when: inventory_hostname == "192.168.80.80"

4 循环迭代with_items创建目录文件

vim /etc/ansible/playbook/play4.yaml 

- name: fouth play
  hosts: webservers
  remote_user: root
  vars:
    myfile:
    - /opt/a
    - /opt/b
    - /opt/c
    - /opt/d
  tasks:
  - name: touch directory
    with_items: "{{myfile}}"
    file: path={{item}} state=directory

  - name: touch file
    with_items:
    - /root/a
    - /root/b
    - /root/c
    - /root/d
    file:
      path: "{{item}}"
      state: touch

  4  playbook剧本格式

vim  XXX.yaml
- name:                      #指定play名称
  hosts:                     #指定主机组
  remote_user:               #执行用户 
  gather_facts: true|false   #是否收集远程主机facts信息
  vars:                      #定义变量
  tasks:                     #定义task任务列表
  - name:                 #定义task任务名称
    模块:                 #定义任务使用的模块和参数
    with_items:           #定义循环列表
    when:                 #定义判断条件(== != >= > <= <),true则执行任务,否则不执行任务
	ignore_errors: true   #忽略任务失败
    notify:               #定义task任务changed状态时触发的任务名
    tags:                 #指定标签,ansible-playbook --tags 仅执行拥有指定 tags 标签的任务(always标签总会执行)
  handlers:                  #定义notify触发的任务列表

task任务模块语法

横向格式:
模块名: 参数选项1=值  参数选项2={{变量名}}  ...

纵向格式:
模块名:
  参数选项1: 值
  参数选项2: "{{变量名}}"
  ...

with_items迭代变量语法

横向格式:
with_items: ["值1", "值2", "值3"]

值为对象(键值对字段)时:
with_items:
- {key1: value1, key2: value2, ...}
- {key1: value3, key2: value4, ...}

纵向格式:
with_items:
- 值1
- 值2
- 值3

值为对象(键值对字段)时:
with_items:
- key1: value1
  key2: value2
- key1: value3
  key2: value4

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值