Ansible-playbook 之 loops 循環一
標準loops 循環:下面是官方給的參考,參考鏈接 http://docs.ansible.com/ansible/latest/playbooks_loops.html#standard-loops
- name: add several users
user:
name: "{{ item }}"
state: present
groups: "wheel"
with_items:
- testuser1
- testuser2
備註:復制上面的代碼,直接執行是不成功的,剛學loops 循環 直接運行無論如何也不能成功,遇到了很多坑。
需要改寫如下:
--- - hosts: web tasks: - name: add several users user: name: "{{ item }}" state: present groups: "wheel" with_items: - testuser1 - testuser2
執行 stands.yml ansible-playbook stands.yml
執行效果如下
查看用戶是否創建成功
ansible web -m shell -a "id tsetuser1"
用戶創建成功
實戰演示: 使用loops 循環安裝PHP
--- - hosts: web gather_facts: False tasks: - name: install php packages yum: name: "{{ item }}" state: latest with_items: - gcc-c++ - php - php-devel - php-fpm
關於 yum 模塊的其他用法,以httpd 為列
name: 安裝最新版本的apache
yum: name=httpd state=latest
- name: 移除apache
yum: name=httpd state=absent
- name: 安裝一個特殊版本的apache
yum: name=httpd-2.2.29-1.4.amzn1 state=present
- name: 升級所有的軟件包
yum: name=* state=latest
- name: 從一個遠程yum倉庫安裝nginx
yum: name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present
- name: 從本地倉庫安裝nginx
yum: name=/usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present
- name: 安裝整個Development tools相關的軟件包
yum: name="@Development tools" state=present
執行 ansible-playbook yum_install_php.yml
執行過程如下
檢查php 是否安裝成功
ansible web -m shell -a "php -v "
PHP 安裝成功
Ansible-playbook 之 loops 循環一