From 4645029a279405d2a41bb0a90d8286610a183674 Mon Sep 17 00:00:00 2001 From: yumaojun <719118794@qq.com> Date: Tue, 23 Feb 2016 11:48:09 +0800 Subject: [PATCH 1/2] =?UTF-8?q?1.=20=E9=85=8D=E7=BD=AEansible=20=E4=BD=BF?= =?UTF-8?q?=E7=94=A8paramiko=E8=BF=9B=E8=A1=8C=E9=93=BE=E6=8E=A5=EF=BC=88s?= =?UTF-8?q?sh=20=E6=9C=89=E5=8D=A1=E6=AD=BB=E9=97=AE=E9=A2=98,=20=E7=AD=89?= =?UTF-8?q?=E5=BE=85=E5=8F=8D=E9=A6=88.=EF=BC=89=202.=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?freebsd=20=E6=8E=A8=E9=80=81=E5=B8=A6sudo=E8=A7=84=E5=88=99?= =?UTF-8?q?=E7=9A=84=E7=94=A8=E6=88=B7=E6=97=B6=20=E7=94=B1=E4=BA=8E=20sed?= =?UTF-8?q?=20=20=E5=BC=95=E8=B5=B7=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jperm/ansible_api.py | 5 +++-- templates/jperm/role_sudo.j2 | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/jperm/ansible_api.py b/jperm/ansible_api.py index 0ceea61d5..953271917 100644 --- a/jperm/ansible_api.py +++ b/jperm/ansible_api.py @@ -125,7 +125,7 @@ class MyRunner(MyInventory): self.results_raw = {} def run(self, module_name='shell', module_args='', timeout=10, forks=10, pattern='*', - become=False, become_method='sudo', become_user='root', become_pass=''): + become=False, become_method='sudo', become_user='root', become_pass='', transport='paramiko'): """ run module from andible ad-hoc. module_name: ansible module_name @@ -140,7 +140,8 @@ class MyRunner(MyInventory): become=become, become_method=become_method, become_user=become_user, - become_pass=become_pass + become_pass=become_pass, + transport=transport ) self.results_raw = hoc.run() logger.debug(self.results_raw) diff --git a/templates/jperm/role_sudo.j2 b/templates/jperm/role_sudo.j2 index 642a30a66..c48d354c1 100644 --- a/templates/jperm/role_sudo.j2 +++ b/templates/jperm/role_sudo.j2 @@ -13,7 +13,12 @@ add_cmd_alias() { {% for sudo_name, sudo_cmd in sudo_alias.items %} {% if sudo_name != 'ALL' %} if $(grep '^Cmnd_Alias \<{{ sudo_name }}\>' ${sudo_file} &> /dev/null); then - sed -i 's@^Cmnd_Alias \<{{ sudo_name }}\>.*@Cmnd_Alias {{ sudo_name }} = {{ sudo_cmd }}@g' ${sudo_file} + isbsd=$(uname -a | grep -i 'freebsd' &> /dev/null && echo "yes" || echo "no") + if [ $isbsd == "yes" ]; then + sed -i .bk 's@^Cmnd_Alias \<{{ sudo_name }}\>.*@Cmnd_Alias {{ sudo_name }} = {{ sudo_cmd }}@g' ${sudo_file} + else + sed -i 's@^Cmnd_Alias \<{{ sudo_name }}\>.*@Cmnd_Alias {{ sudo_name }} = {{ sudo_cmd }}@g' ${sudo_file} + fi else echo "Cmnd_Alias {{ sudo_name }} = {{ sudo_cmd }}" >> ${sudo_file} fi @@ -27,7 +32,12 @@ add_role_chosen() { sudo_file=$1 {% for user, alias in sudo_user.items %} if $(grep '^{{ user }}\>' ${sudo_file} &> /dev/null); then - sed -i 's@^{{ user }}\>.*@{{ user }} ALL = (root) NOPASSWD: {{ alias }}@g' ${sudo_file} + isbsd=$(uname -a | grep -i 'freebsd' &> /dev/null && echo "yes" || echo "no") + if [ $isbsd == "yes" ]; then + sed -i .bk 's@^{{ user }}\>.*@{{ user }} ALL = (root) NOPASSWD: {{ alias }}@g' ${sudo_file} + else + sed -i 's@^{{ user }}\>.*@{{ user }} ALL = (root) NOPASSWD: {{ alias }}@g' ${sudo_file} + fi else echo "{{ user }} ALL = (root) NOPASSWD: {{ alias }}" >> ${sudo_file} fi From 3b5daf19c3aa52fd81bab2e97619411ae370c2bc Mon Sep 17 00:00:00 2001 From: yumaojun <719118794@qq.com> Date: Tue, 23 Feb 2016 15:21:27 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=EF=BC=91.=20=E4=BF=AE=E5=A4=8Dfreebsd=20?= =?UTF-8?q?=E6=8E=A8=E9=80=81=E5=B8=A6sudo=E8=A7=84=E5=88=99=E7=9A=84?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=97=B6=20,=20sudo=E3=80=80=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E5=BC=95=E5=8F=91=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- templates/jperm/role_sudo.j2 | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/templates/jperm/role_sudo.j2 b/templates/jperm/role_sudo.j2 index c48d354c1..46f96342f 100644 --- a/templates/jperm/role_sudo.j2 +++ b/templates/jperm/role_sudo.j2 @@ -4,6 +4,13 @@ real_file=/etc/sudoers tmp_file=$(mktemp /tmp/XXXXXXX) +# fixed sudoers file path in bsd +isbsd=$(uname -a | grep -i 'freebsd' &> /dev/null && echo "yes" || echo "no") +if [ $isbsd == "yes" ]; then + real_file=/usr/local/etc/sudoers +fi + + # Backup sudoers file cp ${real_file} ${tmp_file} @@ -13,7 +20,6 @@ add_cmd_alias() { {% for sudo_name, sudo_cmd in sudo_alias.items %} {% if sudo_name != 'ALL' %} if $(grep '^Cmnd_Alias \<{{ sudo_name }}\>' ${sudo_file} &> /dev/null); then - isbsd=$(uname -a | grep -i 'freebsd' &> /dev/null && echo "yes" || echo "no") if [ $isbsd == "yes" ]; then sed -i .bk 's@^Cmnd_Alias \<{{ sudo_name }}\>.*@Cmnd_Alias {{ sudo_name }} = {{ sudo_cmd }}@g' ${sudo_file} else @@ -32,7 +38,6 @@ add_role_chosen() { sudo_file=$1 {% for user, alias in sudo_user.items %} if $(grep '^{{ user }}\>' ${sudo_file} &> /dev/null); then - isbsd=$(uname -a | grep -i 'freebsd' &> /dev/null && echo "yes" || echo "no") if [ $isbsd == "yes" ]; then sed -i .bk 's@^{{ user }}\>.*@{{ user }} ALL = (root) NOPASSWD: {{ alias }}@g' ${sudo_file} else