为什么80%的码农都做不了架构师?>>>
首先目录还是按照以前的格式
root@ip-172-31-6-109:/etc/ansible# pwd
/etc/ansible
root@ip-172-31-6-109:/etc/ansible# tree
.
├── ansible.cfg
├── callbacks
│ ├── jastme.py
│ └── jastme.pyc
├── facts.d
│ └── jastme.fact
├── hosts
└── playbooks
├── register_var.yml
├── templates
│ └── test.j2
└── test.yml
4 directories, 8 files
我们来看看
cat test.yml
- hosts : t_nginx
remote_user : ec2-user
tasks :
- name : test ansible facts for template
template : src=templates/test.j2 dest=/tmp/test.txt
- name : test shell command result registe to be var
command : ls /tmp #这里是调用的shell命令
register : hehe #注册这个命令的结果,把它赋值给hehe 所以有了变量hehe
- name : validate the var
command : echo {{ hehe }} #测试变量是否可用
执行以下,先看看我的回调配置
cat callbacks/jastme.py
class CallbackModule(object):
#if foo:
# self.disabled = True
pass
def runner_on_ok(self, host, res):
pass
# print res['ansible_facts']
print res
# if 'stdout' in res.keys():
# print res['stdout']
# if 'state' in res.keys():
# print res['state']
# if 'invocation' in res.keys():
# print res['invocation']
执行playbook
ansible-playbook playbooks/test.yml
PLAY [t_nginx] ****************************************************************
GATHERING FACTS ***************************************************************
ok: [172.31.9.125]
{'invocation': {'module_name': 'setup', 'module_args': ''}, u'verbose_override': True, u'changed': False, u'ansible_facts': {u'ansible_product_serial': u'NA', u'ansible_form_factor': u'Other', u'ansible_product_version': u'4.2.amazon', u'ansible_user_id': u'ec2-user', u'ansible_memtotal_mb': 14881, u'ansible_product_uuid': u'NA', u'ansible_ssh_host_key_ecdsa_public': u'AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFbZfuubOoj2okUgTcQvh4gNcB1jARGLKdWth4jLMtfLXuhLu4KA0KXGtrC7szb/AQBlqItdWZE2H73uBF++bo8=', u'ansible_distribution_version': u'7.1', u'ansible_domain': u'', u'ansible_date_time': {u'tz': u'UTC', u'hour': u'03', u'year': u'2016', u'tz_offset': u'+0000', u'month': u'03', u'epoch': u'1458617487', u'iso8601_micro': u'2016-03-22T03:31:27.757927Z', u'time': u'03:31:27', u'date': u'2016-03-22', u'iso8601': u'2016-03-22T03:31:27Z', u'day': u'22', u'minute': u'31', u'second': u'27'}, u'ansible_processor_cores': 1, u'ansible_virtualization_role': u'guest', u'ansible_env': {u'LANG': u'C', u'TERM': u'linux', u'SHELL': u'/bin/bash', u'XDG_RUNTIME_DIR': u'/run/user/1000', u'SELINUX_ROLE_REQUESTED': u'', u'SHLVL': u'2', u'SSH_TTY': u'/dev/pts/1', u'HOME': u'/home/ec2-user', u'SSH_CLIENT': u'172.31.6.109 49517 22', u'LESSOPEN': u'||/usr/bin/lesspipe.sh %s', u'PWD': u'/home/ec2-user', u'SELINUX_USE_CURRENT_RANGE': u'', u'LOGNAME': u'ec2-user', u'USER': u'ec2-user', u'MAIL': u'/var/mail/ec2-user', u'PATH': u'/usr/local/bin:/usr/bin', u'SELINUX_LEVEL_REQUESTED': u'', u'XDG_SESSION_ID': u'4273', u'_': u'/usr/bin/python', u'SSH_CONNECTION': u'172.31.6.109 49517 172.31.9.125 22'}, u'ansible_processor_vcpus': 2, u'ansible_bios_version': u'4.2.amazon', u'ansible_processor': [u'Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz', u'Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz'], u'ansible_virtualization_type': u'xen', u'ansible_lo': {u'mtu': 65536, u'device': u'lo', u'promisc': False, u'ipv4': {u'netmask': u'255.0.0.0', u'network': u'127.0.0.0', u'address': u'127.0.0.1'}, u'ipv6': [{u'scope': u'host', u'prefix': u'128', u'address': u'::1'}], u'active': True, u'type': u'loopback'}, u'ansible_userspace_bits': u'64', u'ansible_architecture': u'x86_64', u'ansible_default_ipv4': {u'macaddress': u'02:3b:52:73:56:73', u'network': u'172.31.0.0', u'mtu': 9001, u'alias': u'eth0', u'netmask': u'255.255.240.0', u'address': u'172.31.9.125', u'interface': u'eth0', u'type': u'ether', u'gateway': u'172.31.0.1'}, u'ansible_swapfree_mb': 0, u'ansible_default_ipv6': {}, u'ansible_distribution_release': u'Maipo', u'ansible_system_vendor': u'Xen', u'ansible_os_family': u'RedHat', u'ansible_cmdline': {u'LANG': u'en_US.UTF-8', u'console': u'tty0', u'crashkernel': u'auto', u'BOOT_IMAGE': u'/boot/vmlinuz-3.10.0-229.el7.x86_64', u'ro': True, u'root': u'UUID=6785eb86-c596-4229-85fb-4d30c848c6e8'}, u'ansible_selinux': {u'status': u'enabled', u'policyvers': 28, u'type': u'targeted', u'mode': u'permissive', u'config_mode': u'enforcing'}, u'ansible_userspace_architecture': u'x86_64', u'ansible_swaptotal_mb': 0, u'ansible_product_name': u'HVM domU', u'ansible_pkg_mgr': u'yum', u'ansible_memfree_mb': 1635, u'ansible_devices': {u'xvda': {u'scheduler_mode': u'deadline', u'rotational': u'0', u'vendor': None, u'sectors': u'2147483648', u'host': u'', u'sectorsize': u'512', u'removable': u'0', u'support_discard': u'0', u'model': None, u'partitions': {u'xvda1': {u'start': u'2048', u'sectorsize': 512, u'sectors': u'2048', u'size': u'1.00 MB'}, u'xvda2': {u'start': u'4096', u'sectorsize': 512, u'sectors': u'2147479519', u'size': u'1024.00 GB'}}, u'holders': [], u'size': u'1.00 TB'}}, u'ansible_distribution': u'RedHat', u'ansible_processor_count': 1, u'ansible_hostname': u'ip-172-31-9-125', u'ansible_bios_date': u'05/06/2015', u'ansible_all_ipv6_addresses': [u'fe80::3b:52ff:fe73:5673'], u'ansible_interfaces': [u'lo', u'eth0'], u'ansible_machine': u'x86_64', u'ansible_ssh_host_key_rsa_public': u'AAAAB3NzaC1yc2EAAAADAQABAAABAQCeZWU97i6remsNZV7tPAu6R2ImGCpb5fUcq/8oNDh7rS++jYjaBrHO+SmNvpXv6/0LXoRjyNdjeRPYi5nG2V1kXTvxbJOREKDWF37zz7zkqn7Dq5ZBUMEYxe88uPRq8HdQ3HnmnpH50CGfyYs0mm/qbmUQgjNZtEmo+PVwAfE8vUp0K4Qgmy6EkTzgH58NV3ZX2SRISbVAq1jFF/Yljf5YpNeHuM5xc1NfkyjQpJafX6xN1UXV6eucDSRxcEJMMd73npymy8+lx/LeHI8FnKdZzGzZd3Nsf7JQmUhUCD6EjcldrSXMB7NOgEoviFWx8KHQ1WEuHAai167lloehhrwh', u'ansible_kernel': u'3.10.0-229.el7.x86_64', u'ansible_processor_threads_per_core': 2, u'ansible_fqdn': u'master', u'ansible_mounts': [{u'size_total': 1099499024384, u'mount': u'/', u'size_available': 777148985344, u'fstype': u'xfs', u'device': u'/dev/xvda2', u'options': u'rw,seclabel,relatime,attr2,inode64,noquota'}], u'ansible_eth0': {u'macaddress': u'02:3b:52:73:56:73', u'module': u'xen_netfront', u'mtu': 9001, u'device': u'eth0', u'promisc': True, u'ipv4': {u'netmask': u'255.255.240.0', u'network': u'172.31.0.0', u'address': u'172.31.9.125'}, u'ipv6': [{u'scope': u'link', u'prefix': u'64', u'address': u'fe80::3b:52ff:fe73:5673'}], u'active': True, u'type': u'ether'}, u'ansible_system': u'Linux', u'ansible_all_ipv4_addresses': [u'172.31.9.125'], u'ansible_python_version': u'2.7.5'}}
TASK: [test ansible facts for template] ***************************************
ok: [172.31.9.125]
{u'group': u'ec2-user', u'uid': 1000, u'changed': False, 'invocation': {'module_name': 'template', 'module_args': 'src=templates/test.j2 dest=/tmp/test.txt'}, u'state': u'file', u'gid': 1000, u'secontext': u'unconfined_u:object_r:user_home_t:s0', u'mode': u'0600', u'owner': u'ec2-user', u'path': u'/tmp/test.txt', u'size': 60}
TASK: [test shell command result registe to be var] ***************************
changed: [172.31.9.125]
{u'changed': True, u'end': u'2016-03-22 03:31:28.350423', u'stdout': u'hsperfdata_root\nkafka4-logs\nmongodb-27017.sock\nopenapiaccess.log\nopenapierror.log\nsoaaccess.log\nsoaerror.log\ntemp_dir\ntest.txt', u'cmd': [u'ls', u'/tmp'], u'rc': 0, u'start': u'2016-03-22 03:31:28.347464', u'stderr': u'', u'delta': u'0:00:00.002959', 'invocation': {'module_name': 'command', 'module_args': 'ls /tmp'}}
TASK: [validate the var] ******************************************************
changed: [172.31.9.125]
{u'changed': True, u'end': u'2016-03-22 03:31:28.606013', u'stdout': u'{uchanged: True, uend: u2016-03-22 03:31:28.350423, ustdout: uhsperfdata_root\\nkafka4-logs\\nmongodb-27017.sock\\nopenapiaccess.log\\nopenapierror.log\\nsoaaccess.log\\nsoaerror.log\\ntemp_dir\\ntest.txt, ucmd: [uls, u/tmp], ustart: u2016-03-22 03:31:28.347464, udelta: u0:00:00.002959, ustderr: u, urc: 0, invocation: {module_name: command, module_args: ls /tmp}, stdout_lines: [uhsperfdata_root, ukafka4-logs, umongodb-27017.sock, uopenapiaccess.log, uopenapierror.log, usoaaccess.log, usoaerror.log, utemp_dir, utest.txt]}', u'cmd': [u'echo', u'{uchanged:', u'True,', u'uend:', u'u2016-03-22 03:31:28.350423,', u'ustdout:', u'uhsperfdata_root\\nkafka4-logs\\nmongodb-27017.sock\\nopenapiaccess.log\\nopenapierror.log\\nsoaaccess.log\\nsoaerror.log\\ntemp_dir\\ntest.txt,', u'ucmd:', u'[uls,', u'u/tmp],', u'ustart:', u'u2016-03-22 03:31:28.347464,', u'udelta:', u'u0:00:00.002959,', u'ustderr:', u'u,', u'urc:', u'0,', u'invocation:', u'{module_name:', u'command,', u'module_args:', u'ls /tmp},', u'stdout_lines:', u'[uhsperfdata_root,', u'ukafka4-logs,', u'umongodb-27017.sock,', u'uopenapiaccess.log,', u'uopenapierror.log,', u'usoaaccess.log,', u'usoaerror.log,', u'utemp_dir,', u'utest.txt]}'], u'rc': 0, u'start': u'2016-03-22 03:31:28.603292', u'stderr': u'', u'delta': u'0:00:00.002721', 'invocation': {'module_name': 'command', 'module_args': u"echo {u'changed': True, u'end': u'2016-03-22 03:31:28.350423', u'stdout': u'hsperfdata_root\\nkafka4-logs\\nmongodb-27017.sock\\nopenapiaccess.log\\nopenapierror.log\\nsoaaccess.log\\nsoaerror.log\\ntemp_dir\\ntest.txt', u'cmd': [u'ls', u'/tmp'], u'start': u'2016-03-22 03:31:28.347464', u'delta': u'0:00:00.002959', u'stderr': u'', u'rc': 0, 'invocation': {'module_name': 'command', 'module_args': 'ls /tmp'}, 'stdout_lines': [u'hsperfdata_root', u'kafka4-logs', u'mongodb-27017.sock', u'openapiaccess.log', u'openapierror.log', u'soaaccess.log', u'soaerror.log', u'temp_dir', u'test.txt']}"}}
PLAY RECAP ********************************************************************
172.31.9.125 : ok=4 changed=2 unreachable=0 failed=0
以上的结果可以很明确的发现执行的结果被赋值成了变量,然后利用echo 打印出了变量