ansible常用命令操作,ansible常用命令


##主机组

/etc/ansible/hosts 文件的格式与windowsini配置文件类似:

mail.example.com

[webservers]

foo.example.combar.example.com

[dbservers]

one.example.com

two.example.com

three.example.com

 

加端口号:

badwolf.example.com:5309

 

一组服务器

[webservers]

www[01:50].example.com

 

[databases]

db-[a:f].example.com

 

[targets]

localhost ansible_connection=local

other1.example.com ansible_connection=ssh  ansible_ssh_user=mpdehaan

other2.example.com ansible_connection=ssh  ansible_ssh_user=mdehaan

选择连接类型和连接用户名

 

#patterns类型

ansible <pattern_goes_here> -m <module_name> -a <arguments>

 

如下的patterns等同于目标为仓库(inventory)中的所有机器:

all

*

 

也可以写IP地址或系列主机名:

one.example.com

one.example.com:two.example.com

192.168.1.50

192.168.1.*

 

 

 

你也可以不必严格定义groups,单个的host names, IPs , groups都支持通配符:

*.example.com

*.com

 

 

大部分人都在patterns应用正则表达式,但你可以.只需要以 ‘~’ 开头即可:

~(web|db).*\.example\.com

 

Ansible 的命令行工具来重启 Atlanta 组中所有的 web 服务器,每次重启10个.

$ ansible atlanta -a "/sbin/reboot" -f 10

 

 

/usr/bin/ansible 时,默认是以当前用户的身份去执行这个命令

 ansible atlanta -a "/usr/bin/foo" -u username

 

 

ansible raleigh -m shell -a 'echo $TERM'

尤其注意 shell 引号的规则. 比如在上面的例子中,如果使用双引号”echo $TERM”,会求出TERM变量在当前系统的值,而我们实际希望的是把这个命令传递 到其它机器执行.

 

 ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"

以并行的方式同时 SCP 大量的文件到多台机器.

 

使用 file 模块可以做到修改文件的属主和权限

$ ansible webservers -m file -a "dest=/srv/foo/a.txt mode=600"

$ ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"

 

使用 file 模块也可以创建目录,与执行 mkdir -p 效果类似:

$ ansible webservers -m file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory"

 

删除目录(递归的删除)和删除文件:

$ ansible webservers -m file -a "dest=/path/to/c state=absent"

 

确认一个软件包已经安装,但不去升级它:

 ansible webservers -m yum -a "name=acme state=present"

 

确认一个软件包的安装版本:

$ ansible webservers -m yum -a "name=acme-1.5 state=present"

 

 

确认一个软件包还没有安装:

$ ansible webservers -m yum -a "name=acme state=absent"

 

使用 ‘user’ 模块可以方便的创建账户,删除账户,或是管理现有的账户:

$ ansible all -m user -a "name=foo password=<crypted password here>"

$ ansible all -m user -a "name=foo state=absent"

 

确认某个服务在所有的webservers上都已经启动:

$ ansible webservers -m service -a "name=httpd state=started"

 

所有的webservers上重启某个服务(译者注:可能是确认已重启的状态?):

$ ansible webservers -m service -a "name=httpd state=restarted"

 

确认某个服务已经停止:

$ ansible webservers -m service -a "name=httpd state=stopped"

 

 

playbooks 中有对于 Facts 做描述,它代表的是一个系统中已发现的变量.

$ ansible all -m setup

 

#PLAYBOOK基础

Playbooks 是 Ansible的配置,部署,编排语言.他们可以被描述为一个需要希望远程主机执行命令的方案,或者一组IT程序运行的命令集合.

 

主机与用户:

你可以为 playbook 中的每一个 play,个别地选择操作的目标机器是哪些,以哪个用户身份去完成要执行的步骤(called tasks).

- hosts: webservers

  remote_user: root

 

Tasks 列表:

在运行 playbook 时(从上到下执行),如果一个 host 执行 task 失败,这个 host 将会从整个 playbook 的 rotation 中移除. 如果发生执行失败的情况,请修正 playbook 中的错误,然后重新执行即可.

 

如果 action 行看起来太长,你可以使用 space(空格) 或者 indent(缩进) 隔开连续的一行:

tasks:

  - name: Copy ansible inventory file to client

    copy: src=/etc/ansible/hosts dest=/etc/ansible/hosts

            owner=root group=root mode=0644

action 行中可以使用变量.假设在 ‘vars’ 那里定义了一个变量 ‘vhost’ ,可以这样使用它:

tasks:

  - name: create a virtual host file for {{ vhost }}

    template: src=somefile.j2 dest=/etc/httpd/conf.d/{{ vhost }}

 

 

Handlers: 在发生改变时执行的操作

这里有一个例子,当一个文件的内容被改动时,重启两个 services:

- name: template configuration file

  template: src=template.j2 dest=/etc/foo.conf

  notify:

     - restart memcached

     - restart apache

‘notify’ 下列出的即是 handlers.

 

 

这里是一个 handlers 的示例:

handlers:

    - name: restart memcached

      service:  name=memcached state=restarted

    - name: restart apache

      service: name=apache state=restarted

 

行的运行 playbook,并行的级别 是10

ansible-playbook playbook.yml -f 10

相关内容

    暂无相关文章