ansible-playbook使用

ansible-playbook有两种登陆方式:

1、基于ssh密码登录

2、不配置免密码登录,需要在hosts文件中配置

一、基于ssh密码登录

参数一:inventory
ansible的主要功能用于批量管理主机操作,便捷的使用部分主机,可以在inventory file中分组
默认的inventory file为/etc/ansible/hosts

1. inventory文件格式
同一主机归并到一个或者多个组中,主机使用非默认的22端口,也可以在主机名称后加冒号端口标明

www.ab.com
[webservers]
www.abc.com:222
www.abcd.com
[dbservers]
db1.abc.com
db2.abc.com
db3.abc.com

 如果主机遵循相似的命名模式,可以使用列表标记各个主机

[webserver]
www[01:30].example.com
 
[dbserver]
db-[a:f].example.com

2. 主机变量
可以在定义主机时添加远程主机变量便于在playbook中使用(http中的变量,在j2中配置)

[webserver]
www.ab.com http_port=80 maxRequestsPerChild=808
www.abc.com http_port=8080 maxRequestsPerChild=909

3. 组变量

赋予指定组内的所有主机在playbook中用的变量(vars),可以调用组里面的变量

[webserver]
www.ab.com
www.bc.com

[webserver:vars]
host_port=80
maxRequestsPerChild=890

4. 组嵌套
组还包括其他组,可以在其他组中指定变量,只能在ansible-playbook中使用,ansible不支持
children中包括apache,nginx的主机,共四台主机

[apache]
http1.abc.com
http2.abc.com

[nginx]
nginx1.abc.com
nginx2.abc.com

[webservers:children]
apache
nginx

[webservers:vars]
host_port=80

 ansible基于ssh连接inventory可以指定远程主机,通过参数指定交互式,参数如下:

ansible_ssh_host
ansible_ssh_port
ansible_ssh_user
ansible_ssh_pass
ansible_sudo_pass
ansible_connection
ansible_ssh_private_key_file
ansible_shell_type
ansible_python_interpreter

二、不基于ssh免密码登录

即便是我们没有做关于秘钥的认证,我们也会自动使用账户和密码来登录该主机。

[webservers]
192.168.133.2 ansible_ssh_user=root ansible_ssh_pass=123456
192.168.133.3 testvar=“2.100

案例一:module_name: module_args   模块名和模块参数

- host: webserver
  remote_user:
  task:
  - task1
    module_name: module_args
    - name: test connection
      ping:
      remote_user; www
      sudo: yes
      command: /sbin/setenforce 0
      shell: /usr/bin/somecommand || /bin/true
      ignore_errors: True
  - task2
    module_name: module_args 

       在运行playbook是中途发生错误,可能会回滚,更正后,重新执行一次;可以指定使用sudo的方式远程执行任务,用sudo到root用户执行命令;众多模块中,只有command和shell模块仅使用给定一个列表而无需使用"key=value";命令或脚本的退出码不为0,可以使用如上替代,命令不成功强行成功;使用ignore_errors忽略错误信息。

案例二:handlers

  在notify中列出的操作为handler,比如之前配置了apache的配置文件,http.conf端口发生变化后,重新执行ansible-playbook后,查看远程的端口,并未发生变化。需要用到notify引入变量:

     vars:在后面添加变量名,然后再引入变量,必须加{{}},用变量名替换。

- host: webservers
  remote_user: root
vars:
- packages: httpd tasks: - name: install httpd packages yum: name={{ packages }} state=lastest - name: install configuration file for httpd copy: src=/root/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf notify: - restart httpd - name: start httpd service service: enabled=true name={{ packages }} state=started handlers: - name: restart httpd service: name={{ packages }} state=restarted

 案例三:将每台主机的ip地址(ansible_all_ipv4_addresses)发送到远程主机的/tmp/var.ansible文档中,查找变量名(ansible_all_ipv4_addresses用命令:ansible 192.168.133.4 -m setup ),也可以用主机传导一些变量。用组变量或者inventory变量。在主机webservers中定义变量testvar和调用ansible_ssh_host,在目标主机中调用这个变量。

