Ansible 2.3:ansible-playbook 命令的一般用法

2.3:ansible-playbook 命令的一般用法

ansible-paybook是运行Playbook文件的命令,用于在目标主机上(由Playbook中的hosts指定)执行Playbook中定义的任务(由Playbook中的tasks定义)。

一般用法为:

ansible-playbook [OPTIONS] <PLAYBOOK.yml>

2.3.1:OPTIONS

2.3.1.1:–syntax-check 检查语法

用于检查指定的Playbook文件是否有语法错误(可以简写为--syntax

--syntax-check
          perform a syntax check on the playbook, but do not execute it

示例:

#无语法错误
[root@ansible ~]# ansible-playbook --syntax-check debug.yml 

playbook: debug.yml
#故意改错(将name:后的空格删除)
[root@ansible ~]# vim debug.yml 
---
- hosts: 192.168.1.*
  tasks:
  - name:debug module test
    debug:
      msg: IpAddr of Host {{ ansible_nodename }} is {{ ansible_default_ipv4.address }}
...
#再次检查
[root@ansible ~]# ansible-playbook --syntax debug.yml       

在这里插入图片描述

2.3.1.2:-C 模拟执行

加上-C选项后,不做任何实际的操作,只是将预测的任务执行过程打印出来,检查可能出现的错误。

 -C, --check
          don't make any changes; instead, try to predict some of the changes that may occur

示例:

#编写一个删除fstab文件的playbook
[root@ansible ~]# vim file_absent.yml
---
- hosts: websrvs
  remote_user: root

  tasks:
    - name: remove fstab
      file: name=/etc/fstab state=absent
...
#模拟执行
[root@ansible ~]# ansible-playbook -C file_absent.yml                      

在这里插入图片描述

#目标主机上查看,fstab文件还存在
root@node111:~# ll /etc/fstab
-rw-r--r-- 1 root root 550 Nov 26 10:51 /etc/fstab
2.3.1.3:–list-hosts 列出主机列表

列出Playbook中hosts指定的HOST_PATTER匹配到的主机。

 --list-hosts
          outputs a list of matching hosts; does not execute anything else

示例(列出debug.yml中所涉及的目标主机):

#playbook内容
[root@ansible ~]# cat debug.yml 
---
- hosts: websrvs:testsrvs
  tasks:
  - name: debug module test
    debug:
      msg: IpAddr of Host {{ ansible_nodename }} is {{ ansible_default_ipv4.address }}
...

#列出Playbook匹配到的主机
[root@ansible ~]# ansible-playbook --list-hosts debug.yml 

playbook: debug.yml

  play #1 (websrvs:testsrvs): websrvs:testsrvs  TAGS: []
    pattern: [u'websrvs:testsrvs']
    hosts (2):
      192.168.1.113
      192.168.1.111
2.3.1.4:–list-tags 列出标签

列出Playbook中包含的任务标签。

--list-tags
          list all available tags

示例:

#Playbook内容
[root@ansible ~]# cat httpd.yml
---
- hosts: websrvs
  remote_user: root
  gather_facts: no

  tasks:
    - name: install httpd
      yum: name=httpd
      tags: install
    - name: start httpd
      service: name=httpd state=started enabled=yes
      tags: start
      
#列出标签:
[root@ansible ~]# ansible-playbook --list-tags httpd.yml 

playbook: httpd.yml

  play #1 (websrvs): websrvs    TAGS: []
      TASK TAGS: [install, start]
2.3.1.5:–list-tasks 列出任务

列出Playbook中所有将被执行的任务(注意是将被执行的任务,不是所有任务)。

--list-tasks
          list all tasks that would be executed

示例:

#Playbook内容(index file这个任务被打了never标签,意味着不会被执行)
[root@ansible ~]# vim httpd.yml 
---
- hosts: websrvs
  remote_user: root
  gather_facts: no

  tasks:
    - name: install httpd
      yum: name=httpd
      tags: install
    - name: start httpd
      service: name=httpd state=started enabled=yes
      tags: start
    - name: index file
      copy: src=index.html dest=/var/www/html/index.html owner=www group=www
      tags: index, never
      
#列出将被执行的任务(不包括never标签的任务)
[root@ansible ~]# ansible-playbook --list-tasks httpd.yml 

playbook: httpd.yml

  play #1 (websrvs): websrvs    TAGS: []
    tasks:
      install httpd     TAGS: [install]
      start httpd       TAGS: [start]
2.3.1.6:-l 限制目标主机

使用-l--limit指定额外的限制条件,对Playbook中hosts匹配到的主机列表进行二次匹配。

-l 'SUBSET', --limit 'SUBSET'
          further limit selected hosts to an additional pattern

示例:

#Playbook匹配的主机为websrvs组和testsrvs组的并集:
[root@ansible ~]# ansible-playbook --list-hosts debug.yml 

在这里插入图片描述

#执行Playbook时,添加额外的匹配条件'web*',对匹配到的主机列表进行二次匹配,就只能匹配到websrvs组中的主机了
[root@ansible ~]# ansible-playbook -l 'web*' -C debug.yml 

在这里插入图片描述

2.3.1.7:-t 指定标签

用于指定标签,来仅运行Playbook中特定的PlAY或TASK。

-t, --tags
          only run plays and tasks tagged with these values

示例:

#Playbook内容
[root@ansible ~]# cat apache2.yml
---
- hosts: websrvs
  remote_user: root
  gather_facts: no

  tasks:
    - name: install apache2
      apt: name=apache2
      tags: install
    - name: start apache2
      service: name=apache2 state=started enabled=yes
      tags: start
    - name: index file
      copy: src=index.html dest=/var/www/html/index.html owner=www group=www
      tags: index
...


#仅运行打了start标签的任务
[root@ansible ~]# ansible-playbook -t start apache2.yml 

在这里插入图片描述

2.3.1.8:-i 指定主机清单文件

通常ansible-playbook命令会从相应的ansible.cfg配置文件中inventory配置项指定的路径查找主机清单文件,使用-i选项可以指定本次运行时使用的主机清单文件。

-i, --inventory, --inventory-file
          specify inventory host path or comma separated host list. --inventory-file is deprecated

示例:

#Playbook内容(hosts指定的是websrvs组)
[root@ansible ~]# cat debug.yml 
---
- hosts: websrvs
  tasks:
  - name: debug module test
    debug:
      msg: IpAddr of Host {{ ansible_nodename }} is {{ ansible_default_ipv4.address }}
...

#默认的主机清单文件内容(websrvs组中的主机是192.168.1.111):
[root@ansible ~]# cat /etc/ansible/hosts 
[websrvs]
192.168.1.111

#查看Playbook匹配到的目标主机(匹配到的是192.168.1.111)
[root@ansible ~]# ansible-playbook --list-hosts debug.yml 

playbook: debug.yml

  play #1 (websrvs): websrvs    TAGS: []
    pattern: [u'websrvs']
    hosts (1):
      192.168.1.111

#自定义的主机清单文件(websrvs组中的主机是192.168.1.222):
[root@ansible ~]# cat /tmp/hosts 
[websrvs]
192.168.1.222


#指定自定义的主机清单文件,查看Playbook匹配到的目标主机(匹配到的是192.168.1.222):
[root@ansible ~]# ansible-playbook -i /tmp/hosts --list-hosts debug.yml 

playbook: debug.yml

  play #1 (websrvs): websrvs    TAGS: []
    pattern: [u'websrvs']
    hosts (1):
      192.168.1.222

2.3.1.9:–start-at-task 指定起始TASK

默认执行Playbook中全部的可执行TASK,使用--start-at-task可以从指定的TASK开始执行。

注意这里指定的是TASK的任务名称,而非标签。

--start-at-task 'START_AT_TASK'
          start the playbook at the task matching this name

示例:

#Playbook内容
[root@ansible ~]# cat apache2.yml 
---
- hosts: websrvs
  remote_user: root
  gather_facts: no
  
  tasks:
    - name: install apache2
      apt: name=apache2
      tags: install
    - name: start apache2
      service: name=apache2 state=started enabled=yes
      tags: start
    - name: index file
      copy: src=index.html dest=/var/www/html/index.html owner=www group=www
      tags: index
...


#从start apache2这个任务开始往后执行(即只执行start apache2和index file两个任务)
[root@ansible ~]# ansible-playbook --start-at-task 'start apache2' apache2.yml 

在这里插入图片描述

2.3.1.10:-e 定义变量

用于在命令行中为Playbook定义变量,或指定变量文件(YAML/JSON,指定文件时用@作为前缀)。

-e, --extra-vars
          set additional variables as key=value or YAML/JSON, if filename prepend with @

Playbook 示例:

[root@ansible ~]# vim remove.yml
---
- hosts: websrvs
  tasks:
    - name: remove {{ rmfile_name }}
      file: path={{ rmfile_name }} state=absent
...

在命令行中定义变量并执行Playbook:

[root@ansible ~]# ansible-playbook -e rmfile_name=/etc/fstab -C remove.yml 

在这里插入图片描述

可以将多个变量定义在一个变量文件中:

[root@ansible ~]# vim vars.yml
src_file: files/ports.conf
dest_file: /etc/apache2/ports.conf

Playbook 示例:

[root@ansible ~]# vim copy.yml 
---
- hosts: websrvs
  tasks:
    - name: copy {{ src_file }} to {{ dest_file }} on {{ ansible_nodename }}
      copy:
        src: "{{ src_file }}"
        dest: "{{ dest_file }}"
...

在命令行中指定变量文件并执行Playbook:

[root@ansible ~]# ansible-playbook -e '@vars.yml' -C copy.yml  

在这里插入图片描述

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值