Install Ansible in advance, create a key with ssh-keygen -t rsa so that you can log in with the key.
How to show common settings such as connection
inventory.yml
web:
hosts:
hostname
vars:
domain: helloworld.com
ansible_user: username
ansible_ssh_private_key_file: ~/.ssh/id_rsa
ansible_sudo_pass: test_password
Simply install using the apt command
nginx.yml
- hosts: web
become: yes
tasks:
- name: "install nginx"
apt:
name: ['nginx']
state: latest
Execution result memo
$ ansible-playbook -i inventory.yml nginx.yaml
PLAY [web] ********************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************
ok: [hostname]
TASK [install nginx] **********************************************************************************************************************************************
changed: [hostname]
PLAY RECAP ********************************************************************************************************************************************************
hostname : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Simple operation check that nginx server is running
$ curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Just install using apt
docker.yaml
- hosts: web
become: yes
tasks:
- name: "install docker.io"
apt:
name: ['docker.io']
state: latest
Execution result
$ ansible-playbook -i inventory.yml docker.yaml
PLAY [web] ********************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************
ok: [hostname]
TASK [install docker.io] ******************************************************************************************************************************************
changed: [hostname]
PLAY RECAP ********************************************************************************************************************************************************
hostname : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Confirm that docker is running
$ sudo docker ps
[sudo]password for username:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Recommended Posts