vi /etc/ansible/hosts
[webservers]
192.168.133.2 testvar="1.100" ansible_ssh_host=root
192.168.133.3 testvar="2.100"

vi test.yml
- hosts: webservers remote_user: root tasks: - name: copy file copy: content="{{ ansible_all_ipv4_addresses }}, {{ testvar }}" dest=/tm/vars.ansible 在远程主机上查看cat /tmp/vars.ansible 或者ssh 192.168.133.2 cat /tmp/vars.ansible

案例四:when条件变量

当检查系统为Debin系统时,关机

- name: "shutdown Debian system"
  command: /sbin/shutdown -h now
  when: ansible_os_family == "Debin"

 当ansible_fqdn == "www.test.com"时,创建user10用户

- hosts: all
  remote_user: root
  vars:
  - username: user10
  tasks:
  - name: create {{ username }} user
    user: name={{ username }}
    when: ansible_fqdn == "www.test.com"

案例五:迭代

当有需要重复性执行的任务时,可以用迭代机制,使用格式为将需要迭代的内容定义为item变量引用,并通过with_items语句来指明迭代的元素列表即可

- name: add several users
  user: name={{ item }} state=present groups=wheel
  with_items:
     - testuser1
     - testuser2 

上面的语句功能等同于下面的语句:

 - name: add several users1
      user: name=testuser1 state=present groups=wheel
 - name: add several users2
      user: name=testuser2 state=present groups=wheel

事实上with_items可以使用元素还可为hashes,

注意:name: 'testuser1'调用的是item.name;group: 'wheel'调用的是item.groups,

其中item.后面的name是自己定义的,with_items中的子名字name是自己定义的,和item.后面定义的一致。

- name: add several users
  user: name={{ item.name }} state=present groups={{ item.groups }}
  with_items:
    - { name: 'testuser1', group: 'wheel' }
    - { name: 'testuser2', group: 'root' }

 案例六:template模板
 比如两台远程服务器要监听的端口不同,maxclients不同,而且主机名不同。可以调用模板,j2代表模板。在本地创建模板文件httpd.conf.j2,在yml中定义template

cd /root/ && mkdir templates && cp conf/httpd.conf templates/
mv templates/httpd.conf template/httpd.conf.j2
vim httpd.conf.j2
Listen        {{ http_port}}
MaxClients    {{ maxClients}}
ServerName    {{ ansbible_fqdn }}
     
vi /etc/ansible/hosts
[webservers]
192.168.133.2 http_port=80 maxClients=100
192.168.133.3 http_port=8080 maxClients=200
     
vi apache.yml
- host: webservers
  remote_user: root
  vars:
  - package: httpd
  tasks:
  - name: install httpd packages
  yum: name={{ package }} state=lastest
  - name: install configuration file for httpd
    template: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
    notify:
    - restart httpd
  - name: start httpd service
    service: enabled=true name={{ package }} state=started   
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted
     
ansible-playbook apache.yml

 案例七:tags

多次运行playbook时,其中某些task不需要运行,只需运行某个task,可以单独标记该task,在yml中指定tags。
在playbook可以为某个或某些任务定义为一个标签,在执行playbook时,通过ansible-playbook命令使用--tags选项能实现仅运行指定的tasks而非所有的。

其中tags中的conf和service为自己命名的。特殊:不能定义为always

vi apache.yml
- host: webservers
  remote_user: root
  vars:
  - package: httpd
  tasks:
  - name: install httpd packages
    yum: name={{ package }} state=lastest
  - name: install configuration file for httpd
    template: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
    tags:
    - conf
    notify:
    - restart httpd
  - name: start httpd service
    service: enabled=true name={{ package }} state=started<br>    tags:<br>    - service  
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted
     
ansible-playbook apache.yml --tags="conf"

ansible-playbook apache.yml --tags="service"

 案例八:roles,推荐使用此种模式

1. 目录名同文件名

