[student@workstation lab-roles]$ ansible-galaxy init --offline -p roles student.myenv
- student.myenv was created successfully
[student@workstation lab-roles]$ ansible-galaxy init --offline -p roles myapache
- myapache was created successfully

[student@workstation lab-roles]$ tree
.
├── ansible.cfg
├── apache_httpdconf.j2
├── apache_indexhtml.j2
├── inventory
├── mkcd.sh.j2
└── roles
    ├── myapache
    │   ├── README.md
    │   ├── defaults
    │   │   └── main.yml
    │   ├── files
    │   ├── handlers
    │   │   └── main.yml
    │   ├── meta
    │   │   └── main.yml
    │   ├── tasks
    │   │   └── main.yml
    │   ├── templates
    │   ├── tests
    │   │   ├── inventory
    │   │   └── test.yml
    │   └── vars
    │       └── main.yml
    └── student.myenv
        ├── README.md
        ├── defaults
        │   └── main.yml
        ├── files
        ├── handlers
        │   └── main.yml
        ├── meta
        │   └── main.yml
        ├── tasks
        │   └── main.yml
        ├── templates
        ├── tests
        │   ├── inventory
        │   └── test.yml
        └── vars
            └── main.yml


[student@workstation lab-roles]$ mv mkcd.sh.j2 roles/student.myenv/templates/
[student@workstation lab-roles]$ cp /usr/share/icons/hicolor/48x48/apps/system-logo-icon.png roles/student.myenv/files/profile.png



[student@workstation lab-roles]$ vi roles/student.myenv/tasks/main.yml
[student@workstation lab-roles]$ cat roles/student.myenv/tasks/main.yml
---
# tasks file for student.myenv

- name: check myenv_user default
  fail:
    msg: You must specify the variale 'myenv_user' to use this role!
  when: "myenv_user == ''"

- name: install my packages
  yum:
    name: "{{ item }}"
    state: installed
  with_items: "{{ myenv_packages }}"

- name: copy placeholder profile pic
  copy:
    src: profile.png
    dest: "~{{ myenv_user }}/profile.png"

- name: set an alias in '.bashrc'
  copy:
    src: profile.png
    dest: "~{{ myenv_user }}/.bashrc"

- name: template out mkcd function
  template:
    src: mkcd.sh.j2
    dest: /etc/profile.d/mkcd.sh
    owner: root
    group: root
    mode: 0644



[student@workstation lab-roles]$ vi roles/student.myenv/vars/main.yml 
[student@workstation lab-roles]$ cat roles/student.myenv/vars/main.yml
---
# vars file for student.myenv

myenv_packages:
  - 'git'
  - 'tree'
  - 'vim-enhanced'



[student@workstation lab-roles]$ vi roles/student.myenv/defaults/main.yml 
[student@workstation lab-roles]$ cat roles/student.myenv/defaults/main.yml
---
# defaults file for student.myenv

myenv_user: ''


[student@workstation lab-roles]$ vi myenv.yml
[student@workstation lab-roles]$ cat roles/student.myenv/defaults/main.yml
---
# defaults file for student.myenv

myenv_user: ''
[student@workstation lab-roles]$ cat myenv.yml 
---
- name: setup my personal environment
  hosts: all
  roles:
    - student.myenv


[student@workstation lab-roles]$ ansible-playbook myenv.yml 

PLAY [setup my personal environment] ******************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [serverb.lab.example.com]
ok: [serverc.lab.example.com]

TASK [student.myenv : check myenv_user default] *******************************************************************************************************************************************************************
fatal: [serverb.lab.example.com]: FAILED! => {"changed": false, "failed": true, "msg": "You must specify the variale 'myenv_user' to use this role!"}
fatal: [serverc.lab.example.com]: FAILED! => {"changed": false, "failed": true, "msg": "You must specify the variale 'myenv_user' to use this role!"}
    to retry, use: --limit @/home/student/lab-roles/myenv.retry

PLAY RECAP ********************************************************************************************************************************************************************************************************
serverb.lab.example.com    : ok=1    changed=0    unreachable=0    failed=1   
serverc.lab.example.com    : ok=1    changed=0    unreachable=0    failed=1   



[student@workstation lab-roles]$ 
[student@workstation lab-roles]$ vi myenv.yml
---
# defaults file for student.myenv

myenv_user: ''
[student@workstation lab-roles]$ cat myenv.yml 
---
- name: setup my personal environment
  hosts: all
  roles:
    - role: student.myenv
      myenv_user: student


[student@workstation lab-roles]$ ansible-playbook myenv.yml 

PLAY [setup my personal environment] ******************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [serverb.lab.example.com]
ok: [serverc.lab.example.com]

TASK [student.myenv : check myenv_user default] *******************************************************************************************************************************************************************
skipping: [serverb.lab.example.com]
skipping: [serverc.lab.example.com]

