目录

- playbook是剧本的意思
- 通过 task 调用 ansible 的模块将多个 play 组织在一 个playbook中运行。
playbook本身由以下各部分组成:
playbook由YMAL语言编写,YAML是一种非标记语言。是用来写配置文件的语言,非常简洁合强大
1、大小写敏感
2、使用缩进表示层级关系
3、缩进时不允许使用tab键、只允许使用空格
4、缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
| hosts | 定义节点,可以是组 |
|---|---|
| remote_user | 是你以什么用户身份进行登陆 |
| tasks | 是你的任务 |
| become:yes | 表示切换用户 |
| become_user: mysql | 表示切换到mysql用户,配合上一条使用 |
| - name: | 为下面执行的操作起名 |
Inventory是Ansible管理主机信息的配置文件,相当于系统HOSTS文件的功能,默认存放在/etc/ansible/hosts
主机变量
- [web1]
- www.zhangbin.com
- www.lichen.com
组变量
[servers:vars]
组嵌套
- [web1]
- www.zhangbin.com
- www.lichen.com
-
- [web2]
- www.lcdb.com
- www.lc2b.com
-
- [webservers]
- web1
- web2
| 参数 | 说明 |
| 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” |
| ansible_*_interpreter | 这里的*可以是ruby或perl或其他语言的解释器,作用和ansible_python_interpreter类似 |
| ansible_shell_executable | 这将设置ansible控制器将在目标机器上使用的shell,覆盖ansible.cfg中的配置,默认为/bin/sh |
- ansible-playbook xxx.yaml --syntax-check #检查yaml文件的语法是否正确
- ansible-playbook xxx.yaml --list-task #检查tasks任务
- ansible-playbook xxx.yaml --list-hosts #检查生效的主机
- ansible-playbook xxx.yaml --start-at-task='xxx' #指定从某个task开始运行
-
- - hosts: webserver #指定主机组,可以是一个或多个组
-
- remote_user: root #指定远程主机执行的用户名
| 参数 | 说明 |
|---|---|
| -k(-ask-pass) | 用来交互输入ssh密码 |
| -K(-ask-become-pass) | 用来交互输入sudo密码 |
| -u | 指定用户 |
| -e | 引入变量值 |
- cd /opt
- vim 1.yaml
-
- - hosts: mysql
- remote_user: root
- tasks:
- - name: test connection
- ping:
- remote_user: mysql
-
- ansible mysql -m user -a 'name=mysql'
- ansible mysql -m shell -a 'echo 123456 | passwd --stdin mysql'
- ansible-playbook 1.yaml -k
- 123456
- vim 2.yaml
-
- - hosts: mysql
- remote_user: root
- become: yes
- become_user: mysql
- tasks:
- - name: copy text
- copy: src=/etc/fstab dest=/home/mysql/fstab.bak
-
- ansible-playbook 2.yaml
错误示例:遇到错误task自动停止,apache服务不会继续安装
- vim 3.yaml
-
- - hosts: webserver
- remote_user: root
- tasks:
- - name: stop selinux
- command: '/usr/sbin/setenforc 0'
- - name: install httpd
- yum: name=httpd
- - name: start httpd
- service: name=httpd state=started
-
- ansible-playbook 3.yaml
加入ignore_errors: True 忽略错误,报错后继续执行
- vim 3.yaml
-
- - hosts: webserver
- remote_user: root
- tasks:
- - name: stop selinux
- command: '/usr/sbin/setenforc 0'
- ignore_errors: True
- - name: install httpd
- yum: name=httpd
- - name: start httpd
- service: name=httpd state=started
-
- ansible-playbook 3.yaml
- vim 4.yaml
-
- - hosts: webserver
- remote_user: root
- tasks:
- - name: remove httpd
- yum: name=httpd state=absent
-
- - hosts: mysql
- remote_user: root
- tasks:
- - name: copy file
- copy: src=/etc/fstab dest=/opt/haha.txt
Handlers也是一些task的列表, 和一般的task并没有什么区别。
是由通知者进行的notify(通知),如果没有被notify,则Handlers不会执行,假如被notify了 ,则Handlers被执行不管有多少个通知者进行了notify,等到play中的所有task执行完成之后,handlers也只会被执行一次
- vim 5.yaml
-
- - hosts: webserver
- remote_user: root
- tasks:
- - name: remove httpd
- yum: name=httpd state=absent
-
- - name: start firewalld
- service: name=firewalld state=started
-
- - name: setenforce 0 && install httpd
- command: '/usr/sbin/setenforce 0'
- notify:
- - step one
-
- - name: stop firewalld && start httpd
- service: name=firewalld state=stopped
- notify:
- - step two
-
- handlers:
-
- - name: step one
- yum: name=httpd
-
- - name: step two
- service: name=httpd state=started
-
-
- ansible-playbook 5.yaml
playbook引入变量有三种方式
- vim 6_1.yaml
-
- - hosts: mysql
- remote_user: root
- vars:
- - user:
- tasks:
- - name: add user
- user: name={{user}}
-
- ansible-playbook 6_1.yaml -e "user=wangwu"
- ansible mysql -a 'tail -1 /etc/passwd'
- vim 6_2.yaml
-
- - hosts: mysql
- remote_user: root
- vars:
- - user: lisi
- tasks:
- - name: add user
- user: name={{user}}
-
- ansible-playbook 6_2.yaml
- ansible mysql -a 'tail -1 /etc/passwd'
- vim 6_2.yaml
-
- - hosts: mysql
- remote_user: root
- tasks:
- - name: copy file
- copy: content="{{ansible_all_ipv4_addresses}}" dest=/opt/vars.txt
-
- ansible-playbook 6_2.yaml
- ansible mysql -a 'ls /opt'
- ansible mysql -a 'cat /opt/vars.txt'
- vim /etc/ansible/hosts
-
- [webserver]
- 192.168.184.20
- [mysql]
- 192.168.184.30 user=zhaoliu
-
- vim 6_3.yaml
-
- - hosts: mysql
- remote_user: root
- tasks:
- - name: add user
- user: name={{user}}
-
- ansible-playbook 6_3.yaml
- ansible mysql -a 'tail -1 /etc/passwd'
如果需要根据变量、facts (setup) 或此前任务的执行结果来作为某task执行与否的前提时要用到条件测试,在Playbook中条件测试使用。在task后添加when子句即可使用条件测试: when子句支持 jinjia2 表达式或语法
- vim 7_1.yaml
-
- - hosts: mysql
- remote_user: root
- tasks:
- - name: "shutdown CentOS"
- command: /sbin/shutdown -h now
- when: ansible_distribution == "CentOS"
-
- ansible-playbook 7_1.yaml
- vim 7_2.yaml
-
- - hosts: mysql
- remote_user: root
- tasks:
- - name: "shut down CentOS 7 systems"
- command: /sbin/shutdown -r now
- when:
- - ansible_distribution == "CentOS"
- - ansible_distribution_major_version == "7"
-
- ansible-playbook 7_2.yaml
- vim 7_3.yml
-
- - hosts: mysql
- remote_user: root
- tasks:
- - name: "shut down CentOS 6 and Debian 7 systems"
- command: /sbin/shutdown -t now
- when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or (ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
-
- ansible-playbook 7_3.yaml
当有需要重复性执行的任务时,可以使用迭代机制。其使用格式为将需要迭代的内容定义为item变量引用,并通过with_items语句指明迭代。
- vim 7_5.yaml
-
- - hosts: webserver
- remote_user: root
- tasks:
- - name: install
- yum: name={{item}} state=latest
- with_items:
- - httpd
- - rpcbind
- - nfs-utils
-
- ansible-playbook 7_5.yaml
- ansible webserver -a 'rpm -q httpd'
- ansible webserver -a 'rpm -q rpcbind'
- ansible webserver -a 'rpm -q nfs-utils'
也可以自己定义item变量
- vim 7_5.yaml
-
- - hosts: webserver
- remote_user: root
- tasks:
- - name: add user && join group
- user: name={{item.x}} state=present group={{item.y}}
- with_items:
- - {x: 'qianqi', y: 'wheel'}
- - {x: 'sicong', y: 'root'}
-
- ansible-playbook 7_5.yaml
- ansible webserver -a 'tail -2 /etc/passwd'