2018-10-31 Ansible 2.7.1在Ubuntu 16.4安装使用

增加PPA源

Ubuntu官方软件仓库的ansible版本是2.0.0.2,而我们要安装2.7版本,通过增加PPA源的方式最简单方便。

PPA是Personal Package Archives首字母简写。翻译为中文意思是:个人软件包文档。

因为某些原因,很多软件包不能进入官方的Ubuntu软件仓库,为了方便Ubuntu用户使用,launchpad.net提供了ppa,允许用户建立自己的软件仓库。PPA也被用来对一些打算进入Ubuntu官方仓库的软件,或者某些软件的新版本进行测试。

Launchpad是Ubuntu母公司canonical有限公司所架设的网站,是一个提供维护、支援或联络Ubuntu开发者的平台。

命令很简单:

sudo add-apt-repository ppa:user/ppa-name
Sudo apt update
Sudo apt upgrade

axing@Bastion_THN:~$ sudo apt-add-repository ppa:ansible/ansible --yes --update

gpg: keyring `/tmp/tmpjcbw7wvc/secring.gpg' created

gpg: keyring `/tmp/tmpjcbw7wvc/pubring.gpg' created

gpg: requesting key 7BB9C367 from hkp server     [keyserver.ubuntu.com](http://keyserver.ubuntu.com/)

gpg: /tmp/tmpjcbw7wvc/trustdb.gpg: trustdb created

gpg: key 7BB9C367: public key "Launchpad PPA for Ansible, Inc." imported

gpg: Total number processed: 1

gpg:               imported: 1  (RSA: 1)

OK

axing@Bastion_THN:~$ sudo apt install ansible

axing@Bastion_THN:~$ sudo ansible --version

ansible 2.7.1

  config file = /etc/ansible/ansible.cfg

  configured module search path = [u'/home/axing/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']

  ansible python module location = /usr/lib/python2.7/dist-packages/ansible

  executable location = /usr/bin/ansible

  python version = 2.7.12 (default, Dec  4 2017, 14:50:18) [GCC 5.4.0 20160609]

由于安装的时候生成的ansible目录属于root,直接运行ansible命令会出错,要加sudo

axing@Bastion_LD8:~$ ls -al

...

drwx------ 3 root  root  4096 Oct 30 17:34 .ansible

测试ansible基本使用

axing@Bastion_LD8:~$ sudo ansible localhost -m ping

[WARNING]: provided hosts list is empty, only localhost is available. Note

that the implicit localhost does not match 'all'

localhost | SUCCESS => {

    "changed": false,

    "ping": "pong"

}

不同于icmp ping,这里返回的"pong"表示按照默认配置远程SSH,ansible可以登录到远端设备。

创建一个文件夹

axing@Bastion_LD8:~$ sudo ansible localhost -m command -a "mkdir /home/axing/ansible/test"

[WARNING]: provided hosts list is empty, only localhost is available. Note

that the implicit localhost does not match 'all'

[WARNING]: Consider using the file module with state=directory rather than

running mkdir.  If you need to use command because file is insufficient you can

add warn=False to this command task or set command_warnings=False in

ansible.cfg to get rid of this message.

localhost | CHANGED | rc=0 >>

“[WARNING]: provided hosts list is empty…” 这句告警是因为没有定义host列表,使用默认 localhost=127.0.0.1,暂时不用管。

第二个WARNING提示你可以用file模块来处理文件夹,还可以在配置文件ansible.cfg里修改set command_warnings=False,取消告警提示。

-a 传递参数细节,可以使用双引号或单引号;

rc=0 表示命令成功执行;

axing@Bastion_LD8:~$ ls ansible/ -ls

total 4

4 drwxr-xr-x 2 root root 4096 Oct 31 12:23 test

现在先删除test目录,加上warn=False参数再运行一遍:

axing@Bastion_LD8:~/ansible$ sudo ansible localhost -m command -a "mkdir /home/axing/ansible/test warn=False"

[WARNING]: provided hosts list is empty, only localhost is available. Note

that the implicit localhost does not match 'all'

localhost | CHANGED | rc=0 >>

第二个warning就没有了。

-m command 是默认的,如果不加模块参数,就像这样:

axing@Bastion_LD8:~/ansible$ sudo ansible localhost -a "mkdir /home/axing/ansible/test warn=False"

[WARNING]: provided hosts list is empty, only localhost is available. Note

that the implicit localhost does not match 'all'

localhost | CHANGED | rc=0 >>

axing@Bastion_LD8:~/ansible$ ls -ls

total 4

4 drwxr-xr-x 2 root root 4096 Oct 31 16:03 test

和加-m command是同样的效果。

既然建议使用模块,那么我们使用-m file参数

相关参数可以在ansible官方帮助文档找到:https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module

axing@Bastion_LD8:~/ansible$ sudo ansible localhost -m file -a "path=/home/axing/ansible/test/ state=directory"

[WARNING]: provided hosts list is empty, only localhost is available. Note

that the implicit localhost does not match 'all'

localhost | CHANGED => {

    "changed": true,

    "gid": 0,

    "group": "root",

    "mode": "0755",

    "owner": "root",

    "path": "/home/axing/ansible/test/",

    "size": 4096,

    "state": "directory",

    "uid": 0

}

axing@Bastion_LD8:~/ansible$ ls

test

这里对目录操作,所以使用state=directory, 如果建立一个文件,则使用 state=touch参数

axing@Bastion_LD8:~/ansible$ sudo ansible localhost -m file -a "path=/home/axing/ansible/test/01 state=touch"

[WARNING]: provided hosts list is empty, only localhost is available. Note

that the implicit localhost does not match 'all'

localhost | CHANGED => {

    "changed": true,

    "dest": "/home/axing/ansible/test/01",

    "gid": 0,

    "group": "root",

    "mode": "0644",

    "owner": "root",

    "size": 0,

    "state": "file",

    "uid": 0

}

axing@Bastion_LD8:~/ansible$ ls test -ls

total 0

0 -rw-r--r-- 1 root root 0 Oct 31 16:22 01

可以看到命令都成功执行了。

你可能感兴趣的:(2018-10-31 Ansible 2.7.1在Ubuntu 16.4安装使用)