ssh to remote node and run CLI

32 views
Skip to first unread message

Abdul Rahim

unread,
Nov 13, 2018, 9:11:12 PM11/13/18
to ansible...@googlegroups.com
Hello Experts,

As you can see struggling through basic tasks will appreciate any pointers , using ansible version 2.7

 cat inventory.yml
fabric01:
  hosts:
    build_node:
      build_host: 192.168.115.101
      build_username: root
      build_password: 123Abdul123

root@ansibile-launch:~/ansible/tasks/add-compute# cat add-compute.yml
---
# Demo Adding Compute Node
- name: Adding Compute Node
  hosts: build_node
  connection: local
  gather_facts: no


  tasks:
  - name: return motd to registered var
    ansible_user= "{{build_username}}"
    ansible_ssh_pass= "{{build_password}}"
    command: 'cp /root/setup_data.yaml /root/setup_data.yaml.bak'

Appreciate if you can please int me in the right direction here ..

Regards,
AR 

Brian Coca

unread,
Nov 13, 2018, 9:26:53 PM11/13/18
to ansible...@googlegroups.com
Without an error I can only guess, one thing i've noticed is that you
are incorrectly formatting the task

- name: return motd to registered var
command: 'cp /root/setup_data.yaml /root/setup_data.yaml.bak'
vars:
ansible_user= "{{build_username}}"
ansible_ssh_pass= "{{build_password}}"



--
----------
Brian Coca

Abdul Rahim

unread,
Nov 13, 2018, 9:56:15 PM11/13/18
to ansible...@googlegroups.com
Thans Brian , 

It fails with following 

root@ansibile-launch:~/ansible/tasks/add-compute# ansible-playbook -i inventory.yml add-compute.yml -vvvv
ansible-playbook 2.7.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.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-playbook
  python version = 2.7.12 (default, Dec  4 2017, 14:50:18) [GCC 5.4.0 20160609]
Using /etc/ansible/ansible.cfg as config file
setting up inventory plugins
/root/ansible/tasks/add-compute/inventory.yml did not meet host_list requirements, check plugin documentation if this is unexpected
/root/ansible/tasks/add-compute/inventory.yml did not meet script requirements, check plugin documentation if this is unexpected
Parsed /root/ansible/tasks/add-compute/inventory.yml inventory source with yaml plugin
ERROR! Syntax Error while loading YAML.
  mapping values are not allowed in this context

The error appears to have been in '/root/ansible/tasks/add-compute/add-compute.yml': line 11, column 23, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  - name: Copy Test
          ansible_user: "{{build_username}}"
                      ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

root@ansibile-launch:~/ansible/tasks/add-compute#

root@ansibile-launch:~/ansible/tasks/add-compute# cat add-compute.yml
---
# Demo Adding Compute Node
- name: Adding Compute Node
  hosts: build_node
  connection: local
  gather_facts: no


  tasks:
  - name: Copy Test
          ansible_user: "{{build_username}}"
          ansible_ssh_pass: "{{build_password}}"
          ansible_connection: ssh
          command: cp /root/arahim/ansible/tasks/add-compute.yml /root/arahim/ansible/tasks/add-compute.yml.bak
root@ansibile-launch:~/ansible/tasks/add-compute# cat inventory.yml
fabric01:
  hosts:
    build_node:
      build_host: 192.168.115.101
      build_username: root
      build_password: 123Abdul123


It does work with below


root@ansibile-launch:~/ansible/tasks/add-compute# ansible-playbook -i new_inventory test.yml
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo' (default). This feature will be removed in version 2.9. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.

PLAY [all] ******************************************************************************************************************************************************************************************************

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

TASK [Copy file] ************************************************************************************************************************************************************************************************
changed: [192.168.115.101]

PLAY RECAP ******************************************************************************************************************************************************************************************************
192.168.115.101            : ok=2    changed=1    unreachable=0    failed=0

root@ansibile-launch:~/ansible/tasks/add-compute# cat new_inventory
[hosts]
192.168.115.101 ansible_connection=ssh ansible_ssh_user=root ansible_ssh_pass=123Abdul123

root@ansibile-launch:~/ansible/tasks/add-compute# cat test.yml
---
- hosts: all
  user: root
  vars:
    createuser: 'ansible'
    createpassword: '123Abdul123'
  tasks:
  - name: Copy file
    command: cp /root/arahim/ansible/tasks/add-compute.yml /root/arahim/ansible/tasks/add-compute.yml.bak
    sudo: true


Not sure what is wrong with the yml version of inventory and the variables called .. but I am able to now make progress , thanks for getting back to me on this

Regards,
AR

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7fh-MUbzxu2zp8Abat5URfJekrvpO4K8yoWqx%2BDasBO4w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

vinoth kumar

unread,
Nov 18, 2018, 1:47:51 PM11/18/18
to ansible...@googlegroups.com
Hi Abdul,

You could remove the hosts from inventory.ym , else directly mention it in default hosts inventory file 

Br
Vinoth

Reply all
Reply to author
Forward
0 new messages