inventory主机清单
ansible默认的主机清单是/etc/ansible/hosts文件,主机清单可以手动设置,也可以通过Dynamic Inventory动态生成,一般主机名使用FQDN
vi /etc/ansible/hosts
[webserver] #方括号设置组名
www1.example.org #定义被监控主机,这边可以是主机名也可以是IP地址,主机名需要修改/etc/hosts文件
www2.example.org:2222 #冒号后定义远程连接端口,默认是ssh的22端口
如果是名称类似的主机,可以使用列表的方式标识各个主机
[webserver]
www[01:50].example.org ansible_ssh_user=root ansible_ssh_pass=123456[dbbservers]
db-[a:f].example.org
下面是Inventory中变量
(1)主机变量
[webserver]
www1.magedu.com http_port=80 maxRequestsChild=808
www2.magedu.com http_port=8080 maxRequestsChild=909
(2)组变量
[servers:vars]
ntp_server=ntp.example.org
nfs_server=nfs.example.org
(3)组嵌套
[apache]
http1.example.org
http2.example.org[nginx]
ngx1.example.org
ngx2.example.org[webservers:children]
apache
nginx
(4)inventory变量参数
参数 | 说明 |
ansible_ssh_host | 将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置. |
ansible_ssh_port | ssh端口号.如果不是默认的端口号,通过此变量设置. |
ansible_ssh_user | 默认的 ssh 用户名 |
ansible_ssh_pass | ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥) |
ansible_ssh_private_key_file | ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况. |
ansible_ssh_common_args | 此设置附加到sftp,scp和ssh的缺省命令行 |
ansible_sftp_extra_args | 此设置附加到默认sftp命令行。 |
ansible_scp_extra_args | 此设置附加到默认scp命令行。 |
ansible_ssh_extra_args | 此设置附加到默认ssh命令行。 |
ansible_ssh_pipelining | 确定是否使用SSH管道。 这可以覆盖ansible.cfg中得设置。 |
ansible_shell_type | 目标系统的shell类型.默认情况下,命令的执行使用 'sh' 语法,可设置为 'csh' 或 'fish'. |
ansible_python_interpreter | 目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是"/usr/bin/python",比如 *BSD, 或者 /usr/bin/python |
ansible_*_interpreter | 这里的"*"可以是ruby 或perl 或其他语言的解释器,作用和ansible_python_interpreter 类似 |
ansible_shell_executable | 这将设置ansible控制器将在目标机器上使用的shell,覆盖ansible.cfg中的配置,默认为/bin/sh。 |
YAML
YAML:另一种标记语言。是用来写配置文件的语言,非常简洁和强大。
YAML语法和其他语言类似,也可以表达散列表、标量等数据结构。
1、结构通过空格来展示;
2、序列里配置项通过-来代表;
3、Map里键值用:来分隔;
4、YAML的扩展名为yaml。
基本语法规则:
1.大小写敏感
2.使用缩进表示层级关系
3.缩进时不允许使用Tab键,只允许使用空格。
4.缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
YAML支持的数据结构:
1.对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
例如:name:Example Developer
键 值
2.数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
例如:-Apple
-Orange
3.纯量:单个的、不可再分的值
例如:number:12.30
sure:true
yaml示例:
---
name:zhangsan
age:20
name:lisi
age:22
people:
-name:zhangsan
age:20
-name:lisi
age:22
Ansible的脚本---playbook剧本
通过task调用ansible的模板将多个play组织在一个playbook中运行。
playbooks本身由以下各部分组成:
(1)Tasks:任务,即调用模块完成的某操作;
(2)Variables:变量
(3)Templates:模板
(4)Handlers:处理器,当某条件满足时,触发执行的操作;
(5)Roles:角色。
执行一个playbook
格式:ansible-playbook [yaml文件名]
例如:ansible-playbook ping.yml
参数:-k(–ask-pass) 用来交互输入ssh密码
-K(-ask-become-pass) 用来交互输入sudo密码
-u 指定用户
补充命令:
ansible-playbook nginx.yaml --syntax-check #检查yaml文件的语法是否正确
ansible-playbook nginx.yaml --list-task #检查tasks任务
ansible-playbook nginx.yaml --list-hosts #检查生效的主机
ansible-playbook nginx.yaml --start-at-task='Copy Nginx.conf' #指定从某个task开始运行
简单剧本编写:
[root@localhost opt]# vim a.yml
- hosts: webserver #指定主机组,可以是一个或多个组。
remote_user: root #指定远程主机执行的用户名
检查语法是否正确
[root@localhost opt]# ansible-playbook a.yml --syntax-check
playbook: a.yml #表明没问题
执行剧本
[root@localhost opt]# ansible-playbook a.yml
PLAY [webserver] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.35.101]PLAY RECAP *********************************************************************
192.168.35.101 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
原有基础上还可以为每个任务定义远程执行用户
[root@localhost opt]# !vim
- hosts: webserver
remote_user: root
tasks:
- name: test connect
ping:
remote_user: root #指定远程主机执行tasks的运行用户为root
验证语法
[root@localhost opt]# ansible-playbook a.yml --syntax-check
playbook: a.yml
执行剧本
[root@localhost opt]# ansible-playbook a.yml
PLAY [webserver] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.35.101]TASK [test connect] ************************************************************
ok: [192.168.35.101]PLAY RECAP *********************************************************************
192.168.35.101 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
指定远程主机sudo切换用户
[root@localhost opt]# !vim
- hosts: webserver
remote_user: root
become: #2.6版本以后的参数,之前是sudo,意思为切换用户运行
become_user: shan #指定sudo用户为shan
执行剧本
[root@localhost opt]# ansible-playbook a.yml
PLAY [webserver] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.35.101]PLAY RECAP *********************************************************************
192.168.35.101 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
tasks列表和action
1.Play的主体部分是task列表,task列表中的各任务按次序逐个在hosts中指定的主机上执行,即在所有主机上完成第一个任务后再开始第二个任务。
在运行playbook时(从上到下执行),如果一个host执行task失败,整个tasks都会回滚,请修正playbook 中的错误,然后重新执行即可。
Task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量,模块执行时幂等的,这意味着多次执行是安全的,因为其结果一致。2.每一个task必须有一个名称name,这样在运行playbook时,从其输出的任务执行信息中可以很好的辨别出是属于哪一个task的。如果没有定义name,‘action’的值将会用作输出信息中标记特定的task。
3.定义一个task,常见的格式:”module: options” 例如:yum: name=httpd
4.ansible的自带模块中,command模块和shell模块无需使用key=value格式
示例1:
[root@localhost opt]# !vim
- hosts: webserver
remote_user: root
tasks:
- name: disable selinux
command: '/sbin/setenforce 0'
- name: install httpd
yum: name=httpd
- name: start httpd
service: name=httpd state=started
检查语法
[root@localhost opt]# ansible-playbook a.yml --syntax-check
playbook: a.yml
执行剧本
[root@localhost opt]# ansible-playbook a.yml
PLAY [webserver] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.35.101]TASK [disable selinux] *********************************************************
changed: [192.168.35.101]TASK [install httpd] ***********************************************************
ok: [192.168.35.101]TASK [start httpd] *************************************************************
ok: [192.168.35.101]PLAY RECAP *********************************************************************
192.168.35.101 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
检查结果
[root@localhost ~]# rpm -q httpd
httpd-2.4.6-90.el7.centos.x86_64
[root@localhost ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since 日 2020-01-26 14:27:30 CST; 2h 25min ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 12872 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─12872 /usr/sbin/httpd -DFOREGROUND
├─12873 /usr/sbin/httpd -DFOREGROUND
├─12874 /usr/sbin/httpd -DFOREGROUND
├─12875 /usr/sbin/httpd -DFOREGROUND
├─12876 /usr/sbin/httpd -DFOREGROUND
└─12877 /usr/sbin/httpd -DFOREGROUND1月 26 14:27:30 localhost.localdomain systemd[1]: Starting The Apache HTTP...
1月 26 14:27:30 localhost.localdomain httpd[12872]: AH00558: httpd: Could ...
1月 26 14:27:30 localhost.localdomain systemd[1]: Started The Apache HTTP ...
Hint: Some lines were ellipsized, use -l to show in full.
进行修改
[root@localhost opt]# !vim
- hosts: webserver
remote_user: root
tasks:
- name: disable selinux
command: '/sbin/setenforce 0'
- name: disable firewalld
service: name=firewalld state=stopped
- name: start httpd
service: name=httpd state=started
执行剧本
vim a.yml
[root@localhost opt]# ansible-playbook a.ymlPLAY [webserver] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.35.101]TASK [disable selinux] *********************************************************
changed: [192.168.35.101]TASK [disable firewalld] *******************************************************
ok: [192.168.35.101]TASK [start httpd] *************************************************************
ok: [192.168.35.101]PLAY RECAP *********************************************************************
192.168.35.101 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
检查防火墙
[root@localhost ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon #已关闭
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: inactive (dead) since 日 2020-01-26 11:48:58 CST; 5h 6min ago
Docs: man:firewalld(1)
Main PID: 751 (code=exited, status=0/SUCCESS)1月 16 12:08:39 localhost.localdomain systemd[1]: Starting firewalld - dyn...
1月 16 12:08:39 localhost.localdomain systemd[1]: Started firewalld - dyna...
1月 16 12:08:40 localhost.localdomain firewalld[751]: WARNING: ICMP type '...
1月 16 12:08:40 localhost.localdomain firewalld[751]: WARNING: beyond-scop...
1月 16 12:08:40 localhost.localdomain firewalld[751]: WARNING: ICMP type '...
1月 16 12:08:40 localhost.localdomain firewalld[751]: WARNING: failed-poli...
1月 16 12:08:40 localhost.localdomain firewalld[751]: WARNING: ICMP type '...
1月 16 12:08:40 localhost.localdomain firewalld[751]: WARNING: reject-rout...
1月 26 11:48:58 localhost.localdomain systemd[1]: Stopping firewalld - dyn...
1月 26 11:48:58 localhost.localdomain systemd[1]: Stopped firewalld - dyna...
Hint: Some lines were ellipsized, use -l to show in full.
如果剧本中有错误或者少写了字母,势必会报错,我们来试验一下
[root@localhost opt]# !vim
- hosts: webserver
remote_user: root
tasks:
- name: disable selinux
command: '/sbin/setenforce 0'
- name: disable firewalld
service: name=firewall state=stopped #此行中firewalld少写个字母d
- name: start httpd
service: name=httpd state=started
去执行剧本
[root@localhost opt]# ansible-playbook a.yml
PLAY [webserver] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.35.101]TASK [disable selinux] *********************************************************
changed: [192.168.35.101]TASK [disable firewalld] *******************************************************
fatal: [192.168.35.101]: FAILED! => {"changed": false, "msg": "Could not find the requested service firewall: host"}PLAY RECAP *********************************************************************
192.168.35.101 : ok=2 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
会发生报错,解决方法如下
[root@localhost opt]# !vim
- hosts: webserver
remote_user: root
tasks:
- name: disable selinux
command: '/sbin/setenforce 0'
- name: disable firewalld
service: name=firewall state=stopped
ignore_errors: True #忽略错误,强制返回成功
- name: start httpd
service: name=httpd state=started
执行剧本,即使写错了,会直接被忽略,依然会执行成功
[root@localhost opt]# ansible-playbook a.yml
PLAY [webserver] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.35.101]TASK [disable selinux] *********************************************************
changed: [192.168.35.101]TASK [disable firewalld] *******************************************************
fatal: [192.168.35.101]: FAILED! => {"changed": false, "msg": "Could not find the requested service firewall: host"}
...ignoringTASK [start httpd] *************************************************************
ok: [192.168.35.101]PLAY RECAP *********************************************************************
192.168.35.101 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
Handlers介绍
Handlers也是一些task的列表,和一般的task并没有什么区别。
是由通知者进行的notify,如果没有被notify,则Handlers不会执行,假如被notify了,则Handlers被执行不管有多少个通知者进行了notify,等到play中的所有task执行完成之后,handlers也只会被执行一次
playbook使用变量的方法:
1.通过ansible命令传递
[root@localhost opt]# vim b.yml
- hosts: ftpserver
remote_user: root
vars:
- username: lisi
tasks:
- name: create user
user: name={{username}}
检查语法
[root@localhost opt]# ansible-playbook b.yml --syntax-check
playbook: b.yml
执行剧本
[root@localhost opt]# ansible-playbook b.yml
PLAY [ftpserver] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.35.103]TASK [create user] *************************************************************
ok: [192.168.35.103]PLAY RECAP *********************************************************************
192.168.35.103 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2、直接引用一些变量
[root@localhost opt]# vim b.yml
- hosts: ftpserver
remote_user: root
tasks:
- name: create file
copy: content={{ansible_all_ipv4_addresses}} dest=/opt/add.txt
检查语法
[root@localhost opt]# ansible-playbook b.yml --syntax-check
playbook: b.yml
执行剧本
[root@localhost opt]# ansible-playbook b.yml
PLAY [ftpserver] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.35.103]TASK [create file] *************************************************************
changed: [192.168.35.103]PLAY RECAP *********************************************************************
192.168.35.103 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
在192.168.35.103主机上进行查看
[root@localhost ~]# cat /opt/add.txt
["192.168.122.1", "192.168.35.103"]
条件测试
如果需要根据变量、facts(setup)或此前任务的执行结果来作为某task执行与否的前提时要用到条件测试,在Playbook中条件测试使用when子句。
在task后添加when子句即可使用条件测试:when子句支持jinjia2表达式或语法,例如:
[root@localhost opt]# vim b.yml
- hosts: ftpserver
remote_user: root
tasks:
- name: "shutdown CentOS"
command: /sbin/shutdown -h now
when: ansible_distribution == "CentOS"
执行剧本
[root@localhost opt]# ansible-playbook b.yml
PLAY [ftpserver] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.35.103]TASK [shutdown CentOS] *********************************************************
fatal: [192.168.35.103]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Shared connection to 192.168.35.103 closed.", "unreachable": true}PLAY RECAP *********************************************************************
192.168.35.103 : ok=1 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
执行后会发现192.168.35.103主机已经关机。
迭代
当有需要重复性执行的任务时,可以使用迭代机制。其使用格式为将需要迭代的内容定义为item变量引用,并通过with_items语句指明迭代的元素列表即可。例如:(批量添加用户)
[root@localhost ~]# vim a.yml
- hosts: all
vars:
exist: "False"
tasks:
- name: create users
user: name={{item}}
with_items:
- t01
- t02
- t03
检查语法
[root@localhost ~]# ansible-playbook a.yml --syntax-check
playbook: a.yml
执行剧本
[root@localhost ~]# ansible-playbook a.yml
PLAY [all] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.35.102]
ok: [192.168.35.103]
ok: [192.168.35.101]TASK [create users] ************************************************************
changed: [192.168.35.102] => (item=t01)
changed: [192.168.35.103] => (item=t01)
changed: [192.168.35.101] => (item=t01)
changed: [192.168.35.102] => (item=t02)
changed: [192.168.35.101] => (item=t02)
changed: [192.168.35.103] => (item=t02)
changed: [192.168.35.102] => (item=t03)
changed: [192.168.35.101] => (item=t03)
changed: [192.168.35.103] => (item=t03)PLAY RECAP *********************************************************************
192.168.35.101 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.35.102 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.35.103 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
在所有主机上查看是否有所添加的用户
192.168.35.101主机
[root@localhost ~]# tail -3 /etc/passwd
t01:x:1001:1001::/home/t01:/bin/bash
t02:x:1002:1002::/home/t02:/bin/bash
t03:x:1003:1003::/home/t03:/bin/bash
192.168.35.102主机
[root@localhost ~]# tail -3 /etc/passwd
t01:x:1001:1001::/home/t01:/bin/bash
t02:x:1002:1002::/home/t02:/bin/bash
t03:x:1003:1003::/home/t03:/bin/bash
192.168.35.103主机
[root@localhost ~]# tail -3 /etc/passwd
t01:x:1001:1001::/home/t01:/bin/bash
t02:x:1002:1002::/home/t02:/bin/bash
t03:x:1003:1003::/home/t03:/bin/bash
Templates模块
针对于apache的配置文件作为模板
首先,从主机192.168.35.101上远程复制一份配置文件到当前目录下
[root@localhost opt]# scp root@192.168.35.101:/etc/httpd/conf/httpd.conf ./
httpd.conf 100% 11KB 7.2MB/s 00:00
针对复制过来的配置文件进行配置变量,并做成模板
[root@localhost opt]# vim httpd.conf
/42 Listen {{http_port}}
/95 ServerName {{server_name}}
/96 MaxClients {{access_num}}
[root@localhost opt]# mv httpd.conf httpd.conf.j2 #制作为模板
然后给相应参数赋值
[root@localhost opt]# vim /etc/ansible/hosts
[webserver]
192.168.35.101 http_port=192.168.35.101:80 server_name="www.yun.com:80" access_num=200
[mysql]
192.168.35.102
[ftpserver]
192.168.35.103
开始编写剧本
[root@localhost opt]# vim apache.yml
- hosts: webserver
remote_user: root
vars:
- package: httpd
- server: httpd #定义变量
tasts:
- name: check latest
yum: name={{package}} state=latest #检查最新版本
- name: configure apache
template: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf #指定路径
notify:
- restart httpd
- name: start httpd
service: name={{server}} enabled=true state=started #开机自启操作
handlers:
- name: restart httpd
service: name={{server}} state=restarted #重启操作
检查语法
[root@localhost opt]# ansible-playbook apache.yml --syntax-check
playbook: apache.yml
执行剧本
[root@localhost opt]# ansible-playbook apache.yml
PLAY [webserver] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.35.101]TASK [check latest] ************************************************************
ok: [192.168.35.101]TASK [configure apache] ********************************************************
changed: [192.168.35.101]TASK [start httpd] *************************************************************
changed: [192.168.35.101]RUNNING HANDLER [restart httpd] ************************************************
changed: [192.168.35.101]PLAY RECAP *********************************************************************
192.168.35.101 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
最后在192.168.35.101主机上查看,配置文件中是否替换
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
/42 Listen 192.168.35.101:80
/95 ServerName www.yun.com:80
/96 MaxClients 200
或者
grep -i listen /etc/httpd/conf/httpd.conf
grep -i maxClient /etc/httpd/conf/httpd.conf
grep -i servername /etc/httpd/conf/httpd.conf
tags模块
1、在一个playbook中,我们一般会定义很多个task,如果我们只想执行其中的某一个task或多个task时就可以使用tags标签功能了,格式如下:
[root@localhost opt]# vim file.yml
- hosts: webserver
remote_user: root
tasks:
- name: Copy hosts file
copy: src=/etc/hosts dest=/opt/hosts #执行部分
tags:
- only
- name: touch file
file: path=/opt/hosts01 state=touch #不执行部分
检查语法
[root@localhost opt]# ansible-playbook file.yml --syntax-check
playbook: file.yml
执行剧本
[root@localhost opt]# ansible-playbook file.yml --tags="only"
PLAY [webserver] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.35.101]TASK [Copy hosts file] *********************************************************
changed: [192.168.35.101]PLAY RECAP *********************************************************************
192.168.35.101 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
在192.168.35.101主机上查看,opt目录下是否有hosts文件
[root@localhost ~]# ls /opt/
hosts httpd.txt rh
2、事实上,不光可以为单个或多个task指定同一个tags。playbook还提供了一个特殊的tags为always。作用就是当使用always当tags的task时,无论执行哪一个tags时,定义有always的tags都会执行。
[root@localhost opt]# !vim
- hosts: webserver
remote_user: root
tasks:
- name: Copy hosts file
copy: src=/etc/hosts dest=/opt/hosts #执行
tags:
- only
- name: touch file
file: path=/opt/hosts01 state=touch #无论什么情况下都执行
tags:
- always
检查语法
[root@localhost opt]# ansible-playbook file.yml --syntax-check
playbook: file.yml
在执行剧本前,先把远程主机中/opt/hosts文件删除,不影响此次操作
[root@localhost ~]# ls /opt/
hosts httpd.txt rh
[root@localhost ~]# rm -rf /opt/hosts
[root@localhost ~]# ls /opt/
httpd.txt rh
执行剧本
[root@localhost opt]# ansible-playbook file.yml --syntax-check
playbook: file.yml
[root@localhost opt]# ansible-playbook file.yml --tags="only"PLAY [webserver] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.35.101]TASK [Copy hosts file] *********************************************************
changed: [192.168.35.101]TASK [touch file] **************************************************************
changed: [192.168.35.101]PLAY RECAP *********************************************************************
192.168.35.101 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
在远程主机上查看有没有文件生成
[root@localhost ~]# ls /opt/
hosts hosts01 httpd.txt rh