實際操作測試1 - 使用ssh

以下是小編在看Udemy前的實作測試

Configure ssh

ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub username@host

# in case you cannot do the pubkey auth, do this and it may help:
chmod g-w /home/your_user
chmod 700 /home/your_user/.ssh
chmod 600 /home/your_user/.ssh/authorized_keys

Ansible Inventory Configuration

in /etc/ansible, edit hosts :

[apache] # it is a group name of these servers
# you can specify the user and ssh port number of this server
# Inventory Parameters
# ansible_connection=ssh/winrm/local
# ansible_port=22
# ansible_user=root/administrator
# ansible_ssh_pass=password

apache2 [email protected] ansible_port=22

*Try to ping the remote server

# you can ping the group of server by using the group name
# you can also ping all the host by using all option
# ansible all -m ping
# you can specify a different file for hosts by using -i <path/file>
$ ansible apache -m ping
apache2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

SUCCESS means we can ping the remote server.

*Check date of all remote servers

$ ansible all -i hosts -m "command" -a "date"
apache2 | CHANGED | rc=0 >>
Wed May 29 18:06:47 UTC 2019

Writing Ansible Playbook

let's take an easy one for writing the playbook!

Create a .yml file in /etc/ansible first. For example, I am going to create a apache.ymlfile:

- hosts: apache  # the group for excuting the commands.
  become: true  # use sudo
  tasks:
    - name: install apache2
      apt: name=apache2 update_cache=yes state=latest
      #update_cache stands for sudo apt-get update

Run the .yml file

ansible-playbook apache.yml --ask-become-pass
# --ask-become-pass stands for using sudo
# type host password
BECOME password: 

PLAY [apache] *******************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [apache2]

TASK [install apache2] **********************************************************************************************
 [WARNING]: Updating cache and auto-installing missing dependency: python-apt

 [WARNING]: Could not find aptitude. Using apt-get instead

ok: [apache2]

PLAY RECAP **********************************************************************************************************
apache2                    : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

We add task and handler for apache:

- hosts: apache
  become: true
  tasks:
    - name: install apache2
      apt: name=apache2 update_cache=yes state=latest
    - name: enabled mod_rewrite
      apache2_module: name=rewrite state=present
      notify:
        - restart apache2
  handlers:
    - name: restart apache2
      service: name=apache2 state=restarted
$ ansible-playbook apache.yml --ask-become-pass
BECOME password:

PLAY [apache] *******************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [apache2]

TASK [install apache2] **********************************************************************************************
 [WARNING]: Could not find aptitude. Using apt-get instead

ok: [apache2]

TASK [enabled mod_rewrite] ******************************************************************************************
ok: [apache2]

PLAY RECAP **********************************************************************************************************
apache2                    : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

It will update the status of remote server

Last updated

Was this helpful?