2. 目录结构有固定格式

     files:静态文件

     templates:Jinjia2模板文件

     tasks:至少有main.yml文件,定义各tasks

     handlers:至少一个main.yml文件,定义各handlers

     vars:至少有一个main.yml文件,定义变量

     meta:定义依赖关系等信息

3. site.yml中定义playbook,额外有其他的yml文件。

roles能根据层次型结构目录装载变量文件,task已经handlers,使用roles需要在playbook中使用include指令即可。roles通过将变量,文件,任务,模块,处理器放置到单独的目录中。

roles案例:
tree ansible_playbook
site.yml
webservers.yml
dbservers.yml
roles/
    common/
        files/
        templates/
        tasks/
        handlers/
        vars/
        meta/
    webservers/
        files/
        templates/
        tasks/
        handlers/
        vars/
        meta/   

在playbook中,可以这样使用roles:

- hosts: webservers
  roles:
    - common
    - webservers

也可以向 roles传递参数,例如:

- hosts: webservers
  roles:
    - common
    - { role: foo app instance, dir:'/opt/a, port: 5000 }
    - { role: foo app instance, dir: '/opt/b', port: 5001 }

甚至也可以条件式地使用 roles,例如:

- hosts: webservers
  roles
    - { role: some_role, when:"ansible_os_ family =='RedHat'" }

1. 创建role的步骤

  • 创建以 roles 命名的目录;
  • 在 roles 目录中分别创建以各角色名称命名的目录,如 webservers等;
  • 在每个角色命名的目录中分别创建files、 handlers、meta、 tasks、 templates、vars目录(用不到的目录可以创建为空目录,也可以不创建);
  • 在playbook文件中,调用各角色;

2. role内各目录中可用的文件

  • tasks目录:至少应该包含一个名为main.yml的文件,其定义了此角色的任务列表,此文件可以使用include包含其它的位于此目录中的task文件;
  • files目录:存放由copy或script等模块调用的文件;
  • templates目录:template模块会自动在此目录中寻找j2模板文件;
  • handlers目录:此目录中应当包含一个main.yml文件,用于定义此角色用到的各handler,在handler中使用include包含的其它的handler文件也应该位于此目录中;
  • vars目录:应当包含一个main.yml文件,用于定义此角色用到的变量;
  • meta目录:应当包含一个main.yml文件,用于定义此角色的特殊设定及其依赖关系,ansible1.3及其以后的版本才支持;
  • default目录:为当前角色设定默认变量时使用此目录,应当包含一个main.yml文件;

案例九:Tags
tags用于让用户选择运行或跳过playbook中的部分代码。ansible具有幂等性,因此会自动跳过没有变化的部分,即便如此有些代码为测试其确实没有发生
变化的时间依然会非常地长。此时,如果确信其没有变化,就可以通过tags跳过此类代码片断。

案例:172.168.100.1配置成webserver,172.168.100.2配置成dbservers,172.168.100.3配置成web和db

mkdir -pv ansible_playbook/roles/{webservers,dbservers}/{tasks,files,templates,meta,handlers,vars}
cd roles/webservers/
cp /etc/httpd/conf/httpd.conf files/
vim tasks/main.yml
- name: install httpd packages
- yum: name=httpd
- nane: install configuration file
- copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
  tags:
  - conf
  notify:
  - restart httpd
- name: start httpd
  service: name=httpd state=started
 
vim handler/main.yml
- name: restart httpd
- service: name=httpd state=restarted

如果有vars可以配置,此示例中不使用

vim vars/main.yml
- http_port: 80
- maxClients: 200

如果想掉用webservers,在roles目录之外创建site.yml文件

vim ansible_playbook/site.yml
- hosts: webservers
  remote_user: root
  roles:
  - webservers
  
- hosts: 172.168.100.1
  remote_user: root
  roles:
  - dbservers
 
- hosts: 172.168.100.2
  remote_user: root
  roles:
  - dbservers 
  - webservers
同理在dbservers进行配置
ansible-playbook site.yml

 

 

 

posted @ 2019-06-05 10:49  Viviane未完  阅读(1337)  评论(0编辑  收藏  举报