[root@node1 ansible]# ansible-playbook httpd.yaml
PLAY [nodes] ***********************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************
ok: [192.168.223.146]
TASK [install httpd service] *******************************************************************************************************
ok: [192.168.223.146]
TASK [copy configuration file for nodes] *******************************************************************************************
changed: [192.168.223.146] 这里将配置文件端口又修改为了808
TASK [start httpd service] *********************************************************************************************************
ok: [192.168.223.146]
RUNNING HANDLER [restart httpd service] ********************************************************************************************
changed: [192.168.223.146] 上面配置文件进行修改,于是触发了handler
PLAY RECAP *************************************************************************************************************************
192.168.223.146 : ok=5 changed=2 unreachable=0 failed=0
在playbook中使用变量:前提已经将节点的httpd服务卸载了
[root@node1 ~]# cat /etc/ansible/httpd_var.yaml
- hosts: nodes
remote_user: root
vars: 变量定义
- package: httpd
- service_name: httpd
tasks:
- name: install httpd service
yum: name={{ package }} state=latest 这里调用变量格式{{ 变量名 }}
- name: copy configuration file for nodes
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd service
- name: start httpd service
service: name={{ service_name }} state=started
handlers:
- name: restart httpd service
service: name=httpd state=restarted
facts中的内容,不用定义变量就能使用
ansible all -m setup 显示facts的值
[root@node1 ansible]# cat test_var.yaml
- hosts: nodes
remote_user: root
tasks:
- name: create a new file
copy: content={{ ansible_hostname }} dest=/tmp/ansible_var.txt
调用facts的值,copy模块创建一个有内容的文件
在hosts清单文件中定义变量,并进行调用:
[root@node1 ansible]# cat hosts
[nodes]
192.168.223.146 node_var=192.168.223.146 然后yaml中调用这个变量(为组中的主机单独定义)
[root@node1 ansible]# cat test_var.yaml
- hosts: nodes
remote_user: root
tasks:
- name: create a new file
copy: content={{ node_var }} dest=/tmp/ansible_var.txt
在playbook中使用条件判断:
[root@node1 ansible]# cat test_when.yaml
- hosts: nodes 因为nodes这个组只有wadeson这个hostname的节点
remote_user: root
tasks:
- name: create a user
user: name=json_hc state=present
when: ansible_hostname != "wadeson" 当节点中hostname=wadeson的这个节点才不创建该用户
当有需要重复执行的任务时,可以使用迭代机制,item是固定使用变量名,并通过with_items语句
来指明迭代的元素列表即可
实例:
[root@node1 ansible]# cat test_item.yaml
- hosts: nodes
remote_user: root
tasks:
- name: create several users
user: name={{ item }} state=present item固定变量名
with_items: 下面列表具体变量值
- testuser1
- testuser2
when结合items使用:
[root@node1 ansible]# cat tar.yaml
- hosts: nodes
remote_user: root
vars:
- sysnum: ls -l /root/tools/scripts/apr-1.6.2.tar.gz |wc -l 这里变量sysnum的值为1
tasks:
- name: tar
shell: cd /root/tools/scripts; tar xf apr-1.6.2.tar.gz
when: item != 1 当条件为sysnum值不等于1的时候,才执行上面的shell任务解压该包
with_items:
- "{{ sysnum }}"
[root@node1 ansible]# ansible-playbook tar.yaml
PLAY [nodes] ***********************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************
ok: [192.168.223.146]
TASK [tar] *************************************************************************************************************************
changed: [192.168.223.146] => (item=ls -l /root/tools/scripts/apr-1.6.2.tar.gz |wc -l)
PLAY RECAP *************************************************************************************************************************
192.168.223.146 : ok=2 changed=1 unreachable=0 failed=0
这里只是介绍when结合items使用,调用前面的变量进行判断