TASK [student.myenv : install my packages] ************************************************************************************************************************************************************************
changed: [serverc.lab.example.com] => (item=[u'git', u'tree', u'vim-enhanced'])
changed: [serverb.lab.example.com] => (item=[u'git', u'tree', u'vim-enhanced'])

TASK [student.myenv : copy placeholder profile pic] ***************************************************************************************************************************************************************
changed: [serverc.lab.example.com]
changed: [serverb.lab.example.com]

TASK [student.myenv : set an alias in '.bashrc'] ******************************************************************************************************************************************************************
changed: [serverc.lab.example.com]
changed: [serverb.lab.example.com]

TASK [student.myenv : template out mkcd function] *****************************************************************************************************************************************************************
changed: [serverb.lab.example.com]
changed: [serverc.lab.example.com]

PLAY RECAP ********************************************************************************************************************************************************************************************************
serverb.lab.example.com    : ok=5    changed=4    unreachable=0    failed=0   
serverc.lab.example.com    : ok=5    changed=4    unreachable=0    failed=0   

     
[student@workstation lab-roles]$ mv apache_*.j2 roles/myapache/templates/
[student@workstation lab-roles]$ tree roles/myapache/templates/
roles/myapache/templates/
├── apache_httpdconf.j2
└── apache_indexhtml.j2

0 directories, 2 files
[student@workstation lab-roles]$ vi roles/myapache/handlers/main.yml 
[student@workstation lab-roles]$ cat roles/myapache/handlers/main.yml 
---
# handlers file for myapache
- name: restart apache
  service:
    name: httpd
    state: restarted

[student@workstation lab-roles]$ vi roles/myapache/tasks/main.yml 
[student@workstation lab-roles]$ cat roles/myapache/tasks/main.yml 
---
# tasks file for myapache
- name: install apache package
  yum:
    name: httpd
    state: latest

- name: install firewalld package
  yum:
    name: firewalld
    state: latest

- name: template out apache configuration file
  template:
    src: apache_httpdconf.j2
    dest: /etc/httpd/conf/httpd.conf
    owner: root
    group: root
    mode: 0444
  notify:
    - restart apache
  when: apache_enable

- name: template out apache index.html
  template:
    src: apache_indexhtml.j2
    dest: /var/www/html/index.html
    owner: root
    group: root
    mode: 0444
  when: apache_enable

- name: start and enable apache daemon
  service:
    name: httpd
    state: started
    enabled: true
  when: apache_enable

- name: start and enble firewalld daemon
  service:
    name: firewalld
    state: started
    enabled: true
  when: apache_enable

- name: open http firewall port
  firewalld:
    port: 80/tcp
    immediate: true
    permanent: true
    state: enabled
  when: apache_enable

[student@workstation lab-roles]$ vi roles/myapache/defaults/main.yml 
[student@workstation lab-roles]$ cat roles/myapache/defaults/main.yml 
---
# defaults file for myapache
apache_enable: false
[student@workstation lab-roles]$ vi apache.yml
[student@workstation lab-roles]$ cat apache.yml 
---
- name: setup apache on serverb.lab.example.com
  hosts: serverb.lab.example.com
  roles:
    - myapache
[student@workstation lab-roles]$ 

[student@workstation lab-roles]$ ansible-playbook apache.yml 

PLAY [setup apache on serverb.lab.example.com] ********************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [serverb.lab.example.com]

TASK [myapache : install apache package] **************************************************************************************************************************************************************************
ok: [serverb.lab.example.com]

TASK [myapache : install firewalld package] ***********************************************************************************************************************************************************************
ok: [serverb.lab.example.com]

TASK [myapache : template out apache configuration file] **********************************************************************************************************************************************************
skipping: [serverb.lab.example.com]

TASK [myapache : template out apache index.html] ******************************************************************************************************************************************************************
skipping: [serverb.lab.example.com]

TASK [myapache : start and enable apache daemon] ******************************************************************************************************************************************************************
skipping: [serverb.lab.example.com]

TASK [myapache : start and enble firewalld daemon] ****************************************************************************************************************************************************************
skipping: [serverb.lab.example.com]

TASK [myapache : open http firewall port] *************************************************************************************************************************************************************************
skipping: [serverb.lab.example.com]

PLAY RECAP ********************************************************************************************************************************************************************************************************
serverb.lab.example.com    : ok=3    changed=0    unreachable=0    failed=0   

[student@workstation lab-roles]$ 
[student@workstation lab-roles]$ 
[student@workstation lab-roles]$ vi apache.yml 
[student@workstation lab-roles]$ cat apache.yml 
---
- name: setup apache on serverb.lab.example.com
  hosts: serverb.lab.example.com
  roles:
    - role: myapache
      apache_enable: true


[student@workstation lab-roles]$ ansible-playbook apache.yml 

[student@workstation lab-roles]$ curl -s http://serverb.lab.example.com
<!-- Ansible managed -->
<h2>Apache is running!</h2>

  • No labels
Write a comment…