版权声明:本文为Buddy Yuan原创文章,未经允许不得转载。原文地址:ANSIBLE入门学习
当前我的系统是Ubuntu 16.04.4 LTS,将作为ansible的管理机器。使用virtualbox的网卡,IP地址是192.168.56.1。而我的虚拟机使用的是centos系统,IP地址是192.168.56.91。这个虚拟机将要是被我Ansible管理的机器。

postgres@postgres-N65S01:~$ sudo apt-get install ansible
[sudo] password for postgres: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  ieee-data python-crypto python-ecdsa python-httplib2 python-jinja2 python-markupsafe python-netaddr python-paramiko python-pkg-resources python-selinux python-six python-yaml
Suggested packages:
  sshpass python-crypto-dbg python-crypto-doc python-jinja2-doc ipython python-netaddr-docs python-setuptools
The following NEW packages will be installed:
  ansible ieee-data python-crypto python-ecdsa python-httplib2 python-jinja2 python-markupsafe python-netaddr python-paramiko python-pkg-resources python-selinux python-six python-yaml
0 upgraded, 13 newly installed, 0 to remove and 95 not upgraded.
Need to get 2,967 kB of archives.
After this operation, 17.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] y


postgres@postgres-N65S01:~$ ansible --version
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides


postgres@postgres-N65S01:~$ sudo groupadd ansible
postgres@postgres-N65S01:~$ sudo useradd -g ansible ansible
postgres@postgres-N65S01:~$ sudo passwd ansible
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
postgres@postgres-N65S01:/home$ su - ansible
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/postgres/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/postgres/.ssh/id_rsa.
Your public key has been saved in /home/postgres/.ssh/
The key fingerprint is:
SHA256:z0mFh7iuQig7vGlg55GA8i9LyysG9X32fiR41+cdvEQ postgres@postgres-N65S01
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|         . o     |
|.       . o o    |
|o..      . o   E |
|.o.o..  S..  .o  |
|+.o+o ..++o.o .+.|
|=o+o.  o.o++  .o+|
|+*ooo  .  . .  .o|
|o=*+ ..  ...     |


$ ssh-copy-id -i .ssh/ ansible@
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/"
The authenticity of host ' (' can't be established.
ECDSA key fingerprint is SHA256:/+Re8LQTEBXAvC2rNaTpKiuO5vAL+4yBZvRa3soV0zs.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
ansible@'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'ansible@'"
and check to make sure that only the key(s) you wanted were added.


postgres@postgres-N65S01:/etc$ ls -l ansible/*
-rw-r--r-- 1 root root 10301 1月  15  2016 ansible/ansible.cfg
-rw-r--r-- 1 root root   982 8月  21 23:13 ansible/hosts
postgres@postgres-N65S01:/etc$ sudo chown -R ansible:ansible /etc/ansible/*
postgres@postgres-N65S01:/etc$ su - ansible
$ cat /etc/ansible/hosts
# This is the default ansible 'hosts' file.
# It should live in /etc/ansible/hosts
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.

# Ex 2: A collection of hosts belonging to the 'webservers' group


# If you have multiple hosts following a pattern you can specify
# them like this:


# Ex 3: A collection of database servers in the 'dbservers' group


# Here's another example of host ranges, this time there are no
# leading 0s:




$ ansible pg-servers -a "/bin/echo I love ansible" | SUCCESS | rc=0 >>
I love ansible

$ ansible pg-servers -a "/bin/mkdir -p aaa" | SUCCESS | rc=0 >>

postgres@postgres-N65S01:/etc$ ssh ansible@
ansible@'s password: 
Last failed login: Wed Oct 24 23:41:56 CST 2018 from on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Wed Oct 24 23:41:45 2018 from
[ansible@db ~]$ ls -lrt
total 0
drwxrwxr-x. 2 ansible ansible 6 10月 24 23:41 aaa




TEL/電話+86 13764045638
QQ 47079569