# Using ansible to hardening Linux Systems Remotely

**URL:** <https://forum.ansible.com/t/using-ansible-to-hardening-linux-systems-remotely/33631>\
**Category:** Ansible Project\
**Created:** [August 17, 2020, 6:15pm UTC](https://forum.ansible.com/t/using-ansible-to-hardening-linux-systems-remotely/33631 "2020-08-17T18:15:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![giob](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@giob](https://forum.ansible.com/u/giob)\
**Post date:** [August 17, 2020, 6:15pm UTC](https://forum.ansible.com/t/using-ansible-to-hardening-linux-systems-remotely/33631/1 "2020-08-17T18:15:46Z")

</div>

I’m new of the Ansible World, and for the moment i used just to hardening some linux systems using every time the command locally:

> _ansible path\_to\_my\_playbook.yml_

There is a way to run this commant to a remote computer? i tried adding a remote computer in the hosts files but or i make a mistake or it does not work

could you recommend some links that can help me? as far i will have time i will start more in details how ansible works…but now i have to hader lots of Linux systems 😑

A.

---

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [August 17, 2020, 6:31pm UTC](https://forum.ansible.com/t/using-ansible-to-hardening-linux-systems-remotely/33631/2 "2020-08-17T18:31:17Z")

</div>

> I'm new of the Ansible World, and for the moment i used just to hardening some linux systems using every time the command locally:
> 
> > ansible path\_to\_my\_playbook.yml
> 
> There is a way to run this commant to a remote computer? i tried adding a remote computer in the hosts files but or i make a mistake or it does not work

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Try

&nbsp;&nbsp;ansible-playbook path\_to\_my\_playbook.yml

instead

---

<div class="post-metadata">

**Author:** ![giob](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@giob](https://forum.ansible.com/u/giob)\
**Post date:** [August 17, 2020, 8:58pm UTC](https://forum.ansible.com/t/using-ansible-to-hardening-linux-systems-remotely/33631/3 "2020-08-17T20:58:17Z")

</div>

Sorry i made a mistake…i used the command

_ansible-playbook path\_to\_my\_playbook.yml_

and in the hosts files i added the hostname and then the IP address of the remote Computer where i want apply the configurations. But does not work…☹

---

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [August 17, 2020, 9:05pm UTC](https://forum.ansible.com/t/using-ansible-to-hardening-linux-systems-remotely/33631/4 "2020-08-17T21:05:14Z")

</div>

Below are real files of mine. Only difference is that I changed the hostname:

file: hosts  
[test]  
testcentos  
testfedora

File: host\_vars/testcentos.yml

---

<div class="post-metadata">

**Author:** ![jcpetro](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jcpetro](https://forum.ansible.com/u/jcpetro)\
**Post date:** [August 17, 2020, 9:12pm UTC](https://forum.ansible.com/t/using-ansible-to-hardening-linux-systems-remotely/33631/5 "2020-08-17T21:12:38Z")

</div>

Can you post the output with the error message you are getting?

–John

---

<div class="post-metadata">

**Author:** ![giob](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@giob](https://forum.ansible.com/u/giob)\
**Post date:** [August 19, 2020, 11:18am UTC](https://forum.ansible.com/t/using-ansible-to-hardening-linux-systems-remotely/33631/6 "2020-08-19T11:18:15Z")

</div>

For sure i’m making some mistake…

however the _secure.yml_ file i have:

_- name Secure_  
_become: yes_  
_hosts:_  
_secured_  
_roles: - ubuntu18,04-desktop.cis (downloaded by github)_

in the hosts file i put

_[secured]_  
_192.168.1.126_

_fatal: [192.168.1.126]: UNREACHABLE! =\> {“changed”: false, “msg”: “Failed to connect to the host via ssh:”_

but for the user account i use i don’t have any problem to ssh manually in that workstation, even without password because i have exported the key. however the problems seems that the ssh connection via ansible is not permitted. I suppose i have something to configure. but what?

---

<div class="post-metadata">

**Author:** ![jcpetro](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jcpetro](https://forum.ansible.com/u/jcpetro)\
**Post date:** [August 19, 2020, 11:33am UTC](https://forum.ansible.com/t/using-ansible-to-hardening-linux-systems-remotely/33631/7 "2020-08-19T11:33:43Z")

</div>

Do you have ssh keys set up between the ansible control node and the host you are running this against? If not, you need to specify the username and password as well. -u is the switch for user and then you would need -k or - Kk as well, depending on whether you have sudoers set up to allow you to execute passwordless root access or not.

---

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [August 19, 2020, 12:52pm UTC](https://forum.ansible.com/t/using-ansible-to-hardening-linux-systems-remotely/33631/8 "2020-08-19T12:52:30Z")

</div>

Elaborating on what John said,

1. Try something like

ansible secured -m setup

Get the above running first. As he said, you may need to do something like

ansible secured -k -m setup

if you are not using ssh keypairs and/or

ansible secured -u USERNAME -k -m setup

if the login user is not the user you are running as (plus password).

2. If that works,

ansible --become -k -K secured -m shell -a "sudo pwd"

(adding user or not as needed based on results from previous step)

3. If the above works, here is the config file for my testubuntu box:

raub@desktop:~/dev/ansible$ cat host\_vars/testubuntu.yml
