Ansible: Difference between revisions
Content added Content deleted
(4 intermediate revisions by the same user not shown) | |||
Line 68: | Line 68: | ||
copy: |
copy: |
||
dest: /etc/motd |
dest: /etc/motd |
||
content: “{{motd_welcome}}” |
|||
</pre> |
</pre> |
||
Run the playbook: |
Run the playbook: |
||
Line 82: | Line 82: | ||
--- |
--- |
||
- hosts: host1 |
- hosts: host1 |
||
user: |
user: root |
||
vars: |
|||
html: '<html><b>Welcome to Aman.into.tm\nThis is second Line.\n</b></html>' |
|||
tasks: |
tasks: |
||
- name: copy repo file |
|||
copy: src=files/ser.repo dest=/etc/yum.repo.d/ser.repo |
|||
- name: installing apache2 |
- name: installing apache2 |
||
action: apt name=apache2 state=installed |
action: apt name=apache2 state=installed |
||
- name: copy index.html |
- name: copy index.html |
||
copy: |
copy: |
||
dest: /var/www/html/index.html |
|||
content: "{{ html }}" |
|||
- name: restart apache2 |
- name: restart apache2 |
||
service: |
service: |
Latest revision as of 09:43, 20 June 2018
Installation
Adding Repository:
sudo apt-get update sudo apt-get install software-properties-common sudo apt-add-repository ppa:ansible/ansible
Installation:
sudo apt-get update sudo apt-get install ansible
Prepare the Remote Host
Generate SSH Key on the Ansible Server:
ssh-keygen
Copy the keys to the Remote Server:
ssh-copy-id -i ~/.ssh/id_rsa.pub aman@10.100.8.103
Test the Connection using SSH Keys:
ssh aman@10.100.8.103
Configure Ansible
Edit Ansible Hosts File:
sudo nano /etc/ansible/hosts
Create a Host:
my_server ansible_ssh_host=10.100.8.103
Or Create a Group:
[my_web_servers] host1 ansible_ssh_host=192.0.2.1 host2 ansible_ssh_host=192.0.2.2 host3 ansible_ssh_host=192.0.2.3
Execute Commands
ansible -m ping all ansible -m shell -a 'free -m' host1
Notebooks
- Ansible Command line can be used to make small changes.
- Real Usage of Ansible is its scripting capabilities using - Playbooks.
Puppet => Module Chef => Cookbook
- Use Playbooks to perform many actions on multiple machines.
- Playbooks are written in - YAML
- Playbooks have 3 sections
- Target Section - Defines all Hosts on which this playbook will be executed.
- Variable Section - Defines variables which cab be used from the playbooks.
- Tasks Section - Lists all modules intended to run in order.
MOTD Playbook
Create Playbook:
sudo nano /etc/ansible/test1.yml
Paste the below data:
--- - hosts: host1 user: root vars: motd_welcome: ‘Welcome to aman.info.tm\nThis is second Line.\n’ tasks: - name: sample motd copy: dest: /etc/motd content: “{{motd_welcome}}”
Run the playbook:
ansible-playbook test1.yml
Apache Server Playbook
Create a Playbook:
sudo nano /etc/ansible/test2.yml
Paste the below data:
--- - hosts: host1 user: root vars: html: '<html><b>Welcome to Aman.into.tm\nThis is second Line.\n</b></html>' tasks: - name: installing apache2 action: apt name=apache2 state=installed - name: copy index.html copy: dest: /var/www/html/index.html content: "{{ html }}" - name: restart apache2 service: name: apache2 state: restarted
Execute the playbook:
ansible-playbook -v test2.yml --step
- References
{{#widget:DISQUS
|id=networkm
|uniqid=Ansible
|url=https://aman.awiki.org/wiki/Ansible